请问如果想退出递归,但是不退出程序,有什么好方法?谢谢
这是一个《骑士问题》,请问如果想退出递归,但是不退出程序,有什么好方法?谢谢
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;