文章

74

粉丝

0

获赞

94

访问

8.2k

头像
迷宫 (bfs模板题)题解:
P1563 天津大学/南开大学机试题
发布于2025年8月15日 16:03
阅读数 117

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100 + 5;
char mp[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
struct node{
    int x, y;
    int step;
};

int bfs(int sx, int sy){
    memset(vis, 0, sizeof(vis));
    queue<node> q;
    q.push(node{sx, sy, 0});
    vis[sx][sy] = 1;
    int ans = -1;

    while(!q.empty()){
        node now = q.front();
        q.pop();

        if(mp[now.x][now.y] == 'E'){
            ans = now.step;
            break;
        }

        for(int i = 0; i < 4; i ++){
            int nextx = now.x + dir[i][0];
            int nexty = now.y + dir[i][1];
            if((mp[nextx][nexty] == '*' || mp[nextx][nexty] == 'E') && vis[nextx][nexty] == 0){
                q.push(node{nextx, nexty, now.step + 1});
                vis[nextx][nexty] = 1;
            }
        }
    }

    return ans;
}

int main(){

	int h, w;
	while(cin >> h &...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发