Key Task bfs()找到最优值
作者:互联网
Problem Description
The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places.
The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.
The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.
You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.
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.
One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.
*........X
1 3
*#X
3 20
####################
#XY.gBr.*.Rb.G.GG.y#
####################
0 0
The poor student is trapped!
Escape possible in 45 steps. *************************************************************************************************************************** bfs(),找最优值 用了map给初始状态编号 ***************************************************************************************************************************
The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.
The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.
You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.
Input The 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
- more than one exit,
- no exit at all,
- more doors and/or keys of the same color, and
- keys without corresponding doors and vice versa.
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.
Output For 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(),找最优值 用了map给初始状态编号 ***************************************************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdio> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 bool vis[20][110][110]; 10 char save[110][110]; 11 int n,m,i,j,k; 12 int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; 13 struct node 14 { 15 int x,y,step,ss; 16 }sr,er,s; 17 map<char,int>myp1,myp2; 18 bool Is_can(int x,int y)//是否能走 19 { 20 if(x<1||x>n||y<1||y>m||save[x][y]=='#') 21 return false; 22 return true; 23 } 24 25 void bfs()//找到每种钥匙可以开相同颜色的门 26 { 27 memset(vis,false,sizeof(vis)); 28 s.step=0,s.ss=0; 29 queue<node>myq; 30 31 myq.push(s); 32 vis[0][s.x][s.y]=true; 33 while(!myq.empty()) 34 { 35 node tmp=myq.front(); 36 myq.pop(); 37 38 int xx,yy; 39 40 for(int i=0;i<4;i++) 41 { 42 xx=tmp.x+dir[i][0],yy=tmp.y+dir[i][1]; 43 if(!Is_can(xx,yy)) 44 continue; 45 if(save[xx][yy]=='X') 46 { 47 printf("Escape possible in %d steps.\n",tmp.step+1); 48 return ; 49 } 50 51 int tt=tmp.ss; 52 if(myp1.find(save[xx][yy])!=myp1.end()) //得到一把钥匙 53 { 54 tt=tt|(1<<myp1[save[xx][yy]]); 55 if(vis[tt][xx][yy]) //该状态之前已被访问 56 continue; 57 } 58 else if(myp2.find(save[xx][yy])!=myp2.end()) 59 { 60 if(!(tt&(1<<myp2[save[xx][yy]]))) //没有钥匙 61 continue; 62 } 63 if(vis[tt][xx][yy])//已经被访问 64 continue; 65 vis[tt][xx][yy]=true; 66 node t; 67 t.x=xx,t.y=yy,t.ss=tt,t.step=tmp.step+1; 68 myq.push(t); 69 } 70 } 71 printf("The poor student is trapped!\n"); 72 return ; 73 } 74 int main() 75 { 76 myp1['b']=0,myp1['y']=1,myp1['r']=2,myp1['g']=3;//map预处理 77 myp2['B']=0,myp2['Y']=1,myp2['R']=2,myp2['G']=3; 78 while(scanf("%d%d",&n,&m)&&(m+n)) 79 { 80 //memset(vis,false,sizeof(vis)); 81 for(i=1;i<=n;i++) 82 { 83 scanf("%s",save[i]+1); 84 for(j=1;j<=m;j++) 85 if(save[i][j]=='*') 86 s.x=i,s.y=j; 87 88 } 89 //cout<<"s.x:::s.y::: "<<s.x<<" "<<s.y<<endl; 90 bfs(); 91 } 92 return 0; 93 }View Code
转载于:https://www.cnblogs.com/sdau--codeants/p/3425881.html
标签:map,Task,doors,int,possible,bfs,step,Key,include 来源: https://blog.csdn.net/weixin_34034670/article/details/94300168