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个点是否走过

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 105
#define M 15
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int ans;
int n,m;
int k;
char s[N][N];
int a[N][N];
int mi[][N][N];
int dirx[]={,,-,};
int diry[]={,,,-}; typedef struct
{
int x;
int y;
int step;
int st;
}PP; PP start; void ini()
{
ans=-;
int i,j,p;
int x,y;
for(i=;i<n;i++){
scanf("%s",s[i]);
}
memset(a,,sizeof(a));
scanf("%d",&k);
for(i=;i<n;i++){
for(j=;j<m;j++){
for(p=;p<(<<k);p++){
mi[p][i][j]=;
}
if(s[i][j]=='@'){
start.x=i;
start.y=j;
start.st=;
start.step=;
}
if(s[i][j]=='#'){
a[i][j]=-;
}
}
}
for(i=;i<k;i++){
scanf("%d%d",&x,&y);
a[x-][y-]=(<<i);
if(x-==start.x && y-==start.y){
start.st+=(<<i);
mi[start.st][start.x][start.y]=;
}
}
if(start.st==){
mi[][start.x][start.y]=;
} // for(i=0;i<n;i++){
// for(j=0;j<m;j++){
// printf(" %d",a[i][j]);
// }printf("\n");
// }
} int ok(int i,int j)
{
if(i>= && i<n && j>= && j<m && a[i][j]!=-){
return ;
}
return ;
} void solve()
{
int i;
PP now,nx;
queue<PP> q;
q.push(start);
while(q.size()>=)
{ now=q.front();
// printf(" i=%d j=%d st=%d step=%d\n",now.x,now.y,now.st,now.step);
q.pop();
if(now.st== ((<<k)-) ){
ans=now.step;return;
} for(i=;i<;i++){
nx=now;
nx.step++;
nx.x=now.x+dirx[i];
nx.y=now.y+diry[i];
if(ok(nx.x,nx.y)==) continue;
if( (now.st & a[nx.x][nx.y])==){
nx.st=(now.st ^ a[nx.x][nx.y]);
// printf(" %d %d %d\n",now.st, a[nx.x][nx.y],nx.st);
//q.push(nx);
// mi[nx.st][nx.x][nx.y]=min(nx.step,mi[nx.st][nx.x][nx.y]);
}
//else{
if( nx.step< mi[nx.st][nx.x][nx.y]){
q.push(nx);
mi[nx.st][nx.x][nx.y]=nx.step;
}
// }
}
}
} void out()
{
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n== && m== ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}

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. UVA 10735 Euler Circuit (最大流)

    题意:求混合图的欧拉路径. 一句话总结:网络流,最主要在于建图,此题是将出度则是和流量联系在了一起,用最大流来调整边的指向. 分析: 这题的困难之处在于无向边只能用一次,相当于一个方向未定的有向边. ...

  2. 用dfs遍历联通块(优化)

    一.题目(CF 598D) 输入一个n x m的字符矩阵,求从某个空点出发,能碰到多少面墙壁,总共询问k次.(3 ≤m,n ≤1000,1 ≤ k ≤ min(nm,100 000)) 二.解题思路 ...

  3. Mac如何让调整窗口大小更简单

    在使用Mac的时候,你能把鼠标的光标悬停在任何程序的边缘,当光标自动变成箭头样式后,按住鼠标左键你将能随意拖动来改变程序窗口的大小.但是,这里有个问题,我们有时候很难控制把鼠标光标移动在正确的窗口边缘 ...

  4. Bootstrap历练实例:表单控件状态(焦点)

    输入框焦点 当输入框 input 接收到 :focus 时,输入框的轮廓会被移除,同时应用 box-shadow. <!DOCTYPE html><html><head& ...

  5. Ukulele 原来你也在这里

  6. [LUOGU] P2759 奇怪的函数

    题目描述 使得 x^x x x 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^xx x 达到 n 位数字的最小正整数 x 输入输出样 ...

  7. python中with用法及原理

    资源的管理在程序的设计上是一个很常见的问题,例如管理档案,开启的网络socket与各种锁定(locks)等.最主要的问题在于我们必须确保这些开启的资源在使用之后能够关闭(或释放),若忘记关闭这些资源, ...

  8. SVN如何避免冲突

    在团队开发时,必然会用到代码版本控制工具,比如SVN. 但是多人共同维护同一份代码,当对同一文件进行增删时,就可能造成冲突,如何尽可能避免冲突相当重要. 首先,每次,新建任何文档,都会修改项目文件,所 ...

  9. adb shell am/pm 常用命令详解与使用

    一.adb shell am 使用此命令可以从cmd控制台启动 activity, services:发送 broadcast等等 1.am start <packageName/.classN ...

  10. Python第三方库之openpyxl(6)

    Python第三方库之openpyxl(6) 折线图 折线图允许在固定轴上绘制数据,它们类似于散列图,主要的区别在于,在折线图中,每个数据序列都是根据相同的值绘制的,不同的轴可以用于辅助轴,与条形图类 ...