java递归迷宫

问题:会有跳动

自己代码:
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

 

相似代码不同结局,郁闷,查错中——地图变量将局部变量设成成员变量了。已改正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java迷宫归分割算法是一种生成迷宫的算法,其基本思想是将迷宫看作一个矩形网格,通过不断分割矩形,最终形成迷宫。具体实现过程如下: 1. 初始化迷宫的矩形网格,将网格中每个单元格都设置为墙壁。 2. 选择一个起始点作为迷宫的入口,并将其设置为通道。 3. 选择一个结束点作为迷宫的出口,并将其设置为通道。 4. 从入口开始,按照归分割算法进行分割,直到到达出口。 5. 分割算法的具体过程如下: - 随机选择当前区域的一个墙壁,并将其打通,使其成为通道。 - 将当前区域分成两个子区域,分别归进行分割,直到不能再分割为止。 Java代码实现如下: ``` public class MazeGenerator { private int[][] maze; private int width; private int height; public MazeGenerator(int width, int height) { this.width = width; this.height = height; maze = new int[width][height]; generateMaze(0, 0, width - 1, height - 1); } private void generateMaze(int x1, int y1, int x2, int y2) { if (x2 < x1 || y2 < y1) { return; } int wallX = x1 + (int) (Math.random() * (x2 - x1 + 1)); int wallY = y1 + (int) (Math.random() * (y2 - y1 + 1)); // Make a passage in the wall. for (int i = x1; i <= x2; i++) { if (i != wallX) { maze[i][wallY] = 1; } } for (int j = y1; j <= y2; j++) { if (j != wallY) { maze[wallX][j] = 1; } } // Recursively divide the sub-areas. generateMaze(x1, y1, wallX - 1, wallY - 1); generateMaze(wallX + 1, y1, x2, wallY - 1); generateMaze(x1, wallY + 1, wallX - 1, y2); generateMaze(wallX + 1, wallY + 1, x2, y2); } public void printMaze() { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (maze[j][i] == 0) { System.out.print("#"); } else { System.out.print(" "); } } System.out.println(); } } public static void main(String[] args) { MazeGenerator maze = new MazeGenerator(20, 20); maze.printMaze(); } } ``` 运行上述代码可以生成一个20x20的迷宫,并输出迷宫
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值