题目链接: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. java注解入门(含源码下载)

    注解(Annotation)是从jdk1.5开始增加的特性.学习注解能够读懂框架的代码:让编程更加简洁,代码更加清晰. 注解概念:java提供了一种原程序中的元素关联任何信息和任何元数据的途径和方法. ...

  2. Arrays类学习笔记

    Arrays.asList(arr); 该方法可以把数组变成List集合. String[] arr = {"abc","cc"}; List<Strin ...

  3. php随笔6-thinkphp OA系统 JS 实时显示当前时间

    不多说,直入主题: JS. // JavaScript Document function showtime() { var today,hour,second,minute,year,month,d ...

  4. 0课程介绍(Week1,3月3日)

    一.自我介绍 1.姓名:杨晔 2.办公室:B211-2 3.电子邮件:yangye@zjjy.com.cn 4.QQ:6706892 5.博客:http://www.cnblogs.com/meety ...

  5. VC程序快速删除自己(可能做升级程序的时候有用)

    项目一般都会带有卸载程序,如果这个程序是自己来做的话,在调用完卸载程序后需要删除自己的所有文件,在Google了好久终于找到一些相关信息,一般只能删除一个文件,经过自己的处理,可以删除文件夹下面所有内 ...

  6. QProcess与外部程序的调用(可以通过设置管道来交互)

    项目做到一定阶段,经常需要在原来的工程上调用外部程序.Qt为此提供了QProcess类,QProcess可用于完成启动外部程序,并与之交互通信. 一.启动外部程序的两种方式:(1)一体式:void Q ...

  7. java如何把char型数据转换成int型数据(转)

    一字符串,String=“2324234535”:把第i个数取出来时是char型的:char temp=String.charAt(i)如何把char型转换成int型?我需要求个尾数之和,如:123的 ...

  8. python 模块BeautifulSoup使用

    BeautifulSoup是一个专门用于解析html/xml的库.官网:http://www.crummy.com/software/BeautifulSoup/ 说明,BS有了4.x的版本了.官方说 ...

  9. B-树和B+树的应用:数据搜索和数据库索引

    B-树和B+树的应用:数据搜索和数据库索引  B-树 1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每 ...

  10. c/c++内存分配方式(转)

    原文链接:http://blog.csdn.net/jing0611/article/details/4030237 1.内存分配方式 内存分配方式有三种: [1]从静态存储区域分配.内存在 程序编译 ...