Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
           也是暴力的思想吧!
 
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #include <set>
  10. #define Min(a ,b ) ((a)<(b)?(a):(b))
  11. using namespace std;
  12. const int Max_N= ;
  13. const int inf= ;
  14. int N , M ,K ;
  15. char str[Max_N][Max_N] ;
  16. int d[][]={ {,}, {,} ,{- ,} ,{ ,-} } ;
  17.  
  18. int cango(int x ,int y){
  19. return <=x&&x<=N&&<=y&&y<=M ;
  20. }
  21.  
  22. struct Node{
  23. int X ;
  24. int Y ;
  25. int step ;
  26. Node(){} ;
  27. Node(int x ,int y ,int s):X(x),Y(y),step(s){} ;
  28. friend bool operator <(const Node A ,const Node B){
  29. return A.step>B.step ;
  30. }
  31. } ;
  32.  
  33. int dist[Max_N] [Max_N] ;
  34.  
  35. void spfa(int x ,int y ){
  36. priority_queue<Node>que ;
  37. que.push(Node(x,y,)) ;
  38. for(int i= ; i<=N ; i++)
  39. for(int j= ; j<=M ; j++)
  40. dist[i][j]=inf ;
  41. dist[x][y]= ;
  42. while(!que.empty() ){
  43. Node now = que.top() ;
  44. que.pop() ;
  45. for(int i= ; i< ; i++){
  46. int x=now.X + d[i][] ;
  47. int y=now.Y + d[i][] ;
  48. if(!cango(x,y))
  49. continue ;
  50. if(str[x][y] != '.')
  51. continue ;
  52. if( now.step+<dist[x][y] ){
  53. dist[x][y] = now.step + ;
  54. que.push( Node(x,y,dist[x][y]) ) ;
  55. }
  56. }
  57. }
  58. }
  59.  
  60. struct Point {
  61. int X ;
  62. int Y ;
  63. } ;
  64.  
  65. Point p[] ;
  66. int grid[][] ;
  67. int sele[] ;
  68. bool use[] ;
  69. int ans ;
  70.  
  71. void dfs(int id){
  72. if(id>K){
  73. int sum = ;
  74. for(int i = ; i <= K ; i++)
  75. sum+=grid[ sele[i-] ][ sele[i] ] ;
  76. ans=Min(ans,sum) ;
  77. }
  78. for(int i = ; i <= K ; i++ ){
  79. if(use[i]){
  80. sele[id] = i ;
  81. use[i]= ;
  82. dfs(id+) ;
  83. use[i] = ;
  84. }
  85. }
  86. }
  87.  
  88. int main() {
  89. while(cin>>N>>M){
  90. if(N==&&M==)
  91. break ;
  92. for(int i= ; i<=N ; i++)
  93. scanf("%s",str[i]+) ;
  94. for(int i= ; i<=N ; i++)
  95. for(int j= ; j<=M ; j++){
  96. if(str[i][j]=='@'){
  97. p[].X = i ;
  98. p[].Y = j ;
  99. str[i][j] = '.' ;
  100. }
  101. }
  102. cin>>K ;
  103. for(int i = ; i<=K ; i++)
  104. scanf("%d%d",&p[i].X,&p[i].Y) ;
  105. for(int i = ; i <= K ; i++){
  106. spfa(p[i].X , p[i].Y ) ;
  107. for(int j = ; j <= K ; j++){
  108. grid[i][j] = dist[ p[j].X ][ p[j].Y ] ;
  109. }
  110. }
  111. memset(use,,sizeof(use)) ;
  112. sele[]= ;
  113. ans=inf ;
  114. dfs() ;
  115. if(ans==inf)
  116. puts("-1") ;
  117. else
  118. cout<<ans<<endl ;
  119. }
  120. return ;
  121. }

HDU 4771 Stealing Harry Potter's Precious的更多相关文章

  1. HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  2. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  3. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

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

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

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

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

  6. hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)

    题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...

  7. hdu 4771 Stealing Harry Potter&#39;s Precious

    题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...

  8. hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0

    Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...

  9. Stealing Harry Potter's Precious BFS+DFS

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

随机推荐

  1. [转]谈谈select, iocp, epoll,kqueue及各种网络I/O复用机制

    参考原文:再谈select, iocp, epoll,kqueue及各种I/O复用机制 一.I/O模型概述 介绍几种常见的I/O模型及其区别,如下: blocking I/O nonblocking ...

  2. js键盘事件全面控制详解【转】

    js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型 ...

  3. get utc+8 当时时间

    /// <summary> /// get utc+8 当时时间 /// </summary> /// <returns></returns> publ ...

  4. 网页颜色RGB记法和16进制记法转化方法

    A=>10,B=>11,C=>12,D=>13,E=>14,F=>15 看一个例子: 254,112,85 255/16 等于 15 余 14 那么它对应的应该是F ...

  5. android学习笔记46——File存储

    File存储--IO操作文件 openFileOutput.openFileInput Context提供了如下两个方法来打开本应用程序的数据文件夹里面的文件IO流. 1.FileInputStrea ...

  6. Tomcat在eclipse中起动成功,主页却打不开

    症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的tomc ...

  7. ultraedit15.00.0.1046注册码

      ultraedit注册码,版本:15.00.0.1043·········· 用户名 MAYBELOVE 注册码 LFKKM-KIMMX-OSFEB-PMISO-ELILS-IIIHO-KKHLR ...

  8. 2. hdfs

    一.Hdfs的shell 所有hadoop的fs的shell均用uri路径作为参数 uri格式:schema://authority/path.hdfs的schema是hdfs.其中,schema和a ...

  9. .Net下实现可扩展的编程方法简述

    IoC控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则用来解决系统耦合问题. 控制反转还有一个名字叫做依赖注入(DI:Dependency Inje ...

  10. 张恭庆编《泛函分析讲义》第二章第5节 共轭空间 $\bullet$ 弱收敛 $\bullet$ 自反空间习题解答

    1.$\ell^p\ (1\leq p<\infty)$ 的对偶 求证: $\dps{\sex{\ell^p}^*=\ell^q\quad\sex{1\leq p<\infty,\ \fr ...