2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II BFS

题面

题意:N*M的网格图里,有起点S,终点T,然后有'.'表示一般房间,'#'表示毒气房间,进入毒气房间要消耗一个氧气瓶,而且要多停留一分钟,'B'表示放氧气瓶的房间,每次进入可以获得一个氧气瓶,最多只能带5个,'P'表示有加速器的房间,进入可以获得一个,可以拥有无限个,然后使用一个可以让你用的时间减一。

题解:对于P房间,也就是到达时不花费时间,

        对于#房间,也就是进入前必须要有氧气瓶,消耗的时间为2

        对于B房间,氧气瓶数量num=min(num+1,5)

        因为有氧气瓶,所以每一个节点状态除了位置x,y,还有当前氧气瓶x,当前花费的时间time

        这样的节点扔进队列就行了,按time小的顺序拿出来继续,(所以用了个优先队列)第一次到达终点的那个time就是最小的time

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 bool vis[105][105][6];
  4 int n,m;
  5 struct Node 
  6 {
  7     int x,y,ox,step;
  8     Node() {}
  9     Node(int _x,int _y,int _ox,int _step) 
 10     {
 11         x=_x;
 12         y=_y;
 13         ox=_ox;
 14         step=_step;
 15     }
 16     bool operator<(const Node& e) const {return step>e.step;}
 17 };
 18 char ch[105][105];
 19 priority_queue<Node> Q;
 20 int p[4]={0,0,1,-1};
 21 int q[4]={1,-1,0,0};
 22 int main() 
 23 {
 24     while (scanf("%d%d",&n,&m)) 
 25     {
 26         if (n==0&&m==0) break;
 27         for (int i=0;i<n;i++) scanf("%s",ch[i]);
 28         int sx,sy,ex,ey;
 29         for (int i=0;i<n;i++)
 30             for (int j=0;j<m;j++)
 31             {
 32                 if(ch[i][j]=='S') 
 33                 {
 34                     sx=i;sy=j;
 35                 }else 
 36                 if(ch[i][j]=='T') 
 37                 {
 38                     ex=i;ey=j;
 39                 }
 40             }
 41         while(!Q.empty()) Q.pop();
 42         Node start(sx,sy,0,0);
 43         memset(vis,0,sizeof(vis));
 44         vis[sx][sy][0]=1;
 45         Q.push(start);
 46         bool ok=false;
 47         while(!Q.empty()) 
 48         {
 49             Node node=Q.top();
 50             Q.pop();
 51             if (node.x==ex&&node.y==ey) 
 52             {
 53                 ok=true;
 54                 printf("%d\n",node.step);
 55                 break;
 56             }
 57             for (int i=0;i<=3;i++)
 58             {
 59                 int xx=node.x+p[i],yy=node.y+q[i];
 60                 if (xx<0 || xx>=n || yy<0 || yy>=m) continue;
 61                 int step=node.step;
 62                 if(ch[xx][yy]=='B') 
 63                 {
 64                     int _ox=min(5,node.ox+1);
 65                     if (vis[xx][yy][_ox]) continue;
 66                     vis[xx][yy][_ox]=1;
 67                     Node temp(xx,yy,_ox,step+1);
 68                     Q.push(temp);
 69                 }else 
 70                 if(ch[xx][yy]=='#') 
 71                 {
 72                     int _ox=node.ox;
 73                     if (!_ox) continue;
 74                     _ox--;
 75                     if(vis[xx][yy][_ox]) continue;
 76                     vis[xx][yy][_ox]=1;
 77                     Node temp(xx,yy,_ox,step+2);
 78                     Q.push(temp);
 79                 }else 
 80                 if(ch[xx][yy]=='P') 
 81                 {
 82                     int _ox=node.ox;
 83                     if(vis[xx][yy][_ox]) continue;
 84                     vis[xx][yy][_ox]=1;
 85                     Node temp(xx,yy,_ox,step);
 86                     Q.push(temp);
 87                 }else 
 88                 {
 89                     int _ox=node.ox;
 90                     if(vis[xx][yy][_ox]) continue;
 91                     vis[xx][yy][_ox]=1;
 92                     Node temp(xx,yy,_ox,step+1);
 93                     Q.push(temp);
 94                 }
 95             }
 96         }
 97         if (!ok) puts("-1");
 98     }
 99     return 0;
100 }

猜你喜欢

转载自www.cnblogs.com/qywhy/p/9695261.html