BFS+优先队列 处理走迷宫类问题

cqy终于算出了坑爹的密码!!!进入墓穴,发现墓穴是一个n*m的矩阵。由于墓穴极度缺氧,cqy必须用最短的时间找到唯一的宝藏。墓穴里险象环生,除了有各种各样的陷阱,还有野怪出没!!!cqy每前进一步需要1分钟的时间,如果遇到野怪,还要多花费1分钟来打倒野怪。当然cqy不会往陷阱走,那就有去无回了...如果cqy、宝藏、陷阱、野怪的位置全部已知,那么cqy最短要用多长时间找到宝藏?

输入
有多组测试数据。对于每组测试数据,首先有两个整数n和m(1<n,m<50),接下来有n行,每行有m个字符,A表示宝藏,C表示cqy,E表示正常的道路,X表示陷阱,T表示野怪。

输出

对于每组测试数据,在一行内输出找到宝藏所需的最短时间,如果无法找到宝藏,则输出“Game Over!”


输入:                                                                                                              输出:

4 4 2 3 5

ATTE CXE Game Over!

EXTE TXA

ETCE

EXEX

青杨大神出的BFS+优先队列神题, 刚开始看题,一看就知道是以前做过的BFS水题,但是怎么想也想不起来如何处理 每次遇到野怪times+1如何处理;后来看到了prority_queue

还有vis[x][y]不仅仅可以为0或者1还可以记录到达(x,y)点处的最小时间,后果断AC;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 59
char map[N][N];
int vis[N][N];
int n,m,tox,toy,beginx,beginy;
int x0[] = {0,0,-1,1};
int y0[] = {1,-1,0,0};
struct Node{
    int times,x,y;
    Node(){}
    Node(int _x,int _y,int _t){
        x =_x; y = _y; times = _t;
    }
    friend bool operator <(Node n1,Node n2){
        return n1.times > n2.times;
    }
};
priority_queue<Node> q;
void BFS(int &ans) {
    while(!q.empty()) q.pop();
    q.push(Node(beginx,beginy,0));
    map[beginx][beginy] = 'E';
    vis[beginx][beginy] = 0;
    while(!q.empty()){
        Node cur = q.top(); q.pop();
        if(cur.x==tox&&cur.y==toy){
            ans = cur.times;
            return;
        }
        for(int i = 0;i<4;i++){
            Node tem = Node(cur.x+x0[i],cur.y+y0[i],cur.times+1);
            if(tem.x>n||tem.x<=0||tem.y<0||tem.y>=n) continue;
            if(map[tem.x][tem.y]=='X') continue;
            else if(map[tem.x][tem.y]=='E'&&vis[tem.x][tem.y]>tem.times){
                vis[tem.x][tem.y] = tem.times;
                q.push(tem);
            }
            else if(map[tem.x][tem.y]=='T'&&vis[tem.x][tem.y]>tem.times+1){
                vis[tem.x][tem.y] = tem.times + 1;
                tem.times += 1;
                q.push(tem);
            }
            else if(map[tem.x][tem.y]=='A'){
                vis[tem.x][tem.y] = tem.times;
                q.push(tem);
            }
        }
    }
    ans = -1;
}
void init(){
    memset(vis,0x0f0f0f0f,sizeof(vis));
}
void getData(){
    for(int i = 1;i<=n;i++){
        scanf("%s",map[i]);
        for(int j = 0;j<m;j++){
            if(map[i][j]=='A'){
                tox = i;
                toy = j;
            }
            else if(map[i][j]=='C'){
                beginx = i;
                beginy = j;
            }
        }
    }
}
int main(){
    //freopen("Test.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        init();
        getData();
        int ans = -1;
        BFS(ans);
        if(ans==-1) printf("Game Over!\n");
        else        printf("%d\n",ans);
    }
    return 0;
}





以下是Java中使用BFS算法解决走迷宫问题的示例代码: ```java import java.util.*; public class MazeBFS { static int[][] maze = {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}; static int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static void main(String[] args) { int startX = 1; int startY = 1; int endX = 5; int endY = 6; Queue<Node> queue = new LinkedList<>(); boolean[][] visited = new boolean[maze.length][maze[0].length]; Node startNode = new Node(startX, startY, 0); queue.offer(startNode); visited[startX][startY] = true; while (!queue.isEmpty()) { Node node = queue.poll(); int x = node.x; int y = node.y; int step = node.step; if (x == endX && y == endY) { System.out.println(step); break; } for (int i = 0; i < 4; i++) { int nextX = x + direction[i][0]; int nextY = y + direction[i][1]; if (nextX >= 0 && nextX < maze.length && nextY >= 0 && nextY < maze[0].length && maze[nextX][nextY] == 0 && !visited[nextX][nextY]) { queue.offer(new Node(nextX, nextY, step + 1)); visited[nextX][nextY] = true; } } } } static class Node { int x; int y; int step; public Node(int x, int y, int step) { this.x = x; this.y = y; this.step = step; } } } ``` 在上述代码中,我们使用了一个Node来表示迷宫中的每个节点,其中包含了x和y坐标以及当前步数step。我们使用一个队列来进行BFS搜索,并且使用一个二维布尔数组visited来记录每个节点是否已经被访问过。 我们从起点开始,将其入队列中,并将其visited值设为true。然后不断从队列中取出节点进行搜索,直到队列为空或者找到终点为止。对于每个节点,我们尝试向四个方向扩展,如果扩展出来的节点在迷宫范围内、不是墙、没有被访问过,就将其入队列中,并将visited值设为true。 最终,如果搜索到了终点,就输出步数并停止搜索。如果队列为空仍然没有找到终点,说明终点不可达。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值