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. ASP.NET 之页面重定向和传值

    在开发 ASP.NET 网站时,经常需要从一个网页重定向(导航)到另一个网页,同时希望能够将信息从源页传递到目标页.例如,如果你正在开发一个保险网站,需要用一个页面来收集基本信息(用户信息.保险产品信 ...

  2. 6.12---esultMap查询所有

  3. Selenium 进行参数化

    Selenium参数化分为大小: 小:list.dict.函数 大:txt.excel.mysql.redis 哪种方式使自己的工作简单高效就选那种!!! Selenium进行参数化有多种形式: 本文 ...

  4. Pro ASP.NET Core MVC 第6版 第一章

    目录 第一章 ASP.NET Core MVC 的前世今生 ASP.NET Core MVC 是一个微软公司开发的Web应用程序开发框架,它结合了MVC架构的高效性和简洁性,敏捷开发的思想和技术和.N ...

  5. Hue - Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or

    解决下面两点异常 >> 1. Hue页面 点击DB 查询时弹出: Error loading MySQLdb module: libmysqlclient.so.20: cannot op ...

  6. MSSQLSERVER_3176

    本文档已存档,并且将不进行维护. MSSQLSERVER_3176                 SQL Server 2014                    SQL Server 2012 ...

  7. 诊断Java中的内存泄露

    每次我怀疑有内存泄漏时,我都要翻箱倒柜找这些命令.所以,这里总结一下以备后用: 首先,我用下面的命令监视进程: 1 while ( sleep 1 ) ; do ps -p $PID -o %cpu, ...

  8. httponlycookie

    cookie cookie是目前标识用户身份一项非常流行的技术:设置httponly的cookie客户端是不能通过js来修改的:你以为这样就万事大吉,没有办法伪造了吗? 背景介绍 假设网站A通过设置h ...

  9. 第四次作业——项目Alpha测试

    这个作业属于哪个课程 <课程链接> 这个作业要求在哪里 <作业要求> 团队名称 飞猪们 这个作业的目标 发布项目α版本,对项目进行用例测试,以及项目情况总结 一.团队成员学号列 ...

  10. layer iframe层ajax回调弹出layer.msg()

    ajax success方法 success: function(data){ layer.msg("输入你需要的提示",{time:1000,end:function(){ // ...