题目链接:hdu 4771 Stealing Harry Potter's Precious

题目大意:在一个N*M的银行里,贼的位置在’@‘,如今给出n个宝物的位置。如今贼要将全部的宝物拿到手。问最短的路径,不须要考虑离开。

解题思路:由于宝物最多才4个,加上贼的位置,枚举两两位置,用bfs求两点距离,假设存在两点间不能到达,那么肯定是不能取全然部的宝物。

然后枚举取宝物的顺序。维护ans最小。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std;
const int maxn = 100;
const int maxv = 10;
const int INF = 0x3f3f3f3f;
const int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} }; struct point {
int x, y;
point (int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
} p[maxv]; char g[maxn+5][maxn+5];
int n, N, M, v[maxn+5][maxn+5], d[maxv][maxv]; void init () { memset(g, 0, sizeof(g)); for (int i = 1; i <= N; i++) {
scanf("%s", g[i] + 1);
for (int j = 1; j <= M; j++)
if (g[i][j] == '@') {
p[0].x = i;
p[0].y = j;
}
} scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &p[i].x, &p[i].y);
} int bfs (point s, point e) { queue<point> que;
memset(v, -1, sizeof(v)); que.push(s);
v[s.x][s.y] = 0; while (!que.empty()) {
point u = que.front();
que.pop(); if (u.x == e.x && u.y == e.y)
return v[u.x][u.y]; for (int i = 0; i < 4; i++) {
int xi = u.x + dir[i][0];
int yi = u.y + dir[i][1]; if (xi > N || xi <= 0)
continue; if (yi > M || yi <= 0)
continue; if (v[xi][yi] != -1 || g[xi][yi] == '#')
continue; que.push(point(xi, yi));
v[xi][yi] = v[u.x][u.y] + 1;
}
}
return -1;
} bool judge () {
memset(d, 0, sizeof(d)); for (int i = 0; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
int dis = bfs(p[i], p[j]); if (dis == -1)
return false;
d[i][j] = d[j][i] = dis;
}
}
return true;
} int solve () {
int pos[maxv];
for (int i = 0; i <= n; i++)
pos[i] = i; int ans = INF;
do {
int sum = 0;
for (int i = 0; i < n; i++)
sum += d[pos[i]][pos[i+1]];
ans = min (ans, sum);
} while(next_permutation(pos + 1, pos + n + 1)); return ans;
} int main () {
while (scanf("%d%d", &N, &M) == 2 && N + M) {
init();
if (judge()) {
printf("%d\n", solve());
} else
printf("-1\n");
}
return 0;
}

hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)的更多相关文章

  1. hdu 4771 Stealing Harry Potter&#39;s Precious

    题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...

  2. HDU 4771 Stealing Harry Potter's Precious

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

  3. 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 ...

  4. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  5. hdu4771 Stealing Harry Potter&#39;s Precious

    注意--你可能会爆内存-- 假设一个直接爆搜索词-- 队列存储器元件被减少到-- #include<iostream> #include<map> #include<st ...

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

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

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

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

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

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

  9. 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 ...

随机推荐

  1. 五毛的cocos2d-x学习笔记04-触摸点

    Touch position是屏幕坐标系中的点,OpenGL position是cocos2d-x用到的OpenGL坐标系上的点坐标.所以就需要将touch的坐标转换成OpenGL坐标系中的点坐标. ...

  2. JBPM6工作台使用Mysql数据库

    前面已经安装好jbpm了.现在进行数据库的替换.很简单: 1,修改 build.properties 原: # default is H2 H2.version=1.3.168 db.name=h2 ...

  3. php随笔11-Thinkphp常用系统配置大全

    Thinkphp常用配置  CHECK_FILE_CASE -- windows环境下面的严格检查大小写. /* 项目设定 */     'APP_DEBUG'    => false, // ...

  4. git配合tortoiseGit的基础使用

    转载自:http://www.cnblogs.com/ssor/archive/2012/02/04/2337823.html 一定要自己写出来才能牢记,所以我来写一下 git确实比svn好用的多了, ...

  5. linux系统调用和库函数调用的区别(转)

    Linux下对文件操作有两种方式:系统调用(system call)和库函数调用(Library functions).可以参考<Linux程序设计>(英文原版为<Beginning ...

  6. Qt见解:Post 与 Get 的区别(Get将参数直接与网址整合为一个整体,而Post则将其拆为两个部分)

    第一次接触Qt的Http项目,今天看了一下Post和Get的基本使用方法,就开始尝试了.原先以为Post专门用于向服务器发送请求,然后接收服务器应答的: 而Get只是单纯从服务器获取资源,比如下载这个 ...

  7. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553   ...

  8. BZOJ 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机

    题目 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机 Time Limit: 5 Sec  Memory Limit: 64 MB Desc ...

  9. Lucene 实例教程(二)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui031 ...

  10. Android -- BroadCastReceiver的简单使用

    //首先新建一个继承自BroadcastReceiver的广播监听类 class StartActiviryReceiver extends BroadcastReceiver { public fi ...