HDU 4771 Stealing Harry Potter's Precious

来源:互联网 发布:mac长的破折号怎么打 编辑:程序博客网 时间:2024/06/05 00:58

Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2260    Accepted Submission(s): 1060


Problem Description
  Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:



  Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
 

Input
  There are several test cases.
  In each test cases:
  The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
  Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
  The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
  In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
  The input ends with N = 0 and M = 0
 

Output
  For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
 

Sample Input
2 3##@#.#12 24 4#@##....####....22 12 40 0
 

Sample Output
-15
 

Source
2013 Asia Hangzhou Regional Contest
题目大意:有多组测试数据,输入整个地图的大小n 和m,然后输入整个地图的情况。接下来输入K,表示有K个宝藏点,接下来输入K个宝藏点的坐标, 地图中的@表示起点,#表示墙壁,.表示能行走的。请输出从起点出发拿完所有宝藏所需要的最少的步数。
解题注意点:
1 用BFS搜索,S表示状态,
2 1<<k - 1表示所有的宝藏都被拿走的状态。
3  | 运算符效果是:1|0 = 1,0|1 = 1,1|1=1.
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>using namespace std;char g[200][200];int a[200][200];int st1,st2;int k;pair<int,int>pp[10];int dp[200][200][100];int di[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};int n,m;struct NODE{int x,y;int s;NODE(int _x,int _y,int _s){x = _x;y = _y;s = _s;}};int bfs(){queue<NODE>q;int s = 0;for(int i=0;i<k;i++){if(pp[i].first == st1&&pp[i].second == st2){s |= (1<<i);}}dp[st1][st2][s] = 0;q.push(NODE(st1,st2,s));while(!q.empty()){NODE tt = q.front();//tt = q.front();q.pop();if(tt.s == ((1<<k)-1))return dp[tt.x][tt.y][tt.s];for(int i=0;i<4;i++){s = tt.s;int nx = tt.x+di[i][0];int ny = tt.y+di[i][1];if(nx<0||nx>=n||ny<0||ny>=m)continue;if(a[nx][ny]==-1)continue;for(int j=0;j<k;j++){if(nx == pp[j].first&&ny == pp[j].second)s|= (1<<j);}if(dp[nx][ny][s]!=-1)continue;dp[nx][ny][s] = dp[tt.x][tt.y][tt.s]+1;q.push(NODE(nx,ny,s));}}return -1;}int main(){while(scanf("%d%d",&n,&m)==2){if(n==0&&m==0)break;memset(g,0,sizeof(g));memset(a,0,sizeof(a));memset(dp,-1,sizeof(dp));for(int i=0;i<n;i++)scanf("%s",&g[i]);for(int i = 0;i<n;i++){for(int j = 0;j<m;j++){if(g[i][j]=='#')a[i][j]=-1;if(g[i][j]=='@'){st1 = i;st2 = j;}}}cin >> k;int c,d;for(int i=0;i<k;i++){cin >> c >> d;c--;d--;pp[i] = make_pair(c,d);}printf("%d\n",bfs());}}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 抖音停止运行了怎么办? 华为手机右下角有个小人怎么办 华为手机程序停止运行怎么办 华为手机不支持软件搬家怎么办 红米手机没有任务键怎么办 进网许可丢了怎么办 华为稳私空间被稳蔵怎么办 华为手机安装不了软件怎么办 华为手机自动安装软件怎么办 手机在实体店买贵了怎么办 日本买的kindle坏了怎么办 华为麦芒4屏幕失灵怎么办 华为麦芒6屏幕失灵怎么办 手机为什么总显示内存不足怎么办 小米云服务满了怎么办 魅蓝s6声音小怎么办 华为畅享7s丢失怎么办 华为耳机孔坏了怎么办 苹果手机耳机插孔坏了怎么办 荣耀8听筒声音小怎么办 荣耀8听筒音量小怎么办 华为荣耀6声音小怎么办 华为荣耀v8通话声音小怎么办 华为p20没有耳机孔怎么办 华为荣耀手机耳机声音小怎么办 小米5x显示耳机怎么办 手机进水听音乐人声小怎么办 华为荣耀v8手机音量小怎么办 手机充电孔松了怎么办 华为荣耀7卡顿怎么办 华为手机话筒没声音怎么办 苹果x耳机进水了怎么办 苹果6p进水了怎么办 华为手机声音变耳机模式怎么办 手机设置成耳机模式怎么办 opop耳机一个没有声音怎么办 oppo手机上显示耳机模式怎么办 微信显示耳机模式怎么办 微信变成耳机模式怎么办 5s变成耳机模式怎么办 华为手机一直是耳机模式怎么办