Problem Description
  Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:

  Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.

 
Input
  There are several test cases.
  In each test cases:
  The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
  Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
  The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
  In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
  The input ends with N = 0 and M = 0
 
Output
  For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
 
Sample Input
2 3
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
 
Sample Output
-1
5
 
Source
 
Recommend
We have carefully selected several similar problems for you:  6079 6078 6077 6076 6075 
 
 
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#define MAXN 102
#define INF 0x3f3f3f3f
#define eps 1e-11 + 1e-12/2
typedef long long LL; using namespace std;
/*
BFS + 最小生成树
*/
char g[MAXN][MAXN];
bool vis[MAXN][MAXN];
int step[MAXN][MAXN];
int map[MAXN][MAXN];
struct node
{
node(int _x, int _y, int _t) :x(_x), y(_y), t(_t) {}
int x, y, t;
};
vector<node> v;
int x[] = { ,-,, };
int y[] = { ,,,- };
int tx, ty, n, m, k, ans;
void bfs(int sx,int sy)
{
queue<node> q;
vis[sx][sy] = true;
step[sx][sy] = ;
q.push(node(sx, sy, ));
while (!q.empty())
{
node t = q.front();
q.pop();
for (int i = ; i < ; i++)
{
int nx = t.x + x[i], ny = t.y + y[i];
if (nx >= && ny >= && nx < n&&ny < m && !vis[nx][ny] && g[nx][ny] != '#')
{
vis[nx][ny] = true;
step[nx][ny] = t.t + ;
q.push(node(nx, ny, t.t + ));
}
}
}
}
void dfs(int cnt, int k, int sum)
{
if (cnt == v.size())
{
ans = min(sum, ans);
return;
}
for (int i = ; i < v.size(); i++)
{
if (!vis[][i])
{
vis[][i] = true;
dfs(cnt + , i, sum + map[k][i]);
vis[][i] = false;
}
} }
int main()
{
while (scanf("%d%d", &n, &m), n + m)
{
v.clear();
ans = INF;
for (int i = ; i < n; i++)
{
scanf("%s", g[i]);
for (int j = ; j < m; j++)
if (g[i][j] == '@')
v.push_back(node(i, j, -));
}
scanf("%d", &k);
for (int i = ; i < k; i++)
{
scanf("%d%d", &tx, &ty);
v.push_back(node(tx - , ty - , -));
}
for (int i = ; i < v.size(); i++)
{
memset(step, INF, sizeof(step));
memset(vis, false, sizeof(vis));
bfs(v[i].x, v[i].y);
for (int j = ; j < v.size(); j++)
if (step[v[j].x][v[j].y] != INF)
map[i][j] = step[v[j].x][v[j].y];
else
map[i][j] = INF;
}
int ck = ;
for (ck = ; ck < v.size(); ck++)
if (map[v[ck].x][v[ck].y] == INF)
break;
if (ck != v.size())
{
printf("-1\n");
continue;
}
memset(vis, false, sizeof(vis));
vis[][] = true;
dfs(, , );
if (ans != INF)
printf("%d\n", ans);
else
printf("-1\n");
}
}

Stealing Harry Potter's Precious BFS+DFS的更多相关文章

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

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

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

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

  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

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

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

  6. hdu4771 Stealing Harry Potter's Precious(DFS,BFS)

    练习dfs和bfs的好题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...

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

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

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

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

  9. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

随机推荐

  1. java8的LocalDateTime与Date互相转换

    LocalDateTime转Date Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); Date ...

  2. [C++ 多线程] 学习前瞻

    1 多线程是什么? 1.1 多线程的概念? 说起多线程,那么就不得不说什么是线程,而说起线程,又不得不说什么是进程. 进程可以简单的理解为一个可以独立运行的程序单位,它是线程的集合,进程就是有一个或多 ...

  3. linux学习之路3 文件系统结构

    一些有用的定义: linux文件系统为一个倒转的单根树状结构 文件系统的根为"/" linux系统文件严格区分大小写,而windows系统不区分大小写 路径使用"/&qu ...

  4. Joseph UVA 1452 Jump

    题目传送门 /* 数学:约瑟夫环问题的变形,首先定义f[i]表示剩下i个人时,最后一个选出的人,有个公式:f[i] = (f[i-1] + m) % i f[1] = 0(编号从0开始),那么类似最后 ...

  5. JavaScript 正则表达式(转自 mozilla)

    正则表达式是被用来匹配字符串中的字符组合的模式.在JavaScript中,正则表达式也是对象. 这种模式可以被用于 RegExp 的 exec 和 test 方法以及 String 的 match.r ...

  6. Python操作远程数据库

    我的项目要往数据库中插入create_time和update_time,那就势必要引用现在的系统时间,经过大量的查找,终于发现往python是没有对应时间datetime的相关通配符的,那么我们要怎么 ...

  7. 微软MVC框架实战:开源的JS库Knockout

    [IT168 技术] Knowckout – 当MVC遭遇MVVM Knockout (或者Knockout.js ,KnockoutJS)是一个开源的JavaScript库,网址为www.knock ...

  8. phpcms标签用法(转)

    1.显示指定catid的栏目名称和链接 {$CATEGORYS[25]['catname']}  {$CATEGORYS[25]['url']} 获取父栏目id/获取父栏目名称  $CATEGORY[ ...

  9. MFC_2.9 使用变参函数

    使用变参函数 #include <stdio.h>​// 包含一个头文件,提供不定参数的宏#include <stdarg.h>​// 用于输出不定数量的整数值void pri ...

  10. 梦想CAD控件关于标注的系统变量说明

    主要用到函数说明: IMxDrawDimension::SetDimVarDouble 设置标注属性的实数类型变量值,详细说明如下: 参数 说明 [in] LONG iType 该属性的类形值 dVa ...