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秒.关于康托展开可以 ...
随机推荐
- Nginx not running with no error message
Nginx not running with no error message #!/bin/shecho "start"rm /etc/nginx/sites-enabled/d ...
- 自适应共振理论网络 ART
引言 自适应共振理论的发源与现状 1976年, 美国 Boston 大学学者 G. A.Carpenter 提出自适应共振理论(Adaptive Res-onance Theory , ART ), ...
- GPRS 通信
最近使用SIM900A GSM/GPRS模块,做一些简单物联网信息投递. 1.选取何种流量卡 一般来讲,需要带身份证去运营商办理新的电话卡.对于小流量用户,实际上就是办一个最低月租即可,因为套餐会赠送 ...
- keil里面填数据
编译后寄存器和堆栈的内存数据可以直接写进去的. 寄存器,双击就可以,注意里面是十六进制 堆栈,也是十六进制,八位的 00 00 00 00 ,但这个是从右到左的,比如0x00000006 应该填 06 ...
- Markdown之表格的处理
目前编辑器不支持表格,以往是通过截图,呈现的效果并不好,Markdown支持html,所以我们可以用html来写表格.但是......用html写表格,实在太麻烦了,这里有个简单的转换方法,供大家参考 ...
- QT与HALCON(入门)
#include "qtdesign.h" #include <QtGui/QApplication> #include "halconcpp.h" ...
- linux/ubuntu 端口开放
在ubuntu下面开放端口好像主要有两种方法,一种是ubuntu自带的防火墙,一种是iptables,这里我们主要使用iptables.本文的系统版本为ubuntu14.04和ubuntu16.04 ...
- bugfree调试
最近项目用到bugfree ,一直不能用,原来是session目录的文件权限有问题.
- 【jdbc】【c3p0】c3p0三种配置方式【整理】
c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...
- Python_12-线程编程
1.1 Python中的线程使用1.1.1 函数式1.2 创建threading.Thread的子类来包装一个线程对象1.2.1 threading.Thread类的使用1.3 线程 ...