这是一个《骑士问题》,请问如果想退出递归,但是不退出程序,有什么好方法?谢谢class Stack<E>{
private class Node{
private Node prev;
private E e;
private Node(E e){
this.e = e;
}
private E get(){
return e;
}
}
private Node cur;
private int size;
public void push(E e){
Node n = new Node(e);
n.prev = cur;
cur = n;
size++;
}
public E pop(){
if(size<=0)
return null;
size--;
try{
return cur.get();
}finally{
cur = cur.prev;
}
}
public int size(){
return size;
}
}
public class Knight{
private int map[][],blocks;
private Stack<int[]> s;
public Knight(int x,int y){
map = new int[x+2][y+2];
}
public void start(int x,int y){
init();
displayMatrix();
travel(x,y);
}
public void setBlock(int x,int y){
map[x][y] = -1;
blocks++;
}
private void travel(int x,int y){
if(map[x][y]!=0)
return;
map[x][y] = 1;
s.push(new int[]{x,y});
if(s.size()==(map.length-2)*(map[0].length-2)-blocks){
while(s.size()>0){
int[] temp = s.pop();
map[temp[0]][temp[1]] = s.size();
}
displayMatrix();
System.exit(0);//<<就是这里,不得不用exit(0)来退出递归
}
travel(x+1,y);
travel(x-1,y);
travel(x,y+1);
travel(x,y-1);
s.pop();
map[x][y] = 0;
}
private void displayMatrix(){
for(int i=0;i<map.length;i++)
for(int j=0;j<map[i].length;j++)
System.out.print(map[i][j]+(map[i][j]<10&&map[i][j]>=0?" ":" ")+(j==map[i].length-1?"\n":""));
}
private void init(){
s = new Stack<int[]>();
for(int i=0;i<map.length;i++)
for(int j=0;j<map[i].length;j++)
map[i][j] = i==0||j==0||i==map.length-1||j==map[i].length-1?2:map[i][j]==-1?-1:0;
}
public static void main(String args[]){
Knight k = new Knight(8,7);
k.setBlock(3,3);
k.setBlock(4,5);
k.start(2,2);
}
}
private class Node{
private Node prev;
private E e;
private Node(E e){
this.e = e;
}
private E get(){
return e;
}
}
private Node cur;
private int size;
public void push(E e){
Node n = new Node(e);
n.prev = cur;
cur = n;
size++;
}
public E pop(){
if(size<=0)
return null;
size--;
try{
return cur.get();
}finally{
cur = cur.prev;
}
}
public int size(){
return size;
}
}
public class Knight{
private int map[][],blocks;
private Stack<int[]> s;
public Knight(int x,int y){
map = new int[x+2][y+2];
}
public void start(int x,int y){
init();
displayMatrix();
travel(x,y);
}
public void setBlock(int x,int y){
map[x][y] = -1;
blocks++;
}
private void travel(int x,int y){
if(map[x][y]!=0)
return;
map[x][y] = 1;
s.push(new int[]{x,y});
if(s.size()==(map.length-2)*(map[0].length-2)-blocks){
while(s.size()>0){
int[] temp = s.pop();
map[temp[0]][temp[1]] = s.size();
}
displayMatrix();
System.exit(0);//<<就是这里,不得不用exit(0)来退出递归
}
travel(x+1,y);
travel(x-1,y);
travel(x,y+1);
travel(x,y-1);
s.pop();
map[x][y] = 0;
}
private void displayMatrix(){
for(int i=0;i<map.length;i++)
for(int j=0;j<map[i].length;j++)
System.out.print(map[i][j]+(map[i][j]<10&&map[i][j]>=0?" ":" ")+(j==map[i].length-1?"\n":""));
}
private void init(){
s = new Stack<int[]>();
for(int i=0;i<map.length;i++)
for(int j=0;j<map[i].length;j++)
map[i][j] = i==0||j==0||i==map.length-1||j==map[i].length-1?2:map[i][j]==-1?-1:0;
}
public static void main(String args[]){
Knight k = new Knight(8,7);
k.setBlock(3,3);
k.setBlock(4,5);
k.start(2,2);
}
}
这个Exception可以自己定义然后在相应的地方把它catch掉
在需要的时候判断一下
最好可以类似循环的break那样,又简洁,又好用的,不知有没有
C语言是这样的
但是写JAVA,写得好看一点,自己看着也挺高兴的
散分