Dating with girls(2)
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的整数倍时会消失,求最少多长时间到G。
思路:到达一个点我可以在不同时间到达,因此过石头的时候也不一定,一个点可能需要走n次。所以需要对k求余。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
struct lou
{
int x,y,t;
};
char s[110][110];
int xx,yy,m,n,k,r[4][2]={0,1,0,-1,1,0,-1,0},book[110][110][15];
void bfs()
{
queue<lou>Q;
lou p,q;
int i;
memset(book,0,sizeof(book));
p.x=xx;
p.y=yy;
p.t=0;
book[xx][yy][0]=1;
Q.push(p);
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(s[p.x][p.y]=='G')
{
printf("%d\n",p.t);
return;
}
if(s[p.x][p.y]=='#')
{
if(p.t%k!=0)
continue;
}
for(i=0;i<4;i++)
{
q.x=p.x+r[i][0];
q.y=p.y+r[i][1];
q.t=p.t+1;
if(q.x<0||q.x>=n||q.y<0||q.y>=m||book[q.x][q.y][q.t%k])
continue;
book[q.x][q.y][q.t%k]=1;
Q.push(q);
}
}
printf("Please give me another chance!\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
int i,j;
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
for(j=0;j<m;j++)
{
if(s[i][j]=='Y')
{
xx=i;
yy=j;
}
}
}
bfs();
}
return 0;
}
上一篇: 【python作业】作业一_编写登录接口
下一篇: Python 字符串 列表 元组 字典
推荐阅读
-
罗永浩、卢伟冰联手带货小米10手机 送2大福利、50W红包
-
photoshop cs2 v9.0 绿色中文版 PHP开发框架总结收藏
-
安装APACHE出现No install service named apache2解决思路
-
lcd debug notes 2(qcom and orise)
-
DB2 runstats、reorgchk、reorg 命令
-
做一个p2p网贷系统需要注意些什么?
-
使用PHPRPC实现Ajax安全登录第1/2页_PHP教程
-
PHP中开启gzip压缩的2种方法_php技巧
-
php简简单单搞定中英文混排字符串截取,只需2行代码!_PHP教程
-
MySQL生产库Insert了2次同样的记录但是主键ID是不一样的问题的分_MySQL