我用java编写了一个6*6的马跳日程序,但运行效率太低了,请高手指教
下面是我的程序,结果能出来,但要花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对象数组实现,为什么不行啊.