Escape

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 569    Accepted Submission(s): 227

Problem Description
You find yourself trapped in a large rectangular room, made up of large square tiles; some are accessible, others are blocked by obstacles or walls. With a single step, you can move from one tile to another tile if it is horizontally or vertically adjacent (i.e. you cannot move diagonally).

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).

 
Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

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.

 
Output
For each test case:

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.

 
Sample Input
2
9 13
#############
#@..........#
#####.#.#.#.#
#...........#
#.#.#.#.#.#.#
#.#.......#.#
#.#.#.#.#.#.#
#...........#
#####.#######
4 6
#.####
#.#.##
#...@#
######
 
Sample Output
31
-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的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. 2018-2019学年第一学期Java课程设计

    目录 Magic-Towers 一.团队课程设计博客链接   [团队博客地址](https://www.cnblogs.com/lmb171004/p/10271667.html 二.个人负责模块或任 ...

  2. 使用WebClient與HttpWebRequest的差異

    在<Windows Phone 7-下載檔案至Isolated Storage>提到了透過WebClient的功能將網站上的檔案下載至 WP7的Isoated Storage之中.但實際的 ...

  3. ES6系列_7之箭头函数和扩展

    1.默认值 在ES6中给我们增加了默认值的操作相关代码如下: function add(a,b=1){ return a+b; } console.log(add(1)); 可以看到现在只需要传递一个 ...

  4. Rhythmk 一步一步学 JAVA (19): 注解 annotation

    在编写注解的时候需要了解的四种注解: @Target 表示该注解可以用于什么地方,可能的ElementType参数有: CONSTRUCTOR:构造器的声明 FIELD:域声明(包括enum实例) L ...

  5. 可视化库-Matplotlib-饼图与布局(第四天)

    1. 画出一个基本的饼图,通过plt.pie() m = 51212 f = 40742 m_perc = m / (m+f) f_perc = f / (m+f) colors = ['navy', ...

  6. 迷你MVVM框架 avalonjs 学习教程2、模块化、ViewModel、作用域

    一个项目是由许多人分工写的,因此必须要合理地拆散,于是有了模块化.体现在工作上,PM通常它这为某某版块,某某频道,某某页面.某一个模块,必须是包含其固有的数据,样式,HTML与处理逻辑.在jQuery ...

  7. 迷你MVVM框架 avalonjs 0.91发布

    本版本修了一些BUG与不合理的地方,感谢感谢ztz, 民工精髓, 姚立, qiangtou等人指正. 处理AMD加载 旧式IE下移除script节点内存泄漏的问题 fix firefox 全系列vis ...

  8. filebeat 笔记

    认识Beats Beats是用于单用途数据托运人的平台.它们以轻量级代理的形式安装,并将来自成百上千台机器的数据发送到Logstash或Elasticsearch. (画外音:通俗地理解,就是采集数据 ...

  9. 54. Spiral Matrix (Graph)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  10. Jquery的树插件jqxTreeGrid的使用小结(实现基本的增删查改操作)

    一.引入相应的js <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" t ...