noj1010 基本的迷宫问题

本文介绍了一个迷宫逃脱问题的解决方案,使用广度优先搜索(BFS)算法寻找从指定起点到迷宫出口的路径,并通过回溯记录路径。该算法适用于方形迷宫,迷宫由0(通道)和1(墙壁)组成。

The Inescapable Maze

Time Limit: 1000ms    Memory Limit: 65536K
Total Submissions: 362    Accepted: 138

Description

Jack and his friends were trapped in a maze of amusement park.  Please help them to find the right way to escape.

This square maze is reperented as 0,1 text.  0 is a way,  1 is a wall.  

The size of maze is N. The entrance at (1,1), the exit at (N,N).  You can only escape from exit.

Input

The first line contain three integers, N, x, y. (N<=80)

N is the size of maze.  x, y is the Jack and friends's current location.

Your task is to find the way from (x,y) to (N,N)

 

Output

If you find the way out,  print 'Found' then following with the maze model with '#' to show the way.

If you could't find the way, print 'Not Found'.

 

Sample Input

6 5 2
0 0 1 1 1 1
1 0 0 0 0 1
1 1 1 0 1 1
1 0 0 0 1 1
1 0 1 0 1 1
1 0 1 0 0 0

Sample Output

Found
0 0 1 1 1 1
1 0 0 # # 1
1 1 1 # 1 1
1 0 0 # 1 1
1 0 1 # 1 1
1 0 1 # # #
#include <iostream>
#include<stdio.h>
#include<queue>
#include<stack>
#include<string.h>
using namespace std;

int t;
int m,n;

struct point
{
    int x;
    int y;
}buf;

int vis[100][100];
int mapp[1000][100];
int keep[100][100];
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};
queue<point> q;
stack<point >s;
int flag;

void  bfs()
{
    flag=0;
    vis[buf.x][buf.y]=1;
    while(!q.empty())
    {
        for(int i=0;i<4;i++)
        {
            buf.x=q.front().x+dir[i][0];
            buf.y=q.front().y+dir[i][1];
            if(1<=buf.x&&buf.x<=t&&1<=buf.y&&buf.y<=t&& !vis[buf.x][buf.y] &&mapp[buf.x][buf.y]==0)
            {
                vis[buf.x][buf.y]=1;
                keep[buf.x][buf.y]=i;
                q.push(buf);
                if(buf.x==t&&buf.y==t)
                {
                    flag=1;
                    break;
                }
            }
        }
        q.pop();
        if(buf.x==t&&buf.y==t)
        {
            flag=1;
            break;
        }
    }
}

int main()
{
    memset(vis,0,sizeof(vis));
    scanf("%d%d%d",&t,&m,&n);
    for(int i=1;i<=t;i++)
        for(int j=1;j<=t;j++)
            scanf("%d",&mapp[i][j]);
        buf.x=n;
        buf.y=m;
        q.push(buf);
        bfs();
        if(flag==0)
            printf("Not Found\n");
        else
        {
            printf("Found\n");
        point nbuf={t,t};
        s.push(nbuf);
        while(buf.x!=n||buf.y!=m)
        {
            nbuf.x=buf.x-dir[keep[buf.x][buf.y]][0];
            nbuf.y=buf.y-dir[keep[buf.x][buf.y]][1];
            s.push(nbuf);
            buf.x=nbuf.x;
            buf.y=nbuf.y;
        }
        while(!s.empty())
        {
            mapp[s.top().x][s.top().y]=2;
            s.pop();
        }
       for(int i=1;i<=t;i++)
        for(int j=1;j<=t;j++)
        {
            if(mapp[i][j]==0||mapp[i][j]==1)
            printf("%d",mapp[i][j]);
            else
                printf("#");
            if(j!=t)
                printf(" ");
           if(j==t&&i!=t)
            printf("\n");
        }
        }
        return 0;
}

### NOJ 平台电子老鼠闯迷宫解题思路 针对NOJ 1042中的电子老鼠闯迷宫问题,该问题是典型的最短路径寻找问题。由于地图固定且只有上下左右四个方向可以移动,广度优先搜索(BFS)成为解决此类问题的理想选择[^1]。 BFS是一种逐层向外扩展的遍历方式,非常适合用于无权图上的最短路径计算。具体应用在此处意味着从起点出发,逐步探索每一个可达节点直到找到目标位置为止。为了防止重复访问已经到达过的地点以及记录当前所走步数,通常还需要额外的数据结构来辅助完成这一过程。 #### 数据准备阶段 输入数据包含了迷宫尺寸、起始坐标(S),终止坐标(T),以及由一系列整数组成的地图布局信息。其中,“1”代表不可通行区域(障碍物/墙壁),而“0”则标记可自由通过的道路。因此,在实际编码之前,先解析这些原始数据并构建出适合处理的形式是非常必要的。 ```python from collections import deque def parse_input(): n = int(input()) # 迷宫大小 sx, sy, ex, ey = map(int, input().split()) maze = [] for _ in range(n): row = list(map(int, input().split())) maze.append(row) return n, (sx-1, sy-1), (ex-1, ey-1), maze ``` #### 主体逻辑设计 基于上述分析,下面给出完整的Python版本解决方案: ```python def bfs(maze, start, end): directions = [(0,-1),(0,1),(-1,0),(1,0)] # 定义四种可能的方向 queue = deque([(start[0], start[1], 0)]) # 初始化队列,加入初始状态(x,y,distance) visited = set() # 记录已访问的位置集合 while queue: x, y, dist = queue.popleft() if (x, y) == end: # 如果当前位置等于终点,则返回距离 return dist for dx, dy in directions: # 尝试向四周扩散 nx, ny = x + dx, y + dy if not (0 <= nx < len(maze)) or \ not (0 <= ny < len(maze)): # 越界判断 continue elif maze[nx][ny] != 0 or ((nx, ny) in visited): # 阻碍物或已被访问过的情况 continue else: visited.add((nx, ny)) queue.append((nx, ny, dist+1)) if __name__ == "__main__": N, S, E, M = parse_input() result = bfs(M, S, E) print(result) ``` 这段程序首先定义了一个`bfs()`函数用来执行具体的广搜操作;接着实现了简单的命令行交互接口读取测试案例,并调用此方法获取最终结果输出。注意这里假设所有输入均合法有效,即不存在非法字符等问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值