问题:会有跳动
自己代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* 迷宫,求出所有路径。递归 思路:在递归中判断路线是否可以走。一个方法 需要一个值做路线记录 问题:如何在二维数组中判断值
*/
public class MazeTest {
// 地图显示
static int maze[][] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 1, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
// 设置起终坐标
final static int[] start = { 8, 3 };
final static int[] end = { 8, 8 };
static int[][] directions = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
// 递归方法——需要用栈,不然要凉,用先进后出。但是这样也会有问题,这个要怎么退才能判断是否有另一条路。要判断不能走重复的路。
// 走过则改变值么。如果走过则变值为2,然后进行返值。{0,-1}{0,1}{1,0}{-1,0}
public static int go(People people, Stack<People> step,int[][] maze, List<Stack> steps) {
// 设置递归出口
if (people.x == end[0] && people.y == end[1]) {
Stack<People> stackAnswer = new Stack<People>();
for (int i = 0; i < step.size(); i++) {
stackAnswer.add(step.get(i));
}
steps.add(stackAnswer);
return 0;
}
// 得到下一步方位
for (int i = 0; i < directions.length; i++) {
People pNow = people.add(directions[i],people);
if (maze[pNow.x][pNow.y] == 0) {
step.push(pNow);
maze[pNow.x][pNow.y] = -1;
int go = go(pNow, step, maze,steps);
if (go != 1) {
step.pop();
maze[pNow.x][pNow.y] = 0;
} else {
return 1;
}
}
}
return 0;
}
public static void main(String[] args) {
int mazeCopy[][] = new int[maze.length][maze[0].length];
for (int i = 0; i < mazeCopy.length; i++) {
for (int j = 0; j < mazeCopy[0].length; j++) {
mazeCopy[i][j] = maze[i][j];
}
}
People p = new People(start);
// 记录次数
Stack step = new Stack<People>();
step.push(p);
// 记录路线
List<Stack> steps = new ArrayList<Stack>();
steps.add(step);
//改正后
mazeCopy[start[0]][start[1]] = -1;
//原语句 maze[start[0]][start[1]] = -1;
int pathNum = go(p,step,mazeCopy,steps);
for (Stack<People> stack2 : steps) {
for (People step2 : stack2) {
System.out.println(step2);
}
System.out.println("-----------------------");
}
}
/**
* TODO:废料 if (people.x != 10) { if (maze[people.x + 1][people.y] == '0') {
* people.x = people.x + 1; people.xy.add(new int[] { people.x, people.y });
* return go(people); } } if (people.x != 10 && people.y != 10) { if
* (maze[people.x][people.y + 1] == '0') { people.y = people.y + 1;
* people.xy.add(new int[] { people.x, people.y }); return go(people); } } if
* (maze[people.x + 1][people.y + 1] == '0') { people.x = people.x + 1; people.y
* = people.y + 1; people.xy.add(new int[] { people.x, people.y }); return
* go(people); } if (people.x != 0) { if (maze[people.x - 1][people.y] == '0') {
* people.x = people.x - 1; people.xy.add(new int[] { people.x, people.y });
* return go(people); } } if (people.x != 0 && people.y != 0) { if
* (maze[people.x - 1][people.y - 1] == '0') { people.x = people.x - 1; people.y
* = people.y - 1; people.xy.add(new int[] { people.x, people.y }); return
* go(people); } } if (people.y != 0) { if (maze[people.x][people.y - 1] == '0')
* { people.y = people.y - 1; people.xy.add(new int[] { people.x, people.y });
* return go(people); } }
*/
}
import java.util.ArrayList;
import java.util.List;
public class People {
public int x;
public int y;
public People() {
}
People add(int[] xy,People p) {
People stepNew = new People(new int[] {p.x,p.y});
stepNew.x += xy[0];
stepNew.y += xy[1];
return stepNew;
}
public People(int[] xy1) {
super();
x = xy1[0];
y = xy1[1];
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
网上他人代码
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Demo {
static int maze[][] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 1, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
static int[][] directions = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
static class Step {
int x, y;
public Step(int x, int y) {
super();
this.x = x;
this.y = y;
}
Step add(int[] xy) {
Step stepNew = new Step(x, y);
stepNew.x += xy[0];
stepNew.y += xy[1];
return stepNew;
}
Step copy() {
Step stepNew = new Step(x, y);
return stepNew;
}
@Override
public String toString() {
return x + "," + y;
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof Step && x == ((Step) obj).x && y == ((Step) obj).y;
}
}
static Step start = new Step(7, 1);
static Step end = new Step(8, 8);
public static void main(String[] args) {
int mazeCopy[][] = new int[maze.length][maze[0].length];
for (int i = 0; i < mazeCopy.length; i++) {
for (int j = 0; j < mazeCopy[0].length; j++) {
mazeCopy[i][j] = maze[i][j];
}
}
Stack<Step> stack = new Stack<Step>();
stack.add(start);
maze[start.x][start.y] = -1;
List<Stack<Step>> list = new ArrayList<Stack<Step>>();
int path = getPath(list, stack, maze, start, end);
System.out.println(path);
// while(!stack.isEmpty()){
// Step step=stack.pop();
// System.out.println(step);
// }
System.out.println(list.size());
for (Stack<Step> stack2 : list) {
for (Step step : stack2) {
System.out.println(step);
}
System.out.println("-----------------------");
}
}
private static int getPath(List<Stack<Step>> list, Stack<Step> stack, int[][] maze, Step stepNow, Step stepEnd) {
if (stepNow.equals(stepEnd)) {
Stack<Step> stackAnswer = new Stack<Step>();
for (int i = 0; i < stack.size(); i++) {
stackAnswer.add(stack.get(i));
}
list.add(stackAnswer);
return 0;
}
for (int i = 0; i < directions.length; i++) {
Step stepNext = stepNow.add(directions[i]);
// System.out.println(stepNext);
if (maze[stepNext.x][stepNext.y] == 0) {
stack.push(stepNext);
maze[stepNext.x][stepNext.y] = -1;
int result = getPath(list, stack, maze, stepNext, stepEnd);
if (result != 1) {
stack.pop();
maze[stepNext.x][stepNext.y] = 0;
} else {
return 1;
}
}
}
return 0;
}
}
https://blog.csdn.net/wuhen0616/article/details/49821203
相似代码不同结局,郁闷,查错中——地图变量将局部变量设成成员变量了。已改正