题目链接: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. tcpdump使用技巧

    tcpdump使用技巧 http://www.veryarm.com/1751.html

  2. centos7/redhat7 将网卡名字改成eth样式的方法

    方法/步骤    1. 编辑 /etc/sysconfig/grub 找到“GRUB_CMDLINE_LINUX”这一行 

  3. java多线程——同步块synchronized详解

    Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本文介绍以下内容: Java同步关键字(synchronzied) 实例方法同步 静 ...

  4. C#变量命名规范

    1.1命名 1.  所有命名必须有意义 2.  成员变量声明在类的顶端,并且每个变量一行 3.  局部变量声明在引用之前 1.1.1  常量命名 1.  常量名用全大写:MAX_PARAMETER_C ...

  5. Yii2归档安装法

    打开dos 操作命令  1.先把init.bat  拖到dos命令窗口 打开 (如果拖过去没打开 可以回车Enter一下) 这里需要注意一下  下图红圈中是两种环境  0->开发环境  1-&g ...

  6. 使用 Async 和 Await 的异步编程 #Reprinted#

    异步方法容易编写 string urlContents = await client.GetStringAsync(); 以下特征总结了使上面一个异步方法. 方法签名包含一个 Async 或async ...

  7. KBMMW SampleService/SampleClient方式传输数据集

    马上周末了,趁着下午这会儿回顾一下这几天对旧项目的升级过程,一些重要但不常用的东西记录下来是很有必要的.其中一个项目中对KBMMW的远程数据通讯方式做了改进,利用SampleService/Sampl ...

  8. Qt学习 之 文件

    文件操作是应用程序必不可少的部分.Qt 作为一个通用开发库,提供了跨平台的文件操作能力.从本章开始,我们来了解下 Qt 的文件以及输入输出的功能,也就是 I/O 系统. Qt 通过QIODevice提 ...

  9. .vimrc快捷键设置

    $ cat ~/.vimrc,centos7是在/etc/vimrc文件中配置. nmap <C-_>s :cs find s <C-R>=expand("<c ...

  10. SQL Server 基础 05 多链表查询和子查询

     连接查询 值得注意的是:字段前必须加表名,以便混淆 -- 多表连接查询和子查询 select * from dbo.stu_info ,dbo.sname2 -- 加连接规则的查询 where se ...