hdu2579之BFS
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
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 next r line is the map’s description.
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.
#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的更多相关文章
- HDU2579(bfs迷宫)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- 转 :关于springmvc使用拦截器
原博客: http://elim.iteye.com/blog/1750680 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的 ...
- Eclipse/jre/jdk/jvm
理清一下什么是 jre.jdk.jvm这几个容易混淆的东西. 直接在电脑下安装Eclipse,会提醒你缺少JVM. 1.JVM Java Virtual Machine(Java虚拟机)的缩写. 为了 ...
- Alternative PHP Cache ( APC )
简介: Alternative PHP Cache (APC) 是一个开放自由的PHP opcode 缓存.它的目标是提供一个自由.开放和健全的框架用于缓存和优化 PHP 的中间代码,加快 PHP 执 ...
- 服务器意外重启导致storm报错的问题处理
解决方法 cat /opt/storm-0.8.2/conf/storm.yaml中找到storm.local.dir设定的目录,备份supervisor和workers两个文件夹,#nohup su ...
- windows版mongodb不知道安装在哪儿
情景还原: 从官网:点击打开链接 下载了 MongoDB-win32-x86_64-2.6.12-signed.msi文件后, 右键安装,各种Next后,没有选择路径,就安装结束了!! 任务管理器里面 ...
- MySQL数据库篇之多表查询
主要内容: 一.多表连接查询 二.复合条件连接查询 三.子查询 1️⃣ 多表连接查询 一.准备表 #建表 create table department( id int, name varchar( ...
- java aop 日志打印 正则设置
package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...
- 转 Android:文件下载和写入SD卡学习小结
转自 http://blog.csdn.net/zzp_403184692/article/details/8160739 一.文件下载 Android开发中,有时需要从网上下载一些资源以供用户使 ...
- json-lib 之jsonConfig详细使用(转载写的不错)
=========================== Java To Json ============================= 一,setCycleDetectionStrategy 防 ...
- go_变量定义
package main import "fmt" var( aa =3 bb ="kkk" cc =true )//go语言中,变量可以定义在函数外面,并不是 ...