题目链接: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. SAE部署Java应用

    链接地址:http://blog.csdn.net/shuixin536/article/details/9031335 SAE为开发者提供了非常宽松的开发环境,你甚至不用做任何特别定制就能将各种Ja ...

  2. 如何隐藏DLL中,导出函数的名称?

    一.引言 很多时候,我们写了一个Dll,不希望别人通过DLL查看工具,看到我们的导出函数名称.可以通过以下步骤实现: 1. 在def函数中做如下定义: LIBRARY EXPORTS HideFunc ...

  3. 漏网之鱼--HTML&CSS

    一.HTML <meta>标签使用该标签描述网页的具体摘要信息,包括文档内容类型,字符编码信息,搜索关键字,网站提供的功能和服务的详细描述等.<meta>标签描述的内容并不显示 ...

  4. nginx下配置二级域名指向子目录

    今天终于把nginx的二级域名配置搞定了,哎之前在测试服务器上弄过一次,不过那个是在本地解析的hosts,把ip指向到域名上就ok,再在nginx.conf里改了下配置就好了,用同样的方法改了正式服务 ...

  5. python--内建函数(1)

    Python中,按照对象是否可变,将类型分类为: 不可变类型:对象的内容不能够改变(not mutable),这些类型中主要有数值类型(整数,浮点数,复数),字符串类型,元组等 可变类型:对象的内容能 ...

  6. C和C++安全编码读书笔记1

    (1)type safety Another characteristic of C that is worth mentioning is the lack of type safety. Type ...

  7. jQuery.form 中的 ajaxForm() 和 ajaxSubmit()

    官方例子  http://malsup.com/jquery/form/#ajaxForm官方API   http://malsup.com/jquery/form/#api中文API   http: ...

  8. 链接分析算法之:SALSA算法

    链接分析算法之:SALSA算法 SALSA算法的初衷希望能够结合PageRank和HITS算法两者的主要特点,既可以利用HITS算法与查询相关的特点,也可以采纳PageRank的“随机游走模型”,这是 ...

  9. CUDA纹理绑定

    纹理绑定的一般步骤: size_t fea_pitch; texture<unsigned char, 2> features2D; cudaMallocPitch((void**)(&a ...

  10. String类的一些方法

    String 类有以下方法: startsWith(String prefix) boolean java.lang.String.startsWith(String prefix) Tests if ...