文章

85

粉丝

0

获赞

442

访问

8.9k

头像
迷宫 题解:模版 该方法小心溢出
P1563 天津大学/南开大学机试题
发布于2026年3月6日 20:21
阅读数 83

#include <bits/stdc++.h>
using namespace std;
#define Max 100
char myMap[Max][Max];
int isVisited[Max][Max];
struct node {
    int x,y;
    int step;
};
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int bfs(node n,int h,int w) {
    queue<node> q;
    memset(isVisited,0,sizeof(isVisited));
    q.push(n);
    isVisited[n.x][n.y]=1;
    int ans=-1;
    while(!q.empty()) {
        node temp = q.front();
        q.pop();
        int nowx = temp.x,nowy = temp.y;
        if (myMap[nowx][nowy]=='E') {
            ans = temp.step;
            break;
        }
        for(int i=0;i<4;i++) {
            int nx = nowx + dir[i][0];
            int ny = nowy + dir[i][1];
            if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
            if(isVisited[nx][ny]==0&&((myMap[nx][ny]=='*')||myMap[nx][ny]=='E')) {
                isVisited[nx][ny]=1;
                q.push(node{nx,ny,temp.step+1});
            }
        }
...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发