我用java编写了一个6*6的马跳日程序,但运行效率太低了,请高手指教

zyp2005 2005-09-28 04:43:14
下面是我的程序,结果能出来,但要花7分钟左右,且使用范围很有局限性。我现在希望把它的效率提高,且能较快的计算8*8或10*10的任何一个马跳日问题。
小妹刚学java,望各位高手帮帮忙哦,有急用!
import java.util.*;

public class Point
{
boolean visited;
int[] next_nums; //存储点的序号,而非点实例

static int [] stack = new int [37]; //堆栈
static boolean [][] acc = new boolean[37][37]; //存储两点间是否可以到达


public Point()
{
visited = false;
next_nums = new int[8];
}

public static void main(String Args[])
{
Point[] points =//new Point[36];
{
new Point(), //存储36个点实例,1-36
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point(),
new Point()};
int top = -1;
//points = InitPoint(points);

//********************************
int [] p = new int [8]; /* 最大能访问的节点有8个*/
int [][] a = new int[6][6];/*存放6*6 的棋盘并编号*/
int k,t=0,add=0;
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
{
a[i][j] = ++add;

}
for(int x=0;x<6;x++)
for(int y=0;y<6;y++) /*逐个考察每个节点,把它能访问的节点号记录下来*/
{
t = 0;
k=a[x][y];
System.out.print(""+k+":");
if(resab(x-2) && resab(y-1))
{
points[k].next_nums[t++] = a[x-2][y-1];
acc[k][a[x-2][y-1]] = true;
System.out.print(a[x-2][y-1]);
}
if(resab(x-2) && resab(y+1))
{
points[k].next_nums[t++] = a[x-2][y+1];
acc[k][a[x-2][y+1]] = true;
System.out.print(","+a[x-2][y+1]);
}
if(resab(x-1) && resab(y+2))
{
points[k].next_nums[t++] = a[x-1][y+2];
acc[k][a[x-1][y+2]] = true;
System.out.print(","+a[x-1][y+2]);
}
if(resab(x+1) && resab(y+2))
{
points[k].next_nums[t++] = a[x+1][y+2];
acc[k][a[x+1][y+2]] = true;
System.out.print(","+a[x+1][y+2]);
}
if(resab(x+2) && resab(y+1))
{
points[k].next_nums[t++] = a[x+2][y+1];
acc[k][a[x+2][y+1]] = true;
System.out.print(","+a[x+2][y+1]);
}
if(resab(x+2) && resab(y-1))
{
points[k].next_nums[t++] = a[x+2][y-1];
acc[k][a[x+2][y-1]] = true;
System.out.print(","+a[x+2][y-1]);
}
if(resab(x+1) && resab(y-2))
{
points[k].next_nums[t++] = a[x+1][y-2];
acc[k][a[x+1][y-2]] = true;
System.out.print(","+a[x+1][y-2]);
}
if(resab(x-1) && resab(y-2))
{
points[k].next_nums[t++] = a[x-1][y-2];
acc[k][a[x-1][y-2]] = true;
System.out.print(","+a[x-1][y-2]);
}
System.out.println("");

}


//******************************
//System.out.println(""+acc[1][1]);
points[1].visited = true;
top++;
stack[top] = 9;
points[9].visited = true;
while(top>=0)
{
System.out.println(""+stack[top]);
//try{
// System.in.read();
//}
//catch(Exception e){}

int top_num = stack[top];
int sub_num;
boolean exist = false;
for(int j=0;j<8;j++)
{
sub_num = points[top_num].next_nums[j];
if(!points[sub_num].visited && acc[top_num][sub_num] && sub_num!=0)
{
exist =true;
stack[++top] = sub_num;
points[sub_num].visited = true;
break;
}
}
if(!exist)
{
if(top_num == 14 && completed(points))
{
System.out.println("success");
output_data(top);
break;
}
else
{
System.out.println("one line failed !");
//try{
// System.in.read();
// }
// catch(Exception e){}

for(int js=0;js<8;js++)
{
sub_num = points[top_num].next_nums[js];
if(sub_num!=0)
{
acc[top_num][sub_num] = true;
}
}

acc[stack[top-1]][top_num] = false;
points[top_num].visited = false;
top--;
}
}

}
}

public static boolean resab(int i)
{
if(i>=0 && i<=5)return true;
else return false;
}


public static boolean completed(Point[] points)
{
boolean suc = true;
for(int i=1;i<=36;i++)
if(!points[i].visited) suc = false;
return suc;

}

public static void output_data(int top)
{
int i=0;
while(i<=top)
System.out.println(stack[i++]);
}
}

方法较笨,您们就有耐心点看吧。我建立poinit对象数组实现,为什么不行啊.
...全文
319 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
taolei 2005-09-30
  • 打赏
  • 举报
回复
漏了一行:
if (n <= 4)
{
System.out.println("no answer.");
return;
}
taolei 2005-09-30
  • 打赏
  • 举报
回复
算法是抄来的,我改编了一下
public static void answer(int n)
{
if (n <= 4)
System.out.println("no answer.");
int[][] a = new int[n][n];
int x = 0;
int y = 0;
a[x][y] = 1;
for(int index=2;index<=n*n;index++)
{
int nextx = -1;
int nexty = -1;
int level = Integer.MAX_VALUE;
for(int i=0;i<8;i++)
{
int x1 = x + (i%2==0?i/4 + 1:-i/4-1);
int y1 = y + ((i/2)%2==0?2-i/4:i/4-2);
if (x1>=0 && x1<n && y1>=0 && y1<n && a[x1][y1] == 0)
{
int c = 0;
for(int j=0;j<8;j++)
{
int x2 = x1 + (j%2==0?j/4 + 1:-j/4-1);
int y2 = y1 + ((j/2)%2==0?2-j/4:j/4-2);
if(x2>=0 && x2<n && y2 >=0 && y2<n && a[x2][y2]==0)
c++;
}
if(c < level)
{
level = c;
nextx = x1;
nexty = y1;
}
}
}
x = nextx;
y = nexty;
a[x][y] = index;
}
System.out.println(n + " x " + n);
for(y=0;y<n;y++)
{
for(x=0;x<n;x++)
{
System.out.print(a[x][y] + "\t");
}
System.out.println();
}
System.out.println();
}


5 x 5
1 22 11 16 7
12 17 8 21 10
25 2 23 6 15
18 13 4 9 20
3 24 19 14 5

6 x 6
1 10 31 20 7 12
32 19 8 11 30 21
9 2 25 36 13 6
18 33 16 27 22 29
3 26 35 24 5 14
34 17 4 15 28 23

7 x 7
1 30 11 46 27 32 9
12 45 28 31 10 37 26
29 2 49 38 47 8 33
42 13 44 19 34 25 36
3 16 41 48 39 22 7
14 43 18 5 20 35 24
17 4 15 40 23 6 21

8 x 8
1 50 15 32 61 28 13 30
16 33 64 55 14 31 60 27
51 2 49 44 57 62 29 12
34 17 56 63 54 47 26 59
3 52 45 48 43 58 11 40
18 35 20 53 46 41 8 25
21 4 37 42 23 6 39 10
36 19 22 5 38 9 24 7
zouzhejun 2005-09-30
  • 打赏
  • 举报
回复
了解...
zyp2005 2005-09-29
  • 打赏
  • 举报
回复
马跳日问题就是走象棋时马走日啊,我现在的问题是:有一个6*6的棋盘,怎样让马在棋盘的每个位置上走一次且仅一次。也就是数据结构里的哈密顿回路问题。大家帮忙想想哦。
suyejun2005 2005-09-29
  • 打赏
  • 举报
回复
什么叫马跳日?


走过象棋吗兄弟?
Michael_javavb 2005-09-29
  • 打赏
  • 举报
回复
大致看了一下,首先提几个建议,你实例化point对象的方法不太好吧,怎么不用循环呢,main方法里面的方法太多,不象java了,把这些方法都定义到类里去吧,用对象来引用。main里只需要一个语句来执行程序就可以了
算法复杂度也不大,可能是因为引用对象花去的时间,我觉得七分钟也算正常的,你里面反复的引用对象,是会慢的
saiche05 2005-09-29
  • 打赏
  • 举报
回复
终于知道:马跳日问题就是走象棋时马走日。
zhuangzhou 2005-09-29
  • 打赏
  • 举报
回复
我总觉得可以用递归来写
zhuangzhou 2005-09-29
  • 打赏
  • 举报
回复
寒楼主一个,写的那么复杂
jky 2005-09-28
  • 打赏
  • 举报
回复
什么叫马跳日?
gxl0328 2005-09-28
  • 打赏
  • 举报
回复
厉害 刚接触java就能写马跳日了
sunxutx 2005-09-28
  • 打赏
  • 举报
回复
好长阿
能说下想实现什么功能么

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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