问题:
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
思路:
因为只有在T%K==0时刻石头才能变成路,路是可以重复走的,所以我们在标记的时候可以采用三维数组把位置和走到这里是T%K的情况也标记下来。
因为当你走到#好旁边,取余有可能不是0,然后你把你标记走到这个位置取余是几也标记一下,就相当于重复走,走到这时可能取1,2,。。。。,是不一样的,每次广搜,就相当于走不一样的路线,走到这个位置,也可以相当于重复走,然后,当你走到#时也可能取1,,2,3.。。,但只有当取0时可以过。
代码:
#define N 110
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int book[N][N][15],n,m,T;
char k[N][N];
int net[4][2]={0,1,-1,0,0,-1,1,0};
struct node
{
int x,y,step;
};
void bfs(int x,int y)
{
queue<node>q;
node h,t;
h.x=x;
h.y=y;
h.step=0;
book[h.x][h.y][0]=1;
q.push(h);
while(!q.empty())
{
h=q.front();
q.pop();
if(k[h.x][h.y]=='G')
{
printf("%d\n",h.step);
return ;
}
for(int i=0; i<4; i++)
{
int tx=h.x+net[i][0];
int ty=h.y+net[i][1];
if(tx<0||ty<0||tx>=n||ty>=m||book[tx][ty][(h.step+1)%T])
continue;
if(k[tx][ty]=='#'&&(h.step+1)%T!=0)
continue;
book[tx][ty][(h.step+1)%T]=1;
t.x=tx;
t.y=ty;
t.step=h.step+1;
q.push(t);
}
}
printf("Please give me another chance!\n");
return ;
}
int main()
{
int c;
scanf("%d",&c);
while(c--)
{
scanf("%d%d%d",&n,&m,&T);
for(int i=0; i<n; i++)
scanf("%s",k[i]);
memset(book,0,sizeof(book));
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(k[i][j]=='Y')
{
bfs(i,j);
}
}
}
return 0;
}