HDU 4771
http://acm.hdu.edu.cn/showproblem.php?pid=4771
给一个地图,@是起点,给一些物品坐标,问取完所有物品的最小步数,不能取完输出-1
物品数最多只有四个,状态压缩一下bfs即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring> using namespace std; int n, m, k; char G[][];
int vis[][][<<]; struct p {
int x, y, key, step;
}; int dx[] = {, -, , };
int dy[] = {, , , -}; p now; int bfs() {
queue <p> q;
now.step = ;
q.push(now);
vis[now.x][now.y][] = ;
while(!q.empty()) {
p u = q.front();
q.pop();
for(int i = ; i < ; i++) {
int xx = u.x + dx[i];
int yy = u.y + dy[i];
if(xx < || xx >= n || yy < || yy >= m) continue;
if(G[xx][yy] == '#') continue;
p next;
if(G[xx][yy] >= '' && G[xx][yy] <= '') {
if(!vis[xx][yy][u.key]) {
vis[xx][yy][u.key|(<<(G[xx][yy]-''))] = ;
next.x = xx, next.y = yy, next.key = u.key|(<<(G[xx][yy]-'')), next.step = u.step + ;
if(next.key == (<<k)-) return next.step;
q.push(next);
}
}
else {
if(!vis[xx][yy][u.key]) {
vis[xx][yy][u.key] = ;
next.x = xx, next.y = yy, next.key = u.key, next.step = u.step + ;
q.push(next);
}
}
}
}
return -;
} int main() {
while(~scanf("%d%d", &n, &m)) {
if(!n && !m) break;
for(int i = ; i < n; i++)
scanf("%s", G[i]);
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(G[i][j] == '@')
now.x = i, now.y = j;
scanf("%d", &k);
int flag = ;
now.key = ;
memset(vis, , sizeof(vis));
for(int i = ; i < k; i++) {
int x, y;
scanf("%d%d", &x, &y);
if(G[x-][y-] == '#') flag = ;
else if(G[x-][y-] == '@') {
G[x-][y-] = i + '';
vis[x-][y-][<<i] = ;
}
else G[x-][y-] = i + '';
}
if(!flag) {
puts("-1");
continue;
}
printf("%d\n", bfs());
}
return ;
}
HDU 4771的更多相关文章
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- 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
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
题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
- 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 ...
- 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 ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- HDU 4771 BFS + 状压
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
随机推荐
- HTML 列表 <ol><ul><li><dl><dt><dd>
<ol>标签-有序列表 定义和用法: <ol>标签定义有序列表. HTML 与 XHTML 之间的差异 在 HTML 4.01 中,ol 元素的 "compact&q ...
- 使用dreamweaver去掉文本中的空格和换行
当我们从其他地方拷贝文本到网页,在html代码中会自动带有空格和换行,手动去掉很麻烦,今天试着用dreamweaver去了一下,方法如下: 1.点击Ctrl+F,打开“查找和替换”窗口 2‘见下图:
- jenkins安装配置-白痴教程
1. Maven安装 下载maven安装包,放置在usr/maven目录下:apache-maven-3.2.5-bin.tar.gz tar zxvf apache-maven-3.2.5-bin. ...
- MindManger学习技巧
ctrl+shift+f 字体颜色
- react-native执行 npm install cl.exe找不到 的问题
最近在学习react-native,昨天在尝试某个demo时,执行 npm instal, 总是遇到 cl.exe文件找不到,最开始以为Microsoft Visual C++ 2015 Redist ...
- IOS常用框架
IOS开发中有用的第三方库 #Objective-C中最受瞩目库 [链接](https://github.com/languages/Objective-C/most_watched) * [th ...
- 在EC2上搭建L2TP over IPSec VPN服务器
注意(:wq保存文件 putty登陆用户名为ec2-user) 安装与配置: 环境介绍: OS:CentOS 6.4 x86_64 Minimal 1. 修改 /etc/sysctl.conf,新增如 ...
- 博客迁移到GitCafe
博客以前是放在github上,但github在国内的访问速度确实有些慢,所以就想着换个git环境,本来想迁移到oschina中,后来看到以为博友介绍的迁移到gitcafe中,索性我也就照搬迁过来了. ...
- Xcode8 上架前属性列表添加权限
需要注意的是,权限的string,也就是提示语句也要设置,都设置好之后,一次就上传成功,可以添加构建版本了. <key>NSBluetoothPeripheralUsageDescr ...
- VS 2012 No exports were found that match the constraint 解决办法
VS 2012 No exports were found that match the constraint 解决办法 删除C:\Users\你的用户名\AppData\Local\Microsof ...