Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2694 Accepted Submission(s): 754
Problem 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.
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.
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
题意:有一块迷宫,你需要救出女孩,"Y"表示自己的位置,“G”表示女孩子的位置。要求在最短时间内救出女孩。当时间为k的倍数时,石头会变成路。
题解:很好的题目,这里因为石头会变成路,需要特殊考虑。在遍历中,不能直接标记走过的路,在不同路径中到达某一块石头时,时间是不一样的,可能是石头,可能是路。这时候要加入一条时间轴,sign[x][y][t]表示总时间对k取余得到t时,(x,y)这一点的访问情况。这样就能标记好路径。
具体代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,k;
int sx,sy,ex,ey;
int sign[110][110][10];
char map[110][110];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
int x,y,step;
}a,temp;
int OK(node temp)
{
if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&!sign[temp.x][temp.y][temp.step%k])
{
if(map[temp.x][temp.y]!='#'||temp.step%k==0)
return 1;
}
return 0;
}
void bfs()
{
int i,j;
memset(sign,0,sizeof(sign));
queue<node>q;
a.x=sx;
a.y=sy;
a.step=0;
q.push(a);
sign[sx][sy][0]=1;
while(!q.empty())
{
a=q.front();
q.pop();
if(a.x==ex&&a.y==ey)
{
printf("%d\n",a.step);
return ;
}
for(i=0;i<4;++i)
{
temp.x=a.x+dir[i][0];
temp.y=a.y+dir[i][1];
temp.step=a.step+1;
if(OK(temp))
{
q.push(temp);
sign[temp.x][temp.y][temp.step%k]=1;
}
}
}
printf("Please give me another chance!\n");
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<n;++i)
{
scanf("%s",map[i]);
for(j=0;j<m;++j)
{
if(map[i][j]=='Y')
{
sx=i;sy=j;
}
else if(map[i][j]=='G')
{
ex=i;ey=j;
}
}
}
bfs();
}
return 0;
}