@Chilling
2017-01-16T16:15:38.000000Z
字数 1995
阅读 885
搜索
Description
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.
Input
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.
Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
Sample Input
1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.
Sample Output
7
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int x,y,m,n,k,flag,ys;
char a[111][111];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int vis[111][111][11];
struct node
{
int x,y,sec;
};
void BFS()
{
node now,next;
queue<node>q;
now.x=x;
now.y=y;
now.sec=0;
q.push(now);
vis[now.x][now.y][0]=1;
while(!q.empty())
{
now=q.front();
q.pop();
if(a[now.x][now.y]=='G')
{
flag=1;
printf("%d\n",now.sec);
return;
}
for(int i=0;i<4;i++)
{
next=now;
next.x+=dir[i][0];
next.y+=dir[i][1];
next.sec++;
ys=next.sec%k;
if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&vis[next.x][next.y][ys]==0)
{
if(a[next.x][next.y]!='#')
{
vis[next.x][next.y][ys]=1; //到这点的时间取余为某值用的最小时间
q.push(next);
}
if(ys==0&&a[next.x][next.y]=='#')
{
vis[next.x][next.y][ys]=1;
q.push(next);
}
}
}
}
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
flag=0;
scanf("%d%d%d",&m,&n,&k);
for(i=0;i<m;i++)
scanf("%s",a[i]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(a[i][j]=='Y')
{
x=i;
y=j;
}
}
BFS();
if(flag==0)
printf("Please give me another chance!\n");
}
return 0;
}