Stealing Harry Potter's Precious

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

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
 
Recommend
We have carefully selected several similar problems for you:  5057 5055 5054 5053 5052 

题意和题解转自:http://blog.csdn.net/qq574857122/article/details/14649319

题意:

给定n*m的地图

#为墙 @为起点

下面K个坐标

问:遍历K个给定坐标,需要的最小步数

思路:

因为K 最大只有4

状压 当前是否走过某点

用二进制 的 i 位 0、1表示 第i个点是否走过

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<map>
  9. #include<string>
  10.  
  11. #define N 105
  12. #define M 15
  13. #define mod 10000007
  14. //#define p 10000007
  15. #define mod2 100000000
  16. #define ll long long
  17. #define LL long long
  18. #define maxi(a,b) (a)>(b)? (a) : (b)
  19. #define mini(a,b) (a)<(b)? (a) : (b)
  20.  
  21. using namespace std;
  22.  
  23. int ans;
  24. int n,m;
  25. int k;
  26. char s[N][N];
  27. int a[N][N];
  28. int mi[][N][N];
  29. int dirx[]={,,-,};
  30. int diry[]={,,,-};
  31.  
  32. typedef struct
  33. {
  34. int x;
  35. int y;
  36. int step;
  37. int st;
  38. }PP;
  39.  
  40. PP start;
  41.  
  42. void ini()
  43. {
  44. ans=-;
  45. int i,j,p;
  46. int x,y;
  47. for(i=;i<n;i++){
  48. scanf("%s",s[i]);
  49. }
  50. memset(a,,sizeof(a));
  51. scanf("%d",&k);
  52. for(i=;i<n;i++){
  53. for(j=;j<m;j++){
  54. for(p=;p<(<<k);p++){
  55. mi[p][i][j]=;
  56. }
  57. if(s[i][j]=='@'){
  58. start.x=i;
  59. start.y=j;
  60. start.st=;
  61. start.step=;
  62. }
  63. if(s[i][j]=='#'){
  64. a[i][j]=-;
  65. }
  66. }
  67. }
  68. for(i=;i<k;i++){
  69. scanf("%d%d",&x,&y);
  70. a[x-][y-]=(<<i);
  71. if(x-==start.x && y-==start.y){
  72. start.st+=(<<i);
  73. mi[start.st][start.x][start.y]=;
  74. }
  75. }
  76. if(start.st==){
  77. mi[][start.x][start.y]=;
  78. }
  79.  
  80. // for(i=0;i<n;i++){
  81. // for(j=0;j<m;j++){
  82. // printf(" %d",a[i][j]);
  83. // }printf("\n");
  84. // }
  85. }
  86.  
  87. int ok(int i,int j)
  88. {
  89. if(i>= && i<n && j>= && j<m && a[i][j]!=-){
  90. return ;
  91. }
  92. return ;
  93. }
  94.  
  95. void solve()
  96. {
  97. int i;
  98. PP now,nx;
  99. queue<PP> q;
  100. q.push(start);
  101. while(q.size()>=)
  102. {
  103.  
  104. now=q.front();
  105. // printf(" i=%d j=%d st=%d step=%d\n",now.x,now.y,now.st,now.step);
  106. q.pop();
  107. if(now.st== ((<<k)-) ){
  108. ans=now.step;return;
  109. }
  110.  
  111. for(i=;i<;i++){
  112. nx=now;
  113. nx.step++;
  114. nx.x=now.x+dirx[i];
  115. nx.y=now.y+diry[i];
  116. if(ok(nx.x,nx.y)==) continue;
  117. if( (now.st & a[nx.x][nx.y])==){
  118. nx.st=(now.st ^ a[nx.x][nx.y]);
  119. // printf(" %d %d %d\n",now.st, a[nx.x][nx.y],nx.st);
  120. //q.push(nx);
  121. // mi[nx.st][nx.x][nx.y]=min(nx.step,mi[nx.st][nx.x][nx.y]);
  122. }
  123. //else{
  124. if( nx.step< mi[nx.st][nx.x][nx.y]){
  125. q.push(nx);
  126. mi[nx.st][nx.x][nx.y]=nx.step;
  127. }
  128. // }
  129. }
  130. }
  131. }
  132.  
  133. void out()
  134. {
  135. printf("%d\n",ans);
  136. }
  137.  
  138. int main()
  139. {
  140. //freopen("data.in","r",stdin);
  141. //freopen("data.out","w",stdout);
  142. //scanf("%d",&T);
  143. // for(int ccnt=1;ccnt<=T;ccnt++)
  144. // while(T--)
  145. while(scanf("%d%d",&n,&m)!=EOF)
  146. {
  147. if(n== && m== ) break;
  148. //printf("Case %d: ",ccnt);
  149. ini();
  150. solve();
  151. out();
  152. }
  153.  
  154. return ;
  155. }

HDU 4771 BFS + 状压的更多相关文章

  1. hdu 2209 bfs+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=2209 不知为啥有种直觉.会出状压+搜索的题,刷几道先 简单的BFS.状压表示牌的状态, //#pragma co ...

  2. hdu 5025 bfs+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙 ...

  3. hdu 1429 bfs+状压

    题意:这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始 Ignatius被关在(sx,sy)的位置,离开地牢的门 ...

  4. C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压

    C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...

  5. hdu 1044(bfs+状压)

    非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  6. hdu 4771 Stealing Harry Potter's Precious (BFS+状压)

    题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...

  7. hdu 5094 Maze (BFS+状压)

    题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi    含义是(x1,y1)与(x2,y2)之间有gi ...

  8. 孤岛营救问题 (BFS+状压)

    https://loj.ac/problem/6121 BFS + 状压 写过就好想,注意细节debug #include <bits/stdc++.h> #define read rea ...

  9. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

随机推荐

  1. iview 验证 trigger: 'blur,change', 同时加两个,省的每次还想input 还是 select

    iview 验证 trigger: 'blur,change', 同时加两个,省的每次还想input 还是 select dataRuleValidate: { name: [{ required: ...

  2. 关于 QObject 类

    1.QObject类   简述 QObject类是所有Qt对象的基类. QObject是Qt对象模型的核心. 该模型的核心特征是称为信号和槽的对象通信机制. 您可以使用connect()将信号连接到槽 ...

  3. python爬虫---实现项目(三) Selenium分析美团美食

    上一期博客,我本来想爬取美团美食的,但是由于请求头太复杂,没有破解开其中的几个参数,所以放弃,这次我们来用selenium来模式浏览器抓取数据,我们先来简单看一下流程: 1,利用selenium驱动浏 ...

  4. grep-sed命令用法:

    用户切换 su username:非登录式切换 su - username:登录式切换 su -l username:登录式切换 su username -c COMMAND   echo -n   ...

  5. [九省联考2018] IIIDX 线段树+贪心

    题目: 给出 k 和 n 个数,构造一个序列使得 d[i]>=d[i/k] ,并且字典序最大. 分析: 听说,当年省选的时候,这道题挡住了大批的高手,看上去十分简单,实际上那道弯段时间内是转不过 ...

  6. MySQLfailover错误一则

    由于公司现有主库要转移到新的主库上,所以,我打算利用MySQLfailover工具的故障转移. 1.开发把程序账号转移到新主库上 2.停止现有主库,使之进行故障转移,转移期间会自动锁表,保持数据一致性 ...

  7. 如何用纯 CSS 创作一盘传统蚊香

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/BVpvMz 可交互视频教 ...

  8. H5移动端触摸事件:touchstart、touchend、touchmove

    第一部分代码事例: <html><head> <meta charset="utf-8"> <style> #main,#main1 ...

  9. PERL学习之模式匹配

    一.简介   模式指在字符串中寻找的特定序列的字符,由反斜线包含:/def/即模式def.其用法如结合函数split将字符串用某模式分成多个单词:@array = split(/ /, $line); ...

  10. 【css】【动画】【转发】旋转动画

    <!DOCTYPE HTML> <html> <head>     <meta charset="utf-8">     <s ...