博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Key Task bfs()找到最优值
阅读量:6953 次
发布时间:2019-06-27

本文共 3854 字,大约阅读时间需要 12 分钟。

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.
 

 

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
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
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 node14 {15 int x,y,step,ss;16 }sr,er,s;17 map
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
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<
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3425881.html

你可能感兴趣的文章
java多线程之:创建开启一个线程的开销
查看>>
【福利将至】iPhone用户可用Siri发微信了
查看>>
分布式锁的三种实现方式
查看>>
逆向路由器固件之敏感信息泄露 Part2
查看>>
WebSocket与消息推送
查看>>
apache做转发
查看>>
一条SQL的改写
查看>>
常用的慢查询日志分析工具
查看>>
MySQL单表模拟锁的几个场景
查看>>
因子分解机模型简介
查看>>
SAP LSMW 物料主数据导入毛重净重放大1000倍问题之对策
查看>>
外嫁美的被指违约 东芝创维合作或重谈
查看>>
HCI的全面升温可能导致软件定义型传统阵列遭遇搁浅
查看>>
特斯拉和SolarCity下月召开特别股东大会 表决合并事宜
查看>>
Denyhosts shell script
查看>>
高清摄像机镜头的质量和价格分析
查看>>
中国移动计划牵头推动5G传输国际标准立项
查看>>
苹果补上了可被未授权收集传感器数据的iPhone漏洞
查看>>
《UNIXLinux程序设计教程》一2.6 文件结束和错误指示器
查看>>
Q3全球太阳能企业融资规模达30亿美元 环增76%
查看>>