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

思路:

bfs或者dfs用二维数组标记的话,走过的路径就不能再走回去了,
而题目显然是找到宝藏之后还要找另一个宝藏,会重复走之前走过的路径

开三维数组标记,第三维记录当前找到宝藏的状态(状态压缩),这样就能重复走了

这题也可以bfs几次求出任意两个宝藏之间的最短路,然后全排列枚举取宝藏的顺序取答案最小值

code:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#include<queue>
const int maxm=105;
char g[maxm][maxm];
int mark[maxm][maxm][maxm];
int n,m,k;
struct Node{int x,y,s;
}st,K[maxm];
queue<Node>q;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int bfs(){//    for(int i=0;i<k;i++){//实测起点不判也能a
//        if(st.x==K[i].x&&st.y==K[i].y){//            st.s|=(1<<i);
//        }
//    }while(!q.empty())q.pop();q.push(st);memset(mark,-1,sizeof mark);mark[st.x][st.y][st.s]=0;while(!q.empty()){Node x=q.front();q.pop();for(int i=0;i<4;i++){int xx=x.x+dir[i][0];int yy=x.y+dir[i][1];int ss=x.s;if(xx<0||xx>=n||yy<0||yy>=m)continue;//越界if(g[xx][yy]==-1)continue;//墙for(int j=0;j<k;j++){if(xx==K[j].x&&yy==K[j].y){ss|=(1<<j);}}if(mark[xx][yy][ss]!=-1)continue;//如果来过了mark[xx][yy][ss]=mark[x.x][x.y][x.s]+1;if(ss==(1<<k)-1){//如果全部收集到了return mark[xx][yy][ss];}q.push({xx,yy,ss});}}return -1;
}
signed main(){while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0)break;for(int i=0;i<n;i++){scanf("%s",g[i]);for(int j=0;j<m;j++){if(g[i][j]=='@'){st.x=i,st.y=j;st.s=0;}else if(g[i][j]=='#'){g[i][j]=-1;}}}scanf("%d",&k);for(int i=0;i<k;i++){scanf("%d%d",&K[i].x,&K[i].y);K[i].x--;K[i].y--;}printf("%d\n",bfs());}return 0;
}

hdu4771 Stealing Harry Potter's Precious (状压+bfs)相关推荐

  1. codevs 2594 解药还是毒药(状压+bfs)

    题目描述 Description Smart研制出对付各种症状的解药,可是他一个不小心,每种药都小小地配错了一点原料,所以这些药都有可能在治愈某些病症的同时又使人患上某些别的病症(你可能会问那-那是解 ...

  2. 【日常学习】【状压BFS】codevs2594 解药还是毒药题解

    题目描述 Description Smart研制出对付各种症状的解药,可是他一个不小心,每种药都小小地配错了一点原料,所以这些药都有可能在治愈某些病症的同时又使人患上某些别的病症(你可能会问那-那是解 ...

  3. [HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理

    Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 32MB Input 第一行是一个正整数n(n< ...

  4. 小红的rpg游戏 (状压 bfs

    添加链接描述 因为怪物数量小于等于10 所以可以枚举每个怪物是否选择通过 使用状压枚举的方式从当前位开始bfs 选择dist最小的可能性 #include<bits/stdc++.h> u ...

  5. 【ICPC-212】hdu 4771 Stealing Harry Potter's Precious

    点击打开链接 题意:题目给定一个n*m的地图,地图有一个起点标记为'@',还有'#'表示不能够走的,'.'表示可以走.给定k个点,问从起点开始把这k个点走过去的最小步数. 思路:题目k的最大为4,那么 ...

  6. Stealing Harry Potter‘s Precious HDU - 4771

    题意:给一个n*m的矩阵,小偷从@出发可以走".",墙是"#"不可走,之后又K个珠宝,并且给你这K个珠宝的坐标,问小偷最少要走多少步可以吧珠宝偷完,如果不能偷完 ...

  7. jzoj4016-圈地为王【状压,bfs,几何】

    正题 题目链接:https://jzoj.net/senior/#contest/show/3011/1 题目大意 n∗mn*mn∗m的格子,格子之间有道路,对于每个iii就走过最短的回路使得 圈住i ...

  8. bzoj 3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(状压+BFS)

    3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 94  Solve ...

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

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

最新文章

  1. php-函数小知识点
  2. 独舞风雪夜 跟我学mvc系列
  3. 【时间管理】从零开始GTD——GTD原则
  4. redis watch使用场景_[Redis] 常用类型及应用场景
  5. 23种设计模式[5]:原型模式
  6. kotlin 类和对象_Kotlin程序| 类和对象的示例(带有学生数据)
  7. 【pyhon】nvshens图片批量下载爬虫1.01
  8. javaweb(02) JavaScript基础知识
  9. 【java面试题】equals()方法和==的比较区别?
  10. php 面向对象编程(class)之从入门到崩溃 基础篇
  11. 五边形组合包络图matlab,华中赛基于遗传算法的钢构件排料问题.docx
  12. 1.kafka面试题--ISR相关
  13. excel----分组后统计
  14. 网站三级域名是什么样?
  15. 终于搞懂python通过twain模块控制扫描仪了
  16. raspberry pi 用树莓派来听落网电台
  17. 计算机网络fmd是什么意思,不用就亏大了!酷炫又不用花钱的Win10“黑科技”
  18. 串口通信数据位长度对传输数据的影响
  19. python UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 0: invalid start byte
  20. 百度地图的定位与操作 zrj

热门文章

  1. 微信开发流程总结(基于微信平台)
  2. 拼多多微信页面h5 html,拼多多(7pdd)微信跳转h5页面打开app跳转任意url关注技术weixin://dl/business/?ticket...
  3. win10重装系统后连不上公司服务器,Win10重装系统后网络连接不了,重装win10系统后不能上网解决方法...
  4. 2023你冲不冲,冲冲冲冲~~
  5. “字符串的展开”【题解】
  6. 2021.05.29【NOIP提高B组】模拟 总结
  7. 【微商】我和99%的人观点不同
  8. python和c 情侣网名_带符号的qq情侣网名 好听的情侣网名大全
  9. mac 下搭建paly framework体验(环境搭建)
  10. 网络分流器-TCP报文重组和会话规则-网络分流器