您的位置:首页 > 其它

训练之搜索约会2

2016-07-21 15:10 225 查看
Dating with girls(2)Crawling in process...
Crawling failed
Time Limit:5000MS    
Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit

Status

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

一道搜索题,增加了当时间是k的倍数是可以穿过墙,多一重判断是否可以穿过墙就好了。

#include<stdio.h>
#include<queue>
using namespace std;
char map[110][110];
int dir[4][2]={-1,0,0,-1,1,0,0,1};
int ans[110][110][15];
int n,m,k,yx,yy,gx,gy;
struct node
{
int x,y,t;
};
int BFS()
{
queue<node> qu;
node p1;
p1.x=yx;
p1.y=yy;
p1.t=0;
qu.push(p1);
while(qu.size())
{
p1=qu.front();
qu.pop();
if(map[p1.x][p1.y]=='G') return p1.t;
for(int i=0;i<4;i++)
{
node p2;
int px=p1.x+dir[i][0];
int py=p1.y+dir[i][1];
if(px<0||px>=n||py<0||py>=m)   continue;
p2.x=px;
p2.y=py;
p2.t=p1.t+1;
int tk=p2.t%k;
if(map[px][py]=='G') return p2.t;
if(map[px][py]!='#'&&ans[px][py][tk]>p2.t)
{
ans[px][py][tk]=p2.t;
qu.push(p2);
}
else if(tk==0&&ans[px][py][tk]>p2.t)
{
ans[px][py][tk]=p2.t;
qu.push(p2);
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++)    scanf("%s",map[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='Y')  {yx=i;yy=j;}
if(map[i][j]=='G')  {gx=i;gy=j;}
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
for(int z=0;z<=k;z++)
ans[i][j][z]=999999;
ans[yx][yy][0]=0;
int anst;
anst=BFS();
if(anst==-1)
printf("Please give me another chance!\n");
else
printf("%d\n",anst);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  搜索