HDU 4771 Stealing Harry Potter's Precious
2015-08-03 15:50
351 查看
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 ##@ #.# 1 2 2 4 4 #@## .... #### .... 2 2 1 2 4 0 0
Sample Output
-1 5
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()); } }
相关文章推荐
- ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting B
- Android Do not do binder operation in destructor
- Basic
- [java_基础]内部类和静态类
- 元素定位方式 之-----CSS属性定位
- Redis Java连接操作(转)
- Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏
- CentOS6.5中桌面上的面板被误删后,如何正常恢复?
- Ruby中的字符串编写示例
- HDU-2544-最短路-模板
- LeetCode_3 Longest Substring Without Repeating Characters
- 【Android应用开发技术:媒体开发】拍照
- Algorithms—83.Remove Duplicates from Sorted List
- C# 获取URL参数 NameValueCollection
- JavaScript 与 Objective-C 通信
- ViewPager,java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
- xpath解析含有命名空间别名的xml文件的示例代码
- ORACLE 实用语句大集合
- Groovy操作POI
- AndroidTV/机顶盒 ListView获取焦点与点击事件问题处理方案