hdu 4771, 枚举
2013-11-09 22:59
190 查看
Stealing Harry Potter's Precious
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 131 Accepted Submission(s): 68
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
[align=left]Sample Input[/align]
2 3
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
[align=left]Sample Output[/align]
-1
5
[align=left]Source[/align]
2013 Asia Hangzhou Regional Contest
[align=left]Recommend[/align]
We have carefully selected several similar problems for you: 4780 4779 4778 4777 4776
参考了http://blog.csdn.net/u011329714/article/details/14648829
枚举到k个点的次序,比如有三个点,a1, a2, a3, 源点为a0, 则可以(a0 -> a2) -> (a2 -> a1) -> (a1 -> a3), 也就是2, 1, 3这个排列, 所以求全排列并bfs即可。
#include<algorithm> #include<cstdio> #include<iostream> #include<queue> using namespace std; #define LL long long #define MAXN 111 char g[MAXN][MAXN]; int n, m, k; int sx, sy; struct node{ int stp, x, y; }target[5]; #define cint const int cint dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1}; void reset(){ //初始化,相当于memeset(vis, false, ...) for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(g[i][j] == 'v') g[i][j]='.'; } int bfs(int *p){ queue<node> q; reset(); q.push((node){0, sx, sy}); g[sx][sy]='v'; //visited for(int i=0; !q.empty(); ){ node tnode = q.front(); q.pop(); for(int j=0; j<4; j++){ int nx = tnode.x + dx[j], ny = tnode.y + dy[j], stp = tnode.stp + 1; if(nx<0 || ny<0 || nx>=n || ny>=m || g[nx][ny]=='#' || g[nx][ny]=='v') continue; bool f = nx==target[p[i]].x && ny==target[p[i]].y; //走到当前想到的地方 if(f){ //到了,初始化。 while(!q.empty()) q.pop(); reset(); if(++i == k) return stp; } q.push((node){stp, nx, ny}); g[nx][ny]='v'; if(f) break; } } return -1; //目地不可达。 } int main(){ // freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); while(scanf(" %d %d", &n, &m)==2 && n){ int i, j, p[5], cas=1; for(i=0; i<n; i++){ scanf(" %s", g[i]); for(j=0; j<m; j++) if(g[i][j] == '@') sx=i, sy=j; } scanf(" %d", &k); for(i=0; i<k; i++){ scanf(" %d %d", &target[i].x, &target[i].y); target[i].x--; target[i].y--; cas = cas * (i + 1); // k! p[i] = i; } int ans = -1; while(cas--){ int tmp = bfs(p); if(tmp>-1 && (tmp < ans || ans==-1)) ans = tmp; //合法的最小值 next_permutation(p, p+k); } printf("%d\n", ans); } return 0; }
每次贪心可达的最近目的点的WA代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<set> #include<queue> #include<string> #include<cmath> #include<fstream> #include<iomanip> using namespace std; #define LL long long #define MAXN 111 int n, m, k, tofind; char g[MAXN][MAXN]; struct node{ int stp, x, y; }; #define cint const int cint dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1}; bool cango(int x, int y){ if(x<0 || x>=n || y<0 || y>=m || g[x][y]=='#' || g[x][y]=='v') return false; return true; } void reset(){ for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(g[i][j] == 'v') g[i][j] = '.'; } int bfs(){ queue<node> q; int i, j; for(i=0; i<n; i++){ for(j=0; j<m; j++) if(g[i][j] == '@') break; if(j<m) break; } q.push((node){0, i, j}); g[i][j]='v'; while(!q.empty()){ node t = q.front(); q.pop(); for(i=0; i<4; i++){ int nx = dx[i] + t.x, ny = dy[i] + t.y, stp = t.stp + 1; if(cango(nx, ny)){ bool f = (g[nx][ny] == 'q'); if(f){ if(++tofind == k) return stp; while(!q.empty()) q.pop(); reset(); } q.push((node){stp, nx, ny}); g[nx][ny]='v'; if(f) break; } } } return -1; } int main(){ // freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); while(scanf(" %d %d", &n, &m)==2 && n){ int i; for(i=0; i<n; i++) scanf(" %s", g[i]); scanf(" %d", &k); for(tofind = i = 0; i<k; i++){ int x, y; scanf(" %d %d", &x, &y); x--; y--; if(g[x][y] == '@' || g[x][y]=='q') tofind++; else g[x][y] = 'q'; } int ans =(tofind==k ? 0 : bfs()); printf("%d\n", ans); } return 0; }
相关文章推荐
- java操作mysql的小程序
- MySQL数据库的优化(下)MySQL数据库的高可用架构方案
- Documentation_powerpc_cpu_features.txt
- 多线程实现
- createsamples.cpp全过程解析
- android里面EditTex多行输入及输入置顶问题
- 分享一个网址 上面的 python 库比官网还全........
- Chrome商店Crx离线安装包下载
- 分享一个网址 上面的 python 库比官网还全........
- 谈谈我对AIDL的使用方法总结
- Windows 8.1 SkyDrive 挂起 解决参考 推荐
- 单网卡多IP
- 堆排序
- 软件授权协议有什么作用,例如GPL、Apache License、CDDL、EPL这些协议有什么区别?
- Qualcomm和MTK两个android平台比较
- hdu-1236-排名
- 解决send-MailMessage SMTP验证发送时的弹出窗口
- 二项队列
- Java特性
- Android学习的一些问题