Stealing Harry Potter's Precious
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 126 Accepted Submission(s): 63
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
bfs就可以了。
/* ***********************************************
Author :kuangbin
Created Time :2013-11-9 13:26:38
File Name :E:\2013ACM\专题强化训练\区域赛\2013杭州\1002.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int n,m;
char g[][];
int a[][]; int sx,sy;
int k; int dp[][][];
int px[],py[]; struct Node
{
int x,y;
int s;
Node(int _x = ,int _y = ,int _s = )
{
x = _x;
y = _y;
s = _s;
}
};
int move[][] = {{,},{,-},{,},{-,}};
int bfs()
{
queue<Node>q;
int s = ;
for(int i = ;i < k;i++)
if(sx == px[i] && sy == py[i])
s = s|(<<i);
q.push(Node(sx,sy,s));
memset(dp,-,sizeof(dp));
dp[sx][sy][s] = ;
while(!q.empty())
{
Node tmp = q.front();
q.pop();
if(tmp.s == ((<<k) - ))
{
return dp[tmp.x][tmp.y][tmp.s];
}
for(int i = ;i < ;i++)
{
int nx = tmp.x + move[i][];
int ny = tmp.y + move[i][];
int s = tmp.s;
if(nx < || nx >= n || ny < || ny >= m)continue;
if(a[nx][ny] == -)continue;
for(int j = ;j < k;j++)
if(nx == px[j] && ny == py[j])
{
s |= (<<j);
}
if(dp[nx][ny][s] != -)continue;
dp[nx][ny][s] = dp[tmp.x][tmp.y][tmp.s] + ;
q.push(Node(nx,ny,s));
}
} return -;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m) == )
{
if(n == && m == )break;
for(int i = ;i < n;i++)
scanf("%s",g[i]);
memset(a,-,sizeof(a));
for(int i = ;i < n;i++)
for(int j = ;j < m;j++)
{
if(g[i][j] == '@')
{
sx = i;
sy = j;
}
if(g[i][j] == '#')
a[i][j] = -;
}
scanf("%d",&k);
int x,y;
for(int i = ;i < k;i++)
{
scanf("%d%d",&x,&y);
x --;
y--;
px[i] = x;
py[i] = y;
}
printf("%d\n",bfs());
} return ;
}