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 ...
随机推荐
- sql语句判断默认值为getdate()的约束是否存在
TFlowCreateTask--表名 AddDate--字段名 if not exists( select d.name from syscolumns a join sysobjects b on ...
- 使用 AngularJS 和 Electron 构建桌面应用
GitHub 的 Electron 框架(以前叫做 Atom Shell)允许你使用 HTML, CSS 和 JavaScript 编写跨平台的桌面应用.它是io.js 运行时的衍生,专注于桌面应用而 ...
- iOS版本更新的App提交审核流程
App的版本更新估计是在所难免的了!更新App和新的App发布有何不同了?今天我们一起来看看吧!在发布App的时候我们需要通过开发者帐号——(申请)——>发布证书(需要钥匙串对证书签名也叫加密( ...
- FastReport4.6 组件安装
要完整版不是官方版的试用版.下面包括有的文件 安装前请册除原有的FR控件. 1. "Tools|Environmet options..."中的"Library" ...
- json转类
JavaScriptSerializer js = new JavaScriptSerializer();T detaile = js.Deserialize<T>(json);
- 汽车驱动之家 http://bmw360.cn
汽车驱动之家 http://bmw360.cn
- 实验一 cmd命令的编写
一.目的和要求 1. 实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语言编程初步. 2.实验要求 编写类似于DOS,UNIX的命令行解释程序 (1)自行定义 ...
- Ubuntu 修改hosts
Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改这个就可以了,结果发现是需要修改hosts.修改完之后要重启网络.具体过程 ...
- onethinkp导入excel
/** * Excel导入函数 * @author crx349 */ if (!empty($_FILES)) { $config = array( 'maxSize' => 3145728, ...
- HttpServletRequest常用的方法
假设客户端请求的地址:http://localhost:8082/TestReq/MyServlet/?username=李雷&age=20 request.getRequestURL htt ...