hdu2364之BFS
Escape
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 569 Accepted Submission(s): 227
To shake off any people following you, you do not want to move in a straight line. In fact, you want to take a turn at every opportunity, never moving in any single direction longer than strictly necessary. This means that if, for example, you enter a tile from the south, you will turn either left or right, leaving to the west or the east. Only if both directions are blocked, will you move on straight ahead. You never turn around and go back!
Given a map of the room and your starting location, figure out how long it will take you to escape (that is: reach the edge of the room).
a line with two integers separated by a space, h and w (1 <= h, w <= 80), the height and width of the room;
then h lines, each containing w characters, describing the room. Each character is one of . (period; an accessible space), # (a blocked space) or @ (your starting location).
There will be exactly one @ character in each room description.
A line with an integer: the minimal number of steps necessary to reach the edge of the room, or -1 if no escape is possible.
9 13
#############
#@..........#
#####.#.#.#.#
#...........#
#.#.#.#.#.#.#
#.#.......#.#
#.#.#.#.#.#.#
#...........#
#####.#######
4 6
#.####
#.#.##
#...@#
######
-1
#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=80+10;
char Map[MAX][MAX];
int mark[MAX][MAX][5];//到达i,j是以方向k到达的是否标记
int t,n,m;
int dir[4][2]={0,1,1,0,0,-1,-1,0}; struct Node{
int x,y,time,dir;
Node(){}
Node(int X,int Y,int Time,int Dir):x(X),y(Y),time(Time),dir(Dir){}
}start; int BFS(int flag){
if(start.x == 0 || start.y == 0 ||start.x == n-1 || start.y == m-1)return 0;
queue<Node>q;
Node oq,next;
q.push(start);
mark[start.x][start.y][0]=mark[start.x][start.y][1]=flag;
mark[start.x][start.y][2]=mark[start.x][start.y][3]=flag;
while(!q.empty()){
oq=q.front();
q.pop();
bool f=true;//标记是否只能直走
for(int i=0;i<4;++i){
if(i%2 == oq.dir%2)continue;
next=Node(oq.x+dir[i][0],oq.y+dir[i][1],oq.time+1,i);
if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
if(Map[next.x][next.y] == '#')continue;
f=false;//左右可以走(无论以前是否走过)
if(mark[next.x][next.y][i] == flag)continue;
mark[next.x][next.y][i]=flag;
if(next.x == 0 || next.y == 0 || next.x == n-1 || next.y == m-1)return next.time;
q.push(next);
}
if(f){//只能直走
int i=oq.dir;
next=Node(oq.x+dir[i][0],oq.y+dir[i][1],oq.time+1,i);
if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
if(Map[next.x][next.y] == '#' || mark[next.x][next.y][i] == flag)continue;
mark[next.x][next.y][i]=flag;
if(next.x == 0 || next.y == 0 || next.x == n-1 || next.y == m-1)return next.time;
q.push(next);
}
}
return -1;
} int main(){
cin>>t;
while(t--){
cin>>n>>m;
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] == '@')start.x=i,start.y=j;
}
}
start.time=0,start.dir=-1;
cout<<BFS(t+1)<<endl;
}
return 0;
}
hdu2364之BFS的更多相关文章
- 图的遍历(搜索)算法(深度优先算法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秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
随机推荐
- wchar_t char string wstring 之间的转换
wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...
- Linux 开发环境搭建与使用——SlickEdit 的安装与配置
SlickEdit 的介绍 vim 是公认很好用很完美,但是对新手来说,上手毕竟不是很容易.Windows 下程序员很多都很喜欢Source Insight 这个工具来看代码,各种语法高亮看着很舒服. ...
- httpclient的几种请求URL的方式
一.httpclient项目有两种使用方式.一种是commons项目,这一个就只更新到3.1版本了.现在挪到了HttpComponents子项目下了,这里重点讲解HttpComponents下面的ht ...
- gSOAP:C++编写服务器端
1.编写头文件cal.h: //gsoap ns service name: calc //gsoap ns service style: rpc //gsoap ns service encodin ...
- Unity容器声明周期管理
Having said that, here is a solution that you can use with the Unity container: Create some custom a ...
- 操作表单域中的value值
HTML <form action=""> <input type="radio" name="sex" value=&q ...
- 使用WebClient與HttpWebRequest的差異
在<Windows Phone 7-下載檔案至Isolated Storage>提到了透過WebClient的功能將網站上的檔案下載至 WP7的Isoated Storage之中.但實際的 ...
- 有单例模式 Singleton 涉及的一些防止类被继承的东西
c#中 : ------------------------------- 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. java中: ------------------ ...
- 4-30 HTML 细节摘录
<b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> 定义小号字. <s ...
- 小学生福利web及APP原型展示
332熊哲琛 320刘佳 原型作业地址 https://edu.cnblogs.com/campus/fzzcxy/2016SE/home work/2180 原型设计链接 https://modao ...