Dating with girls(2)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1470    Accepted Submission(s): 414

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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=100+10;
char Map[MAX][MAX];
int mark[MAX][MAX][20];
int n,m,k;
int dir[4][2]={0,1,0,-1,1,0,-1,0}; struct Node{
int x,y,time;
Node(){}
Node(int X,int Y,int Time):x(X),y(Y),time(Time){}
}start; int BFS(int &flag){
queue<Node>q;
Node oq,next;
q.push(start);
mark[start.x][start.y][start.time%k]=flag;
while(!q.empty()){
oq=q.front();
q.pop();
for(int i=0;i<4;++i){
next=Node(oq.x+dir[i][0],oq.y+dir[i][1],oq.time+1);
if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
if(mark[next.x][next.y][next.time%k] == flag)continue;
if(next.time%k != 0 && Map[next.x][next.y] == '#')continue;
mark[next.x][next.y][next.time%k]=flag;
if(Map[next.x][next.y] == 'G')return next.time;
q.push(next);
}
}
return -1;
} int main(){
int num=0,t;
cin>>t;
while(t--){
cin>>n>>m>>k;
for(int i=0;i<n;++i)cin>>Map[i];
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(Map[i][j] == 'Y')start.x=i,start.y=j;
}
}
start.time=0;
int temp=BFS(++num);
if(temp != -1)cout<<temp<<endl;
else cout<<"Please give me another chance!"<<endl;
}
return 0;
}

hdu2579之BFS的更多相关文章

  1. HDU2579(bfs迷宫)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  3. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  4. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  5. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  6. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  7. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  8. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  9. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

随机推荐

  1. Less、Sass/Scss

    一.Less.Sass/Scss是什么? 1.Less: 是一种动态样式语言. 对CSS赋予了动态语言的特性,如变量.继承.运算.函数. Less 既可以在客户端上运行 (支持IE 6+, Webki ...

  2. 【转】C#命名规范

    原文地址:http://www.jb51.net/article/57163.htm 本文详细汇总了C#常用的命名规则.分享给大家供大家参考.具体如下: Pascal 规则每个单词开头的字母大写(如 ...

  3. RPC框架的服务注册和发现

    https://www.cnblogs.com/valor-xh/p/6281502.html https://blog.csdn.net/listslim1/article/details/5157 ...

  4. pycharm git工具与coding.net结合

    前提:coding.net中的项目是私密项目 问题描述:在使用pycharm自带的git工具clone(或者push)代码时出现 错误如下:Push failed: Failed with error ...

  5. C# 本地文件夹上传至网络服务器中(待续)

    一.文件的上传参考 思想,C#FTP上传 /// <summary> /// 上传 /// </summary> /// <param name="filena ...

  6. delphi 浮点 精度

    double 没有问题, Single有问题 '0.7' 0.69999999999999996 Single; // 4 byte real Double; // 8 byte real

  7. python算法之选择排序

    选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大 ...

  8. Make 命令教程(转载)

    代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编译的安排),叫做构建(build). Make是最常用的构建工具,诞生于1977年,主要用于C语言的项目.但是实际上 , ...

  9. Django之ModelForm篇

    ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 labels=None, # 提示信 ...

  10. ShaderLab内置变量

    [ShaderLab内置变量]