您的位置:首页 > 其它

HDU 4771 Stealing Harry Potter's Precious(bfs+dfs)

2014-08-08 09:47 471 查看


Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1105 Accepted Submission(s): 533



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


题目大意:#表示不能走,@起点, · 可以走,然后给出几个宝物的位置,坐标,求拿到全部宝物要走的路程,不能走完输出-1

先用bfs求出没两个宝物见的最短路,然后在最短路这个图上用dfs找到最短要走的路程即可,也可以用状压dp来写,这里给出dfs代码
ps:队友想到用kruscal,并查集来写这个问题,但这样只能求出所有宝物连起来的最短路,但是这里求得是走的路程,可能重复走一条路,所以此法不可行

题目链接:点击打开链接

代码注释:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 105
#define inf 0x7ffffff
int visit

,vis
,dis

,n,m,Min,cc;//visit用作bfs的标记,vis用作dfs的标记,dis存两点间最短路,cc宝物总个数
char map

;
int dir[2][4]= {1,-1,0,0,0,0,1,-1};

struct node
{
int x,y,step;//step记载最短路
} good
,cur,tem;

bool ok(int a,int b)
{
if(a<1||a>n||b<1||b>m)
return false;
return true;
}
void dfs(int index,int sum,int c)
{
if(c==cc)
{
Min=min(Min,sum);
return ;
}
for(int i=1;i<=cc;i++)
{
if(!vis[i])
{
vis[i]=1;
dfs(i,dis[index][i]+sum,c+1);
vis[i]=0;
}
}
}

int bfs(node s,node e)
{
memset(visit,0,sizeof(visit));
queue<node>q;
while(!q.empty())
q.pop();
if(s.x==e.x&&s.y==e.y)//同一个点,返回距离0
return 0;
visit[s.x][s.y]=1;
q.push(s);
while(!q.empty())
{
cur=q.front();
q.pop();
for(int k=0; k<4; k++)
{
int X=tem.x=cur.x+dir[0][k];
int Y=tem.y=cur.y+dir[1][k];
if(map[X][Y]!='#'&&!visit[X][Y]&&ok(X,Y))
{
visit[X][Y]=1;
tem.step=cur.step+1;
if(X==e.x&&Y==e.y)
return tem.step;
q.push(tem);
}
}
}
return -1;//没有路返回-1
}
int main()
{
int i,j;
while(cin>>n>>m,m+n)
{
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
good[0].x=i;//起点记为第一个宝物
good[0].y=j;
}
}
}
cin>>cc;
for(i=1; i<=cc; i++)
cin>>good[i].x>>good[i].y;
int flag=0;
for(i=0; i<=cc; i++)
for(j=i+1; j<=cc; j++)
{
dis[i][j]=dis[j][i]=bfs(good[i],good[j]);//用最短路建图
if(dis[i][j]==-1)
{
flag=1;
break;
}
}
if(flag)
cout<<-1<<endl;
else
{
Min=inf;
memset(vis,0,sizeof(vis));
vis[0]=1;
dfs(0,0,0);
cout<<Min<<endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: