程序似乎有点问题 但是我还找不到原因 求解啊
结果中37和62好象是一样的 还有很多一样的 不知道怎么办
public class tour {
final static public int M = 8;//
static public int[][] path = new int[M][M];// 记录步子的数组
final static int[] dx = new int[] { 2, 1, -1, -2, -2, -1, 1, 2 };
final static int[] dy = new int[] { -1, -2, -2, -1, 1, 2, 2, 1 };// pose
public static int counter = 0;
public static int total = 0; public tour() { } public static void initpath() {
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
path[i][j] = 0;// 将数组初始为0
} static public void display() { for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) {
initpath();
counter = 0;
set(i, j); // initpath();
}
} public static void set(int x, int y) { if (test(x, y)) {
path[x][y] = counter++; /* System.out.println(counter); */
if (counter == M * M + 1) {
total++;
System.out.printf("第%d种\n", total);
for (int s = 0; s < M; s++) {
for (int t = 0; t < M; t++)
System.out.printf("%2d ", path[s][t]);
System.out.println();
} }// 打印
//System.out.println();
for (int i = 0; i < 8; i++) {
set(x + dx[i], y + dy[i]);
}
} } private static boolean test(int x, int y) {
if (x < 0 || x >= M || y < 0 || y >= M)
return false;
else if (path[x][y] == 0)
return true;
return false;
} public static void main(String args[]) {
display();
}}
结果中37和62好象是一样的 还有很多一样的 不知道怎么办
public class tour {
final static public int M = 8;//
static public int[][] path = new int[M][M];// 记录步子的数组
final static int[] dx = new int[] { 2, 1, -1, -2, -2, -1, 1, 2 };
final static int[] dy = new int[] { -1, -2, -2, -1, 1, 2, 2, 1 };// pose
public static int counter = 0;
public static int total = 0; public tour() { } public static void initpath() {
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
path[i][j] = 0;// 将数组初始为0
} static public void display() { for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) {
initpath();
counter = 0;
set(i, j); // initpath();
}
} public static void set(int x, int y) { if (test(x, y)) {
path[x][y] = counter++; /* System.out.println(counter); */
if (counter == M * M + 1) {
total++;
System.out.printf("第%d种\n", total);
for (int s = 0; s < M; s++) {
for (int t = 0; t < M; t++)
System.out.printf("%2d ", path[s][t]);
System.out.println();
} }// 打印
//System.out.println();
for (int i = 0; i < 8; i++) {
set(x + dx[i], y + dy[i]);
}
} } private static boolean test(int x, int y) {
if (x < 0 || x >= M || y < 0 || y >= M)
return false;
else if (path[x][y] == 0)
return true;
return false;
} public static void main(String args[]) {
display();
}}
解决方案 »
- eclipse中的build path加入一个jar,居然无法使用,为何??
- 关于jTree的问题
- Jface中的TreeView如何用程序实现选中结点?
- 初学者的问题
- 弱弱的提个问题~关于AOP的~~
- java 如何实现图片的叠加,图片中添加文字,高分求助
- 菜鸟的一个简单问题(实际上是非常有用的知识点),请高手指点指点!!!!
- 请问我将调用JNI的类放到一个包里面就出错,如果没有包就可以,什么原因?怎么解决???
- 兄弟们,给推荐一本介绍java包的书吧?
- 专题探讨:《如何能高效率的返回在接口方法里接收的数据》
- Java UDP文件传输,传输后文件损坏......
- 麻烦分析下这个简单的java程序
for (int j = 0; j < M; j++) {
initpath();
counter = 0;
set(i, j); // initpath();
}
}每走一步就要把数组全初始化一遍,counter也变为0,那之后的结果肯定有不少一样的啊,至少每次test(int x, int y)都是true
for (int j = 0; j < M; j++) {
initpath();
counter = 0;
set(i, j); // initpath();
}
}将这里的counter=0改为=1,这回输出结果都不同了,第i种方案里第i个位置为第一步
话说虽然现在第i种方法中第i个位置为第一步,但是同样的起点不能有两种以上走法吗?没研究过这问题。。