算法 迷宫问题

定义一个二维数组:
int maze[5][5] = {

	0, 1, 0, 0, 0,

	0, 1, 0, 1, 0,

	0, 0, 0, 0, 0,

	0, 1, 1, 1, 0,

	0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

输入:

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

输出:

左上角到右下角的最短路径,格式如样例所示。

样例输入:

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

样例输出:

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

解题代码:

01 //* @author:
02 import java.util.*;
03 public class Main {
04  
05  private class Point {
06  
07   int x = 0;
08   int y = 0;
09  
10   public Point() {
11    this(00);
12   }
13  
14   public Point(int x, int y) {
15    this.x = x;
16    this.y = y;
17   }
18  
19   public boolean equals(Point p) {
20    return (x == p.x) && (y == p.y);
21   }
22  
23   @Override
24   public String toString() {
25    return "(" + x + ", " + y + ")";
26   }
27  }
28  
29  private int[][] maze = null;  //迷宫图
30  private Stack< Point> stack = new Stack< Point>();
31  //保存路径的栈
32  
33  public Main(int[][] maze) {
34   this.maze = maze;
35  }
36  
37  public void go() {
38   Point out = new Point(maze.length-1, maze[0].length-1);  //出口
39   Point in = new Point(0,0);  //入口
40   Point curNode = in;    //当前点为入口
41   Point nextNode = null;  //下一个访问点(目标点)
42  
43   while(!curNode.equals(out)) {
44    nextNode = new Point(curNode.x,curNode.y);   //设置目标点为当前点,便于下面偏移
45    if((curNode.x+1)< maze.length&&maze[curNode.x+1][curNode.y]==0) {  //如果下方是空的,则目标点向下偏移
46     nextNode.x++;
47    else if((curNode.y+1)< maze[0].length&&maze[curNode.x][curNode.y+1]==0) {  //如果右边是空的,则目标点向右偏移
48     nextNode.y++;
49    else if((curNode.x-1)>=0&&maze[curNode.x-1][curNode.y]==0) {  //如果上方是空的,则目标点向上偏移
50     nextNode.x--;
51    else if((curNode.y-1)>=0&&maze[curNode.x][curNode.y-1]==0) {  //如果左边是空的,则目标点向左偏移
52     nextNode.y--;
53    else {  //这里是没有路的状态
54     maze[curNode.x][curNode.y] = 3;  //标记为死路
55     if(stack.isEmpty()) {   //判断栈是否为空
56      System.out.println("Non solution");
57      return;
58     }
59     curNode = stack.pop();    //弹出上一次的点
60     continue;    //继续循环
61    }
62  
63    //如果有路的话会执行到这里
64    stack.push(curNode);   //当前点压入栈中
65    maze[curNode.x][curNode.y] = 2;   //标记为已走
66    curNode = nextNode;   //移动当前点
67   }
68  
69   if(nextNode.equals(out)) {
70    stack.push(nextNode);   //将出口点添加到当前路劲中
71    maze[nextNode.x][nextNode.y] = 2;   //标记为已走
72   }
73  // System.out.println("\n该迷宫的一条可行路劲为:");
74   
75  
76    for(int i=0;i< stack.size();i++)
77      System.out.println(stack.elementAt(i));
78  
79  
80  }
81  
82  public static void main(String[] args) {
83   Scanner in=new Scanner(System.in);
84   int[][] maze=new int[5][5];
85   for(int i=0;i< 5;i++)
86    for(int j=0;j< 5;j++)
87      maze[i][j]=in.nextInt();
88  
89  
90   new Main(maze).go();
91  }
92 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回溯算法是一种经典的解决问题的算法,它的基本思想是在问题的解空间树中,按照深度优先的策略,从根节点出发搜索解空间树。当搜索到某个节点时,如果该节点不满足问题的约束条件,则回溯到该节点的父节点,继续搜索。迷宫问题是回溯算法的一个经典应用,通过回溯算法可以生成迷宫并求解迷宫。 下面是C++实现迷宫的代码: 1.初始化迷宫 vector<vector<Point>> InitMaze(vector<vector<Point>> maze, int n) { //初始化迷宫 迷宫边缘赋0,其余处0,1间隔 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if ((i + j) % 2 == 0 && i != 0 && j != 0 && i != n - 1 && j != n - 1 && i % 2 != 0) { maze[i][j].x = i; maze[i][j].y = j; maze[i][j].value = 1; maze[i][j].flag = false; //用于判断之前是否已经到达过这个方块 } } } return maze; } 2.生成迷宫 void CreateMaze(vector<vector<Point>> &maze, int n) { stack<Point> s; Point cur; cur.x = 1; cur.y = 1; s.push(cur); while (!s.empty()) { cur = s.top(); s.pop(); if (cur.x == n - 2 && cur.y == n - 2) { break; } if (maze[cur.x][cur.y].flag) { continue; } maze[cur.x][cur.y].flag = true; int direction = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; random_shuffle(direction, direction + 4); for (int i = 0; i < 4; i++) { int next_x = cur.x + direction[i] * 2; int next_y = cur.y + direction[i] * 2; if (next_x >= 1 && next_x < n - 1 && next_y >= 1 && next_y < n - 1 && !maze[next_x][next_y].flag) { s.push(maze[next_x][next_y]); maze[(cur.x + next_x) / 2][(cur.y + next_y) / 2].value = 1; } } } } 3.求解迷宫 bool FindPath(vector<vector<Point>> &maze, int n, int x, int y) { if (x == n - 2 && y == n - 2) { maze[x][y].value = 2; return true; } if (maze[x][y].value == 0 || maze[x][y].flag) { return false; } maze[x][y].flag = true; if (FindPath(maze, n, x + 1, y) || FindPath(maze, n, x - 1, y) || FindPath(maze, n, x, y + 1) || FindPath(maze, n, x, y - 1)) { maze[x][y].value = 2; return true; } return false; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值