用java输出马走日的路径问题,求解 本帖最后由 piaobosiSE 于 2011-10-23 10:04:11 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有点啰嗦的代码,观望更好的吧:import java.awt.Point;import java.util.ArrayList;import java.util.List;public class Test3 { List<Point> list = new ArrayList<Point>(); public static void main(String[] args) { Test3 test = new Test3(); test.list.add(new Point(0, 0)); test.path(new Point(0, 0)); } public void path(Point curPoint) { if (curPoint.x > 8 || curPoint.y > 4 || curPoint.x < 0 || curPoint.y < 0) return; if (curPoint.x == 8) { for (int i = 0; i < list.size(); i++) { System.out.print("(" + list.get(i).x + "," + list.get(i).y + ")"); } System.out.println(); return; } Point point = new Point(curPoint.x + 1, curPoint.y + 2); fun(point); point = new Point(curPoint.x + 2, curPoint.y + 1); fun(point);// point = new Point(curPoint.x - 1, curPoint.y + 2);// fun(point);//// point = new Point(curPoint.x - 2, curPoint.y + 1);// fun(point); point = new Point(curPoint.x + 1, curPoint.y - 2); fun(point); point = new Point(curPoint.x + 2, curPoint.y - 1); fun(point);// point = new Point(curPoint.x - 1, curPoint.y - 2);// fun(point);//// point = new Point(curPoint.x - 2, curPoint.y - 1);// fun(point); } private void fun(Point point) { if (!list.contains(point)) { list.add(point); path(point); list.remove(point); } }} public class HorseMove { static Boolean isAllow(int x, int y){ if(x >= 0 && x <= 8 && y >= 0 && y <= 4) return true; return false; } static void move(int x, int y, int[][] mean, int[][] step, int stepCnt){ if(x == 8 && y >= 0 && y <= 4){ System.out.print("(0 0) "); for(int i = 0; i < 8 && step[0][i]!=0; ++i) System.out.print("(" + step[0][i] + " " + step[1][i] + ") "); System.out.println(); return; } for(int j = 0; j < 4; ++j){ if(isAllow(x+mean[0][j], y+mean[1][j])){ step[0][stepCnt] = x+mean[0][j]; step[1][stepCnt] = y+mean[1][j]; move(x+mean[0][j], y+mean[1][j], mean, step, stepCnt+1); } } } public static void main(String[] args){ int[][] mean = {{1, 2, 2, 1}, {2, 1, -1, 2}}; int[][] step = new int[2][8]; for(int i = 0; i < 2; ++i) for(int j = 0; j < 8; ++j) step[i][j] = 0; move(0, 0, mean, step, 0); }}用数组写的还有点问题会重复输出 public class HorseMove { static Boolean isAllow(int x, int y){ if(x >= 0 && x <= 8 && y >= 0 && y <= 4) return true; return false; } static void move(int x, int y, int[][] mean, int[][] step, int stepCnt){ if(x == 8 && y >= 0 && y <= 4){ System.out.print("(0 0) "); for(int i = 0; i < 8; ++i){ System.out.print("(" + step[0][i] + " " + step[1][i] + ") "); if(step[0][i] == 8) break; } System.out.println(); return; } for(int j = 0; j < 4; ++j){ if(isAllow(x+mean[0][j], y+mean[1][j])){ step[0][stepCnt] = x+mean[0][j]; step[1][stepCnt] = y+mean[1][j]; move(x+mean[0][j], y+mean[1][j], mean, step, stepCnt+1); } } } public static void main(String[] args){ int[][] mean = {{1, 2, 2, 1}, {2, 1, -1, -2}}; int[][] step = new int[2][8]; for(int i = 0; i < 2; ++i) for(int j = 0; j < 8; ++j) step[i][j] = 0; move(0, 0, mean, step, 0); }}这个应该没问题了 接口 请教一个string.replaceall正则问题 jtable列变宽,其余不变 在Jbuilder8中遇到的三个问题 程序编译的时候报错,请看详细内容 怎样用jsp生成gif图片,高分求救!!!!! 如何在Jbuilder中实现数据的回显. 如何获得javax.swing包?急!急!急! 帮忙看看下面的代码的意思! 大力推荐《java 2 编程指南 jdk1.3版》,我买了! 在eclipse中postgreSQL的访问问题 请教:用Eclipse 导出jar文件 双击后无法运行
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;public class Test3 {
List<Point> list = new ArrayList<Point>(); public static void main(String[] args) {
Test3 test = new Test3();
test.list.add(new Point(0, 0));
test.path(new Point(0, 0)); } public void path(Point curPoint) {
if (curPoint.x > 8 || curPoint.y > 4 || curPoint.x < 0
|| curPoint.y < 0)
return;
if (curPoint.x == 8) {
for (int i = 0; i < list.size(); i++) {
System.out.print("(" + list.get(i).x + "," + list.get(i).y
+ ")");
}
System.out.println();
return;
}
Point point = new Point(curPoint.x + 1, curPoint.y + 2);
fun(point); point = new Point(curPoint.x + 2, curPoint.y + 1);
fun(point);// point = new Point(curPoint.x - 1, curPoint.y + 2);
// fun(point);
//
// point = new Point(curPoint.x - 2, curPoint.y + 1);
// fun(point); point = new Point(curPoint.x + 1, curPoint.y - 2);
fun(point); point = new Point(curPoint.x + 2, curPoint.y - 1);
fun(point);// point = new Point(curPoint.x - 1, curPoint.y - 2);
// fun(point);
//
// point = new Point(curPoint.x - 2, curPoint.y - 1);
// fun(point);
} private void fun(Point point) {
if (!list.contains(point)) {
list.add(point);
path(point);
list.remove(point);
}
}
}
public class HorseMove {
static Boolean isAllow(int x, int y){
if(x >= 0 && x <= 8 && y >= 0 && y <= 4)
return true;
return false;
}
static void move(int x, int y, int[][] mean, int[][] step, int stepCnt){
if(x == 8 && y >= 0 && y <= 4){
System.out.print("(0 0) ");
for(int i = 0; i < 8 && step[0][i]!=0; ++i)
System.out.print("(" + step[0][i] + " " + step[1][i] + ") ");
System.out.println();
return;
}
for(int j = 0; j < 4; ++j){
if(isAllow(x+mean[0][j], y+mean[1][j])){
step[0][stepCnt] = x+mean[0][j];
step[1][stepCnt] = y+mean[1][j];
move(x+mean[0][j], y+mean[1][j], mean, step, stepCnt+1);
}
}
}
public static void main(String[] args){
int[][] mean = {{1, 2, 2, 1}, {2, 1, -1, 2}};
int[][] step = new int[2][8];
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 8; ++j)
step[i][j] = 0;
move(0, 0, mean, step, 0);
}}
用数组写的
还有点问题会重复输出
public class HorseMove {
static Boolean isAllow(int x, int y){
if(x >= 0 && x <= 8 && y >= 0 && y <= 4)
return true;
return false;
}
static void move(int x, int y, int[][] mean, int[][] step, int stepCnt){
if(x == 8 && y >= 0 && y <= 4){
System.out.print("(0 0) ");
for(int i = 0; i < 8; ++i){
System.out.print("(" + step[0][i] + " " + step[1][i] + ") ");
if(step[0][i] == 8)
break;
}
System.out.println();
return;
}
for(int j = 0; j < 4; ++j){
if(isAllow(x+mean[0][j], y+mean[1][j])){
step[0][stepCnt] = x+mean[0][j];
step[1][stepCnt] = y+mean[1][j];
move(x+mean[0][j], y+mean[1][j], mean, step, stepCnt+1);
}
}
}
public static void main(String[] args){
int[][] mean = {{1, 2, 2, 1}, {2, 1, -1, -2}};
int[][] step = new int[2][8];
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 8; ++j)
step[i][j] = 0;
move(0, 0, mean, step, 0);
}}
这个应该没问题了