走迷宫 oj

走迷宫

Time Limit: 1000 ms  Memory Limit: 65536 KiB
Problem Description
有一个m*n格的迷宫(表示有m行、n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,输入这m*n个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-1表示无路)。
Input
第一行是两个数m,n(1< m, n< 15),接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点。
Output
所有可行的路径,输出时按照左上右下的顺序。描述一个点时用(x,y)的形式,除开始点外,其他的都要用“->”表示。如果没有一条可行的路则输出-1。
Sample Input
5 4
1 1 0 0
1 1 1 1
0 1 1 0
1 1 0 1
1 1 1 1
1 1
5 4
Sample Output
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
Hint
Source
#include<stdio.h>  
#include<string.h>  
#include<vector>  
#include<queue>  
using namespace std;  
const int maxn =30;  
const int dx[] = {0,-1,0,1};  
const int dy[] = {-1,0,1,0};  
int a[maxn][maxn];  
int vis[maxn][maxn];  
int aa[maxn*maxn][3];  
int n,m,e_x,e_y;  
bool f = false;  
inline bool inside(int x,int y)  
{  
   return x>=1&&x<=n&&y>=1&&y<=m&&a[x][y];  
}  
void dfs(int x,int y,int d)  
{  
  
   if(x==e_x &&y ==e_y)  
   {  
      for(int i=0;i<d-1;i++)  
      {  
         printf("(%d,%d)->",aa[i][0],aa[i][1]);  
      }  
      printf("(%d,%d)\n",e_x,e_y);  
      f = true;  
      return ;  
   }  
   for(int i=0;i<4;i++)  
   {  
     int xx=x+dx[i];  
     int yy =y +dy[i];  
     if(inside(xx,yy)&&!vis[xx][yy])  
     {  
        vis[xx][yy] = 1;  
        aa[d][0] = xx;  
        aa[d][1] = yy;  
        dfs(xx,yy,d+1);  
        vis[xx][yy]  = 0;  
  
     }  
  
  
  
   }  
  
  
}  
int main()  
{  
    scanf("%d%d",&n,&m);  
    for(int i=1;i<=n;i++)  
    for(int j=1;j<=m;j++)  
    scanf("%d",&a[i][j]);  
    int f_x,f_y;  
    scanf("%d%d%d%d",&f_x,&f_y,&e_x,&e_y);  
    if(f_x==e_x&&f_y==e_y)  
    {  
    printf("-1\n");  
    return 0;  
  
    }  
    aa[0][0] = f_x;  
    aa[0][1] = f_y;  
    vis[f_x][f_y] = 1;  
    dfs(f_x,f_y,1);  
    if(!f)printf("-1");  
    return 0;  
  
  
  
  
  
} 

### SCAU OJ 19618 简单迷宫解题思路 #### 背景分析 简单迷宫问题是典型的路径规划问题之一,通常可以通过广度优先搜索(BFS)或者深度优先搜索(DFS)来解决。这类问题的核心在于如何有效地遍历整个迷宫并找到最短路径或可行路径。 对于SCAU OJ 19618中的简单迷宫问题,假设输入是一个二维矩阵表示的迷宫地图,其中某些位置可以通行而另一些不可通行。目标是从起点到终点找到一条有效路径,并返回该路径长度或其他指定的结果。 --- #### 数据结构与算法选择 为了高效解决问题,可以选择以下数据结构和方法: 1. **队列 (Queue)** 使用队列为辅助工具实现 BFS,因为 BFS 是逐层扩展节点的方式,能够保证首次到达某个点的距离是最短距离[^1]。 2. **访问标记数组 (Visited Array)** 创建一个布尔类型的二维数组 `visited` 来记录哪些位置已经被访问过,从而避免重复计算以及陷入死循环。 3. **方向向量 (Direction Vectors)** 定义四个移动方向(上下左右),通过这些偏移量模拟在迷宫中的行过程。 --- #### 实现代码 以下是基于 C++ 的一种可能实现方式: ```cpp #include <iostream> #include <queue> using namespace std; // 方向定义:上、下、左、右 const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; struct Node { int x, y; int step; // 当前步数 }; bool isValid(int newX, int newY, int rows, int cols, const vector<vector<int>>& maze, vector<vector<bool>>& visited) { return newX >= 0 && newX < rows && newY >= 0 && newY < cols && !maze[newX][newY] && !visited[newX][newY]; } int bfs(const vector<vector<int>>& maze, pair<int, int> start, pair<int, int> end) { int rows = maze.size(); int cols = maze[0].size(); queue<Node> q; vector<vector<bool>> visited(rows, vector<bool>(cols, false)); q.push(Node{start.first, start.second, 0}); visited[start.first][start.second] = true; while (!q.empty()) { Node current = q.front(); q.pop(); if (current.x == end.first && current.y == end.second) { return current.step; // 返回最短路径步数 } for (int i = 0; i < 4; ++i) { int newX = current.x + dx[i]; int newY = current.y + dy[i]; if (isValid(newX, newY, rows, cols, maze, visited)) { visited[newX][newY] = true; q.push(Node{newX, newY, current.step + 1}); } } } return -1; // 如果无法到达终点,则返回 -1 表示无解 } int main() { int n, m; // 迷宫大小 n * m cin >> n >> m; vector<vector<int>> maze(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> maze[i][j]; } } pair<int, int> start, end; cin >> start.first >> start.second >> end.first >> end.second; int result = bfs(maze, start, end); cout << result << endl; return 0; } ``` --- #### 关键点解析 1. **初始化** 输入读取完成后,需明确起始坐标 `(sx, sy)` 和终止坐标 `(ex, ey)`,并将它们作为参数传递给 BFS 函数。 2. **边界条件判断** 在每次尝试新位置之前,都需要验证其合法性,即是否越界、是否可以及是否已被访问。 3. **时间复杂度** 假设迷宫规模为 \(n \times m\),则 BFS 的时间复杂度为 \(O(n \cdot m)\),空间复杂度同样为 \(O(n \cdot m)\)[^1]。 --- #### 输出说明 如果存在从起点到终点的有效路径,则输出最小步数;否则输出 `-1` 表示不存在这样的路径。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值