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秒.关于康托展开可以 ...
随机推荐
- C#Remoting
C# Remoting 细细品味C#——.Net Remoting专题 http://www.cnblogs.com/xia520pi/archive/2011/11/02/2233371.htm ...
- ubuntu时区设置
local-gen zh_CN.UTF-8 UTF-8 /var/lib/locales/supported.d/local可以看到如下内容: zh_CN.UTF-8 UTF-8 en_US.UTF- ...
- requirejs——config
config 函数用于requirejs的配置信息.主要包括:baseUrl.paths: 一.baseUrl: 待续
- 「小程序JAVA实战」小程序我的个人信息-注销功能(42)
转自:https://idig8.com/2018/09/06/xiaochengxujavashizhanxiaochengxuwodegerenxinxi-zhuxiaogongneng40/ 注 ...
- Python 位运算符 逻辑运算符 成员运算符
位运算符 运算符 描述 实例 & 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 (a & b) 输出结果12 ,二进制解释:0000 1100 | ...
- 关于IP4上WIFI设置静态IP的一点经验
一开始我设置IP4的WIFI的"静态"IP地址后,又查看了一下"BootP"或者"DHCP"选项,然后保存退出(关键错误,后有说明),再进W ...
- 附上SQL Server的存储过程例子
代码如下,看了就明白: --添加项目大类存储过程 use chaiqianD2 go if object_id('p_InsertBigType', 'p') is not null drop pro ...
- Setup Apache2 in Debian 9 and enable two ports for two sites
root@debian:~# apt-get install apache2 root@debian:~# cd /etc/apache2/ root@debian:/etc/apache2# ls ...
- Oracle 下基于 DBMS_RESOURCE_MANAGER 包估算数据库存储 IO 性能
:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...
- 通过maven自动修改idea的compiler
Idea在使用过程中,经常会自动修改compiler水平,有时会变成jdk1.5,不支持@override,也不能忽略实例化的泛型参数,更不支持try-with-resource. 版本太低,很多特性 ...