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秒.关于康托展开可以 ...
随机推荐
- linux 进程通信 :流套接字
消息队列是可以实现没有共同关系的进程之间的通信.Socket则可以实现不同计算机的不同进程之间的通信. //地址的结构体 struct sockaddr_in{ short int sin_famil ...
- linux shell find
find / -ctime +365 ! -name "*.bat" -exce rm -rf {} \;
- jBPM4.4 eclipse插件安装
一.工具下载 jBPM 图形化流程设计器采用eclipse做为平台,我们可以先到jBPM官方网站下jBPM4.4,地址:http://sourceforge.net/projects/jbpm ...
- Coursera连接不上(视频无法播放),修改hosts文件
视频问题 如果Coursera网站连接不上,或者视频加载不出来.可以通过如下方式进行配置: 一.找到hosts文件 Windows 系统, hosts文件位于: [C:\Windows\Syste ...
- Redis搭建(六):Redis持久化配置
一.介绍 Redis的持久化有2种方式: Rdb快照 Aof日志 1. Rdb快照的配置选项 save 900 1 // 900内,有1条写入,则产生快照 save 300 1000 // 如果300 ...
- GameObject.Active
[GameObject.Active] 用于控制一个对象是否激活,一个对象激活当且本身active=true,并且它的父结点也都active.相当API有: 1)GameObject.SetActiv ...
- 开发 WebAPP 的几个前端框架(不断更新中)
http://mobileangularui.com/ http://ionicframework.com/ http://www.idangero.us/framework7/ (中文文档:http ...
- 关于Redis的常识
原文出自:http://blog.jobbole.com/44476/ 版本:V3.0.2 2013-7-13 (江南白衣版权所有,转载请保留出处) 1. Overview 1.1 资料 <Th ...
- 【ZOJ3329】One Person Game
题意 你有三枚色子,第i个色子有ki面,你有一个计数器. 1.开始的时候将计数器调至0 2.扔三个色子,如果色子1是a,色子2是b,色子3是c,则将计数器归零.否则计数器加上三个色子的和. 3.如果计 ...
- H5 继承
继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码:如某种颜色应用于p标签,这个颜色设置不仅应用p标签 ...