hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接: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's Precious(bfs)的更多相关文章
- hdu 4771 Stealing Harry Potter's Precious
题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
- 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 (2013杭州赛区1002题,bfs,状态压缩)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 4771 Stealing Harry Potter's Precious dfs+bfs
Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...
- hdu4771 Stealing Harry Potter's Precious
注意--你可能会爆内存-- 假设一个直接爆搜索词-- 队列存储器元件被减少到-- #include<iostream> #include<map> #include<st ...
- 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】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- 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 ...
随机推荐
- LeetCode:链表排序
Sort a linked list in O(n log n) time using constant space complexity. public class Solution { publi ...
- Java之对象序列化和反序列化
一.对象序列化和反序列化存在的意义: 当你创建对象,只要你需要,他就一直存在,但当程序结束,对象就会消失,但是存在某种情况,如何让程序在不允许的状态,仍然保持该对象的信息.并在下次程序运行的时候使用该 ...
- TextView实现多个TextView对象的走马灯效果
1:自定义一个控件继承TextView,重写isFocused方法,返回值为true; package com.example.helloandroid; import android.R.bool; ...
- ajax.js
/**通用ajax服务的定义对象 * services可以是单个服务对象,也可以是service服务数组 * 具体服务的定义请参考appendServices成员函数 */ function Serv ...
- Java疯狂讲义(四)
- MacOS + Linux + Nginx
Asp.Net Core 发布和部署( MacOS + Linux + Nginx ) 前言 在上篇文章中,主要介绍了 Dotnet Core Run 命令,这篇文章主要是讲解如何在Linux中,对 ...
- cocos2dx进阶学习之CCSpriteBatchNode
继承关系 CCSpriteBatchNode -> CCNode, CCTextureProtocol 成员变量 inline CCTextureAtlas* getTextureAtlas(v ...
- MySQL server has gone away 解决方法
应用程序(比如PHP)长时间的执行批量的MYSQL语句.执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段.比如,图片数据的处理.都容易引起MySQL server has ...
- struts2的总体回想(ACTION、拦截器、值栈、OGNL表达式、ModelDriven方案等)
ValueStack:struts2的一个存放数据的数据结构(核心) ValueStack大致能够理解为:由Map和对象栈组成 ValueStack作用范围:一个请求,用它来取代request的作用域 ...
- @Autowired与@Resource的差别
1.@Autowired与@Resource都能够用来装配bean. 都能够写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...