您的位置:首页 > 其它

hdu 4771 Stealing Harry Potter's Precious(DFS+BFS)

2017-04-29 15:52 375 查看

Stealing Harry Potter’s Precious

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, ‘.’
4000
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

思路:因为k最大为4,所以可以dfs每一种情况,bfs求最短距离(具体详见代码)

代码:

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 110
const int inf=0x3f3f3f3f;
struct node
{
int x,y,step;
};
struct point
{
int x,y;
} p[5];
int to[][2]= {0,1,0,-1,1,0,-1,0};
char mp[maxn][maxn];
bool vis[maxn][maxn],inq[5];
int n,m,st_x,st_y,k,ans;

int bfs(int ed_x,int ed_y)
{
memset(vis,false,sizeof(vis));
node now,next;
now.x=st_x,now.y=st_y,now.step=0;
queue<node>q;
q.push(now);
while(!q.empty())
{
now=q.front();
for(int i=0; i<4; ++i)
{
int xx=now.x+to[i][0],yy=now.y+to[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&mp[xx][yy]!='#'&&!vis[xx][yy])
{
vis[xx][yy]=true;
next.x=xx,next.y=yy,next.step=now.step+1;
if(xx==ed_x&&yy==ed_y)
{
st_x=ed_x,st_y=ed_y;
return next.step;
}
q.push(next);
}
}
q.pop();
}
return -1;
}

void dfs(int step,int num)//枚举每种情况
{
if(step>ans)
return ;
if(num==k)
{
if(step<ans)
ans=step;
return ;
}
for(int i=0; i<k; ++i)
{
if(!inq[i])
{
int x=st_x,y=st_y;
int tot=bfs(p[i].x,p[i].y);
if(tot!=-1)
{
inq[i]=true;
dfs(step+tot,num+1);
inq[i]=false;
}
st_x=x,st_y=y;
}
}
}

int main()
{
while(~scanf("%d%d",&n,&m),n||m)
{
memset(inq,false,sizeof(inq));
for(int i=0; i<n; ++i)
{
scanf("%s",mp[i]);
for(int j=0; j<m; ++j)
{
if(mp[i][j]=='@')
{
st_x=i,st_y=j;
break;
}
}
}
scanf("%d",&k);
for(int i=0; i<k; ++i)
{
scanf("%d%d",&p[i].x,&p[i].y);
--p[i].x,--p[i].y;
}
ans=inf;
dfs(0,0);
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}


总结:比赛时想到了要先bfs搜最近的宝藏,然后以此地为起点接着找最近的第二处宝藏,最后直到找完所有宝藏。

可是这个思想被队友证明了得出的距离不是最优的,最重要的是当时没有想到即使这个不是最优的,那也可以枚举所以情况找到最优的。。。(因为k很小)

所以说,如果题目中明显给出了一个较小的范围,那这道题十有八九就是暴力题!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: