麻烦帮我看看以下java代码哪里错了?

HuikangLiang 2012-03-04 08:56:14
public class ArrayList {
static int X=8;
static int Y=8;
static int[][] chess=new int[X][Y];
static int a,b;
public static void main(String[] args){
for(int i=0;i<X;i++){
for(int j=0;j<Y;j++){
chess[i][j]=0;
}
}
if(TravelChessBoard(2,0,1)==1){
for(int i=0;i<X;i++){
for(int j=0;j<Y;j++){
System.out.print(chess[i][j]+"\t");
}
System.out.println();
}
}
}
public static int nextxy(int x,int y,int count){
switch(count){
case 0:
if(x+2<=X-1&&y-1>=0&&chess[x+2][y-1]==0){
a=x+2;
b=y-1;
return 1;
}
break;
case 1:
if(x+2<=X-1&&y+1<=Y-1&&chess[x+2][y+1]==0){
a=x+2;
b=y+1;
return 1;
}
break;
case 2:
if(x+1<=X-1&&y-2>=0&&chess[x+1][y-2]==0){
a=x+1;
b=y-2;
return 1;
}
break;
case 3:
if(x+1<=X-1&&y+2<=Y-1&&chess[x+1][y+2]==0){
a=x+1;
b=y+2;
return 1;
}
break;
case 4:
if(x-2>=0&&y-1>=0&&chess[x-2][y-1]==0){
a=x-2;
b=y-1;
return 1;
}
break;
case 5:
if(x-2>=0&&y+1<=Y-1&&chess[x-2][y+1]==0){
a=x-2;
b=y+1;
return 1;
}
break;
case 6:
if(x-1>=0&&y-2>=0&&chess[x-1][y-2]==0){
a=x-1;
b=y-2;
return 1;
}
break;
case 7:
if(x-1>=0&&y+2<=Y-1&&chess[x-1][y+2]==0){
a=x-1;
b=y+2;
return 1;
}
break;

default :
break;
}
return 0;
}
public static int TravelChessBoard(int x,int y,int tag){
int flag=0,count=0;
chess[x][y]=tag;
if(tag==X*Y){
return 1;
}
flag=nextxy(x,y,count);
while(flag==0&&count<7){
count=count+1;
flag=nextxy(x,y,count);
}
while(flag==1){
if(TravelChessBoard(a,b,tag+1)==1){
return 1;
}
count=count+1;
flag=nextxy(x,y,count);
while(flag==0&&count<7){
count=count+1;
flag=nextxy(x,y,count);
}
if(flag==0){
chess[x][y]=0;
}
}
return 1;
}
}
输出结果:
0 0 39 44 0 0 0 0
40 43 0 0 0 47 0 0
1 34 41 38 45 0 31 48
42 15 36 0 32 29 46 0
35 2 33 16 37 24 49 30
14 5 12 9 22 19 28 25
3 10 7 20 17 26 23 50
6 13 4 11 8 21 18 27


是国际象棋中的马踏棋盘问题,即马要踏遍每一个空格
...全文
152 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiumeilema 2012-03-05
  • 打赏
  • 举报
回复
楼下同感
安特矮油 2012-03-05
  • 打赏
  • 举报
回复
说真的,看到这个我已经蛋疼了。
dd646711228 2012-03-05
  • 打赏
  • 举报
回复
国际象棋不会哎。。。
gGke26 2012-03-05
  • 打赏
  • 举报
回复
国际象棋上马是走日吗?
java_lover01 2012-03-05
  • 打赏
  • 举报
回复
说实话 我一看到你的类名 我就蛋疼了 下面更是 还要研究象棋,下次提问 请说出哪里出问题出什么问题,谢谢合作.挣分难啊 .
Acesidonu 2012-03-04
  • 打赏
  • 举报
回复
还要先研究国际象棋
hezhao4444 2012-03-04
  • 打赏
  • 举报
回复
每人会回答你的问题,知道为什么吗?如果真回答你真种问题。只能说他蛋疼

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧