很有意思的题目,在时间能被k整除的时候障碍会消失,这样就需要在一般的二维迷宫判重数组上加上一维。这一维的意义是总时间模k的余数。
vis【i】【j】【t】的意义是总时间模k余t时在x,y。因为一个格子上不同的时间余数代表不同的状态,在这些状态下扩展出的节点可能不同,所以需要三维判重。而当时间余数相同的时候,就没有必要继续扩展了,因为接下去扩展出的节点,他们的状态我们一定扩展过了。
Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1392 Accepted Submission(s): 386
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
struct node
{
int x,y,t;
};
//表示在时间t%k==j的时候在x,y这个点
bool vis[110][110][12];
char g[110][110];
int n,m,k,x1,y1,x2,y2;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool jud(node x)
{
if(x.x<0||x.x>=n||x.y<0||x.y>=m)
return false;
if(vis[x.x][x.y][x.t%k])
return false;
if(g[x.x][x.y]=='#' && x.t%k!=0)
return false;
return true;
}
int bfs()
{
node tmp,a;
queue<node> q;
a.x=x1,a.y=y1,a.t=0;
q.push(a);
vis[x1][y1][0]=1;
while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0;i<4;i++)
{
tmp.t=a.t+1;
tmp.x=a.x+dir[i][0];
tmp.y=a.y+dir[i][1];
if(jud(tmp))
{
if(tmp.x==x2 && tmp.y==y2)
return tmp.t;
vis[tmp.x][tmp.y][tmp.t%k]=1;
q.push(tmp);
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
for(int t_=0;t_<t;t_++)
{
scanf("%d%d%d",&n,&m,&k);
gets(g[0]);
for(int i=0;i<n;i++)
gets(g[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(g[i][j]=='Y') x1=i,y1=j;
else if(g[i][j]=='G') x2=i,y2=j;
memset(vis,0,sizeof(vis));
int ans=bfs();
if(ans==-1)
printf("Please give me another chance!\n");
else
printf("%d\n",ans);
}
return 0;
}