Stealing Harry Potter's Precious BFS+DFS
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:
![](http://acm.hdu.edu.cn/data/images/C486-1002-1.jpg)
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.
![](http://acm.hdu.edu.cn/data/images/C486-1002-1.jpg)
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
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
#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[4] = { 1,-1,0,0 }; int y[4] = { 0,0,1,-1 }; int tx, ty, n, m, k, ans; void bfs(int sx,int sy) { queue<node> q; vis[sx][sy] = true; step[sx][sy] = 0; q.push(node(sx, sy, 0)); while (!q.empty()) { node t = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int nx = t.x + x[i], ny = t.y + y[i]; if (nx >= 0 && ny >= 0 && nx < n&&ny < m && !vis[nx][ny] && g[nx][ny] != '#') { vis[nx][ny] = true; step[nx][ny] = t.t + 1; q.push(node(nx, ny, t.t + 1)); } } } } void dfs(int cnt, int k, int sum) { if (cnt == v.size()) { ans = min(sum, ans); return; } for (int i = 0; i < v.size(); i++) { if (!vis[0][i]) { vis[0][i] = true; dfs(cnt + 1, i, sum + map[k][i]); vis[0][i] = false; } } } int main() { while (scanf("%d%d", &n, &m), n + m) { v.clear(); ans = INF; for (int i = 0; i < n; i++) { scanf("%s", g[i]); for (int j = 0; j < m; j++) if (g[i][j] == '@') v.push_back(node(i, j, -1)); } scanf("%d", &k); for (int i = 0; i < k; i++) { scanf("%d%d", &tx, &ty); v.push_back(node(tx - 1, ty - 1, -1)); } for (int i = 0; i < v.size(); i++) { memset(step, INF, sizeof(step)); memset(vis, false, sizeof(vis)); bfs(v[i].x, v[i].y); for (int j = 0; 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 = 0; for (ck = 0; 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[0][0] = true; dfs(1, 0, 0); if (ans != INF) printf("%d\n", ans); else printf("-1\n"); } }