【HDU】 2579 Dating with girls(2)(BFS,可重复)
【题目链接】http://acm.hdu.edu.cn/showproblem.php?pid=2579
题目
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.
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
题目大意
在迷宫里,”Y”是你,”G”是女孩,也就是目的地,”.”是空地,”#”是石头,当时间是K的倍数时,石头消失。求最短路程。
解题思路
因为时间不同,石头的状态不同,可能这一次到这个地方是石头然而下一次绕过来石头就消失了,所以可能重复走某短路等到时间是k的倍数的时候走过石头,因此标记的时候要记录vist[i][j][time%k],这样才能保证走到[i][j]的每一种时间情况。至于时间,可以用数组或者结构体来记录。
一组可能有用的数据
1
3 3 5
Y##
..#
##G
正确输出为6
AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<math.h>
#include<limits.h>
#include<stack>
#include<queue>
#define LL long long
#define mod 1000000007
using namespace std;
int n,r,c,k;
int mx[4]={0,0,-1,1};
int my[4]={1,-1,0,0};
char maze[101][101];
int t[101][101],vist[101][101][11];
struct node
{
int x,y;
};
queue <node> q;
int bfs()
{
int i,j;
node str;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(maze[i][j]=='Y')
{
str.x=i;
str.y=j;
vist[i][j][0]=1;
break;
}
}
if(maze[i][j]=='Y')
break;
} //先找到起点
if(maze[i][j]=='Y') q.push(str);
while(!q.empty())
{//printf("A");
node f,ne;
f=q.front();
if(maze[f.x][f.y]=='G')
return t[f.x][f.y];
q.pop();
for(i=0;i<4;i++)
{
if((f.x+mx[i]<0||f.x+mx[i]>=r||f.y+my[i]<0||f.y+my[i]>=c)
||(maze[f.x+mx[i]][f.y+my[i]]=='#'&&(t[f.x][f.y]+1)%k!=0)
||vist[f.x+mx[i]][f.y+my[i]][(t[f.x][f.y]+1)%k])
//越界或下一步是墙而t不是k的倍数或该点该时间已经走过这三种情况下要continue;
continue;
ne=f;
ne.x+=mx[i],ne.y+=my[i];
t[ne.x][ne.y]=t[f.x][f.y]+1;
vist[ne.x][ne.y][t[ne.x][ne.y]%k]=1;
q.push(ne);
}
}
return 0;
}
int main()
{
int ans;
scanf("%d",&n);
while(n--)
{
memset(maze,0,sizeof(maze));
memset(t,0,sizeof(t));
memset(vist,0,sizeof(vist));
scanf("%d%d%d",&r,&c,&k);
for(int i=0;i<r;i++)
scanf("%s",maze[i]);
while(!q.empty()) q.pop();
ans=bfs();
/*for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%d ",t[i][j]);
}
printf("\n");
}*/
if(ans==0)
printf("Please give me another chance!\n");
else
printf("%d\n",ans);
}
return 0;
}