您的位置:首页 > 其它

hdu 2579 Dating with girls(2) (BFS)

2016-07-04 13:22 495 查看
[align=left]Problem Description[/align]
If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also
in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!

The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.

There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl's location . There is only one ‘Y’ and one ‘G’. Every seconds you can move
left, right, up or down.



 

[align=left]Input[/align]
The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).

The next r line is the map’s description.
 

[align=left]Output[/align]
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
 

[align=left]Sample Input[/align]

1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.

 

[align=left]Sample Output[/align]

7简单bfs
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define inf 1e9
using namespace std;
char maze[110][110];
int n,m,k;
int sx,sy,ex,ey;///sx,sy记录起点,ex,ey记录终点
int ans[110][110][12];///ans[i][j][p]表示“到达(i,j)位置所需时间对k取余所得余数为p”这个状态,到达这个状态所需的最少时间
struct node
{
int x,y,time;
};
int f[4][2]={1,0,-1,0,0,1,0,-1};
int bfs()
{
queue<node> q;
node now,next;
now.x=sx,now.y=sy,now.time=0;
q.push(now);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
for(int p=0;p<=k;p++)
ans[i][j][p]=inf;
ans[sx][sy][0]=0;
while(q.size())
{
now=q.front();
q.pop();
if(now.x==ex&&now.y==ey) return now.time;///因为是bfs,所以第一次到达终点的肯定是最少的时间
for(int i=0;i<4;i++)
{
int mx=now.x+f[i][0];
int my=now.y+f[i][1];
if(mx<=0||mx>n||my<=0||my>m) continue;
next.x=mx,next.y=my;
next.time=now.time+1;
int kk=next.time%k;
if(maze[mx][my]!='#'&&ans[mx][my][kk]>next.time)
{
ans[mx][my][kk]=next.time;
q.push(next);
}
else if(maze[mx][my]=='#'&&kk==0&&ans[mx][my][kk]>next.time)///这个位置是石头,那么就要看到这里时的总时间是不是k的倍数,是的话石头会消失,可以走
{
ans[mx][my][kk]=next.time;
q.push(next);
}
}
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
scanf("%s",maze[i]+1);
for(int j=1;j<=m;j++)
{
if(maze[i][j]=='Y')
{
sx=i;
sy=j;
}
if(maze[i][j]=='G')
{
ex=i;
ey=j;
}
}
}
int res=bfs();
if(res==-1) puts("Please give me another chance!");
else printf("%d\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: