Stealing Harry Potter's Precious BFS+DFS
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.
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
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
5
#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的更多相关文章
- hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=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 ...
- 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
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 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 ...
- hdu4771 Stealing Harry Potter's Precious(DFS,BFS)
练习dfs和bfs的好题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...
- 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 Stealing Harry Potter's Precious(状压BFS)
状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...
随机推荐
- 递推DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...
- .net引用System.Data.SQLite操作SQLite
之所以要做这个笔记,是因为在.NET中使用System.Data.SQLite的时候,遇到了些问题,这些问题是相对于引用其他dll没有遇到过的,所以作个笔记,记录一下. 简单起见,首先建立一个控制台项 ...
- 368 Largest Divisible Subset 最大整除子集
给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...
- [转]mysql的查询、子查询及连接查询
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html 一.mysql查询的五种子句 where(条件 ...
- Kali linux 2016.2(Rolling)安装之后的常用配置
前言 使用默认的Kali Linux设置来学习是可以的,但是我们通常要修改系统的一些基本设置,来最大化使用Kali平台的功能. 以下内容 网络的基础知识 使用图形用户界面来配置网卡 使用命令行来配置网 ...
- Winform学习知识汇总
引用博客 http://www.cnblogs.com/peterzb/archive/2009/06/14/1502918.html
- JS在即将离开当前页面(刷新或关闭)时触发事件
// onbeforeunload 事件在即将离开当前页面(刷新或关闭)时触发 window.onbeforeunload = function () { return /^\#\/ipinfo/.t ...
- js截取字符串 区分中英文
方法如下: //在一个字符串中截取前面部分文字,汉字.全角符号按2个占位,数字英文.半角按一个占位,未显示完的最后加入“……”. //适合多行显示. function suolve(str, sub_ ...
- 浅谈css的行内类型标签和块级标签
常用标签的行内类型标签有:a.span.img:块级标签有:div.p.h1~6.ul.ol.li.dl.dt.dd. 行内类型标签的特征:标签的大小由标签的内容决定,不能设置width和height ...
- Android基础夯实--重温动画(一)之Tween Animation
心灵鸡汤:真正成功的人生,不在于成就的大小,而在于你是否努力地去实现自我,喊出自己的声音,走出属于自己的道路. 摘要 不积跬步,无以至千里:不积小流,无以成江海.学习任何东西我们都离不开扎实的基础知识 ...