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 K (Java/Others)
Total Submission(s): 126 Accepted Submission(s): 63
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.
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
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
5
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,状态压缩)的更多相关文章
- HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)
Gems Fight! Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)T ...
- HDU 4771 Stealing Harry Potter's Precious dfs+bfs
Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...
- HDU 4771 Stealing Harry Potter's Precious
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- CSS-3 渐变的使用
CSS3 渐变(gradients)可以让您在两个或多个指定的颜色之间显示平稳的过渡. 以前,您必须使用图像来实现这些效果.但是,通过使用 CSS3 渐变(gradients),您可以减少下载的事件和 ...
- pt-table-checksum 3.0.4检测不出主从差异数据
群里好几位同学问 pt-table-checksum 3.0.4, 主从两个表数据是不一致,为啥检测不出来?前段时间自己也测试过,只是没整理成随笔^_- 一.基本环境 VMware10.0+CentO ...
- 【Udacity并行计算课程笔记】- lesson 1 The GPU Programming Model
一.传统的提高计算速度的方法 faster clocks (设置更快的时钟) more work over per clock cycle(每个时钟周期做更多的工作) more processors( ...
- mysql_存储过程_后一行减去前一行
DELIMITER $$ /*统计单个用户登录次数的存过 @times_count int 返回值 @i 记录行号的变量 初始值为0 @temp 记录时间差的变量 @total 记录登录次数的变量 初 ...
- 在pycharm和tensorflow环境下运行nmt
目的是在pycharm中调试nmt代码,主要做了如下工作: 配置pycharm编译环境 在File->Settings->Project->Project Interpreter 设 ...
- 利用 devcon.exe实现自动安装驱动(转)
http://blog.csdn.net/u012814201/article/details/44919125 工作的原因打算通过devcon.exe实现自动打包的功能,由于之前一直在Linux那个 ...
- Oracle 数据库和监听器开机自启动两种实现方法
数据库和监听器开机自启动 编辑oratab文件: 修改:orcl:/u01/app/oracle/product/11.2.0/db_1:N orcl:/u01/app/or ...
- win7(x64)安装scrapy框架
Scrapy(官网http://scrapy.org/)是Python开发网络爬虫,一个极好的开源工具.本次安装Scrapy确实不易啊.居然花了2天多时间,需要的支持包比较多,这些支持包相互之间的依赖 ...
- spring-web.xml 模板
ssm模板 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- Coursera台大机器学习技法课程笔记14-Radial Basis Function Network
将Radial Basis Function与Network相结合.实际上衡量两个点的相似性:距离越近,值越大. 将神经元换为与距离有关的函数,就是RBF Network: 可以用kernel和RBF ...