
P1825 [USACO11OPEN] Corn Maze S - 洛谷 | 计算机科学教育新生态
定义状态空间 结构体 精简代码
遇到多种情况判断不要全写进check里面
分开写
传送门是大写字母 A-z
其acll码值 是 65-90
我们将传送门代表的字母-65 就可以将其值映射到 0-26
从而存下相应的传送门坐标
完整代码
#include<bits/stdc++.h>
 #include<queue>
 using namespace std;
 const int N =310;
 char a[N][N];
 int dj[30][2];//dj数组 作为第一个传送门的坐标数组 
 int djj[30][2];//djj数组 作为第一个传送门的坐标数组 
 bool djzt[30];
 #define check(x,y)(x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#')
 int n,m;
 int dir[4][2]={ {-1,0},{0,-1},{0,1},{1,0}};
 struct point{ 
     int x,y,step;
 };
 queue<point> q;
void bfs(int dx , int  dy,int step){ 
     point p;
    while(!q.empty()){ 
         p = q.front();
         q.pop();
         for(int i=0;i<4;i++){ 
         
             int nx = p.x + dir[i][0];
             int ny = p.y + dir[i][1];
                 
             if(check(nx,ny))
             { 
                 if(a[nx][ny] == '.'){ 
                     q.push({nx,ny,p.step+1});
                     a[nx][ny] = '#';
                     continue ;
                 }
                 if(a[nx][ny] == '='){ 
                     cout<< p.step +1;
                     return ;
                 }
                 if(nx == dj[a[nx][ny]-65][0] && ny == dj[a[nx][ny]-65][1]){ 
                     q.push({djj[a[nx][ny]-65][0] , djj[a[nx][ny]-65][1],p.step+1});
                 }else{ 
                     q.push({dj[a[nx][ny]-65][0] , dj[a[nx][ny]-65][1],p.step+1});
                 }
                 
                 
             }
         }
     }
 }
 int main(){ 
     cin>>n>>m;
     int sx,sy,gx,gy;
     for(int i= 1;i <= n;i++){ 
         for(int j = 1;j <= m;j++){ 
             cin>>a[i][j];
             if(a[i][j] == '@'){ 
                 sx = i; sy = j;
                 q.push({sx,sy,0}); 
                 a[i][j] = '#';
             }
             if(a[i][j] == '='){ 
                 gx = i; gy = j;
             }
             if('A' <= a[i][j] && a[i][j] <= 'Z'){ 
                 if(djzt[a[i][j]-65]==false){//如果该传送门没遇到过 记录其坐标 到dj数组 
                     dj[a[i][j]-65][0] = i;
                     dj[a[i][j]-65][1] = j;
                     djzt[a[i][j]-65] = true;
                 }else{//否则遇到过了 将其坐标记录到对应djj数组 
                     djj[a[i][j]-65][0] = i;
                     djj[a[i][j]-65][1] = j;
                 }
             }
         }
     }
     bfs(sx,sy,0);
    return 0;
 }