标签:res com rop following define in place map bin rri
InputThe input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:
Note that it is allowed to have
You may assume that the marker of your position (“*”) will appear exactly once in every map.
There is one blank line after each map. The input is terminated by two zeros in place of the map size.OutputFor each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead.
One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.Sample Input
1 10 *........X 1 3 *#X 3 20 #################### #XY.gBr.*.Rb.G.GG.y# #################### 0 0
Sample Output
Escape possible in 9 steps. The poor student is trapped! Escape possible in 45 steps.
题解:BFS+状态压缩;我们可以记录每一种状态是否出现过来搜索,因为只有4种钥匙,故用二进制表示每一种钥匙的状态;
然后就是普通的BFS,在处理时加上钥匙的处理,以及加判是否为门,如果为门的话,判断上个位置时是否已有该门对应的钥匙,
如有,则该点入队,否则跳过;如果遇到出口,就输出步数,如到最后队列为空时都没有出去,则输出被困在里面;
参考代码为:
#include<bits/stdc++.h> using namespace std; const int N=105; int n,m,sx,sy,ex,ey; char g[N][N]; bool vis[N][N][16]; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; struct node{ int x,y,key,step; }; int judged(char ch) { if(ch==‘B‘)return 1<<0; if(ch==‘Y‘)return 1<<1; if(ch==‘R‘)return 1<<2; if(ch==‘G‘)return 1<<3; } int judgek(char ch) { if(ch==‘b‘)return 1<<0; if(ch==‘y‘)return 1<<1; if(ch==‘r‘)return 1<<2; if(ch==‘g‘)return 1<<3; return 0; } void solve() { memset(vis,0,sizeof(vis)); queue<node>q; node u,v; u.x=sx;u.y=sy;u.key=0;u.step=0;vis[u.x][u.y][u.key]=1; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); if(g[u.x][u.y]==‘X‘) { printf("Escape possible in %d steps.\n",u.step); return; } for(int i=0;i<4;i++) { v.x=u.x+dir[i][0];v.y=u.y+dir[i][1];v.step=u.step+1;v.key=u.key|judgek(g[v.x][v.y]); if(v.x<1||v.x>n || v.y<1||v.y>m)continue; if(g[v.x][v.y]==‘#‘ || vis[v.x][v.y][v.key])continue; if(g[v.x][v.y]==‘B‘||g[v.x][v.y]==‘Y‘||g[v.x][v.y]==‘R‘||g[v.x][v.y]==‘G‘) { if(!(u.key&judged(g[v.x][v.y]))) continue; } vis[v.x][v.y][v.key]=1; q.push(v); } } printf("The poor student is trapped!\n"); } int main() { while(scanf("%d%d",&n,&m)&&n+m) { for(int i=1;i<=n;i++) scanf("%s",g[i]+1); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(g[i][j]==‘*‘) { sx=i;sy=j;g[sx][sy]=‘.‘; break; break; } } } solve(); } return 0; }
标签:res com rop following define in place map bin rri
原文地址:https://www.cnblogs.com/songorz/p/9409847.html