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 ...
随机推荐
- Oracle 数据库简单操作
现在大型企业一般都用Oracle数据库,Oracle数据库在一般采用expdp,impdp 导出导入数据,但是在操作中经常会遇到一些问题.下面来浅析这些问题. 1. 导出数据 一般导出数据的时候需要建 ...
- AngularJS学习笔记
一.初识AngularJS:1.Angularjs通过创建实时模板来代替视图,而不是将数据合并进模板后更新DOM,任何一个独立视图组件中的值都是动态替换的. 二.数据绑定和第一个AngularJS W ...
- PHPCMS开启伪静态和织梦开启伪静态的优缺点比较
PHPCMS和织梦CMS都是国内比较出名的PHP语言的CMS程序系统,他们拥有比较完善的网站内容管理功能,也比较注重网站优化方面的功能,深受很多网站建设者的喜爱. 这两套系统,都有启用伪静态的功能,在 ...
- javascript运算符与表达式
表达式 表达式是关键字.运算符.变量以及文字的组合,用来生成字符串.数字或对象.一个表达式可以完成计算.处理字符.调用函数.或者验证数据等操作. 表达式的值是表达式运算的结果,常量表达式的值就是常量本 ...
- 用Curl测试POST
POST请求 http://172.16.102.208:8089/wiapi/score?leaderboard_id=1&score=36&app_key=66 目的1:通过脚本发 ...
- linux学习 命令ll后字段的解释(转)
原文链接:http://jz20080153.blog.sohu.com/161554070.html ls -l 列表信息详解 我们平时用ls -l 命令查看一个目录下的文件和子目录的详悉信息时,会 ...
- Xcode8 上架前属性列表添加权限
需要注意的是,权限的string,也就是提示语句也要设置,都设置好之后,一次就上传成功,可以添加构建版本了. <key>NSBluetoothPeripheralUsageDescr ...
- vs2012 error c4996: This function or variable may be unsafe
编译lua源码时,使用vs2012,遇到如下错误. 1>------ 已启动生成: 项目: 20130925, 配置: Debug Win32 ------ 1> stdafx.cpp ...
- android editText 监听事件
在软键盘中注意 在监听的 edittext中 使用android:imeOptions属性的时候,一定要对EditText设置 android:inputType 或者 设置 android:sing ...
- http_load
wget http://acme.com/software/http_load/http_load-12mar2006.tar.gz