Stealing Harry Potter's Precious

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

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

bfs就可以了。

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-9 13:26:38
File Name :E:\2013ACM\专题强化训练\区域赛\2013杭州\1002.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int n,m;
char g[][];
int a[][]; int sx,sy;
int k; int dp[][][];
int px[],py[]; struct Node
{
int x,y;
int s;
Node(int _x = ,int _y = ,int _s = )
{
x = _x;
y = _y;
s = _s;
}
};
int move[][] = {{,},{,-},{,},{-,}};
int bfs()
{
queue<Node>q;
int s = ;
for(int i = ;i < k;i++)
if(sx == px[i] && sy == py[i])
s = s|(<<i);
q.push(Node(sx,sy,s));
memset(dp,-,sizeof(dp));
dp[sx][sy][s] = ;
while(!q.empty())
{
Node tmp = q.front();
q.pop();
if(tmp.s == ((<<k) - ))
{
return dp[tmp.x][tmp.y][tmp.s];
}
for(int i = ;i < ;i++)
{
int nx = tmp.x + move[i][];
int ny = tmp.y + move[i][];
int s = tmp.s;
if(nx < || nx >= n || ny < || ny >= m)continue;
if(a[nx][ny] == -)continue;
for(int j = ;j < k;j++)
if(nx == px[j] && ny == py[j])
{
s |= (<<j);
}
if(dp[nx][ny][s] != -)continue;
dp[nx][ny][s] = dp[tmp.x][tmp.y][tmp.s] + ;
q.push(Node(nx,ny,s));
}
} return -;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m) == )
{
if(n == && m == )break;
for(int i = ;i < n;i++)
scanf("%s",g[i]);
memset(a,-,sizeof(a));
for(int i = ;i < n;i++)
for(int j = ;j < m;j++)
{
if(g[i][j] == '@')
{
sx = i;
sy = j;
}
if(g[i][j] == '#')
a[i][j] = -;
}
scanf("%d",&k);
int x,y;
for(int i = ;i < k;i++)
{
scanf("%d%d",&x,&y);
x --;
y--;
px[i] = x;
py[i] = y;
}
printf("%d\n",bfs());
} return ;
}

HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)的更多相关文章

  1. HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)

    Gems Fight! Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)T ...

  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

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

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

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

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

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

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

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

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

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

  8. HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. C# 解决VS2008在win7找不到输入序列号的地方

    1.VS2008在Windows7 打开维护界面看不到可以输序列号的地方. 因为微软把他隐藏了. 2.我们可以借用工具把他显示出来 下载地址:http://www.zlsoft.com/techbbs ...

  2. Linux DRM KMS 驱动简介【转】

    转自:https://blog.csdn.net/yangkuanqaz85988/article/details/48689521 Whoops,上次写完<Linux DRM Graphic ...

  3. 使用pt-table-checksum校验MySQL主从复制【转】

    pt-table-checksum是一个基于MySQL数据库主从架构在线数据一致性校验工具.其工作原理在主库上运行, 通过对同步的表在主从段执行checksum, 从而判断数据是否一致.在校验完毕时, ...

  4. iptables-25个常用用法【转】

    本文介绍25个常用的iptables用法.如果你对iptables还不甚了解,可以参考上一篇iptables详细教程:基础.架构.清空规则.追加规则.应用实例,看完这篇文章,你就能明白iptables ...

  5. FFT(快速傅里叶变换)摘要

    怎么说呢...这次的代码码风有点... 从这篇博客开始,我终于会用LATEX了!撒花 注:以下涉及多项式的n均表示多项式的次数 FFT用途 很简单,多项式相乘. FFT原理 如果暴力乘,复杂度是$O( ...

  6. PyCharm 2018实现远程调试代码

    pycharm是一个非常强大的python开发工具,现在很多代码最终在线上跑的环境都是linux,而开发环境可能还是windows下开发,这就需要经常在linux上进行调试,或者在linux对代码进行 ...

  7. (转载)mysql:“Access denied for user 'root'@'localhost'”

    原文地址:http://www.linuxidc.com/Linux/2007-05/4338.htm # /etc/init.d/mysql stop# mysqld_safe --user=mys ...

  8. window批处理——bat文件的编写

    BAT 批处理脚本 教程   第一章 批处理基础第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD.这些命 ...

  9. 20165330《网络对抗技术》Exp0 Kali安装

    Kali安装 下载地址 Kali官网 VMware 安装步骤 参考在虚拟机中安装kali linux 安装Kali Linux的镜像和VMware 打开VMware,选择文件-新建虚拟机,出现对话框选 ...

  10. .NET Core 项目经验总结:项目结构介绍 (一)

    原文地址(个人博客):http://www.gitblogs.com/Blogs/Details?id=384b4249-15e4-41bf-9cf7-44a3e1e51885 作为一个.NET We ...