Children of the Candy Corn 分类: POJ 2015-07-14 08:19 7人阅读 评论(0) 收藏
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10933 | Accepted: 4708 |
Description
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom
the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding
visitors.
Input
each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also
be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.
Output
space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.
Sample Input
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
Sample Output
37 5 5
17 17 9
注意好靠左走和靠右走的方向就行#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f using namespace std; char Map[100][110];
int Left,Right,Short;
bool vis[110][110];
int Dir[][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m;
struct node
{
int x;
int y;
int step;
};
void Left_Look(int step,int dir,int X,int Y)
{
if(Map[X][Y]=='E')
{
Left=step;
return ;
}
dir--;
if(dir<0)
{
dir=3;
}
int d,fx,fy;
for(int i=0;i<4;i++)
{
d=dir+i;
if(d>=4)
{
d-=4;
}
fx=Dir[d][0]+X;
fy=Dir[d][1]+Y;
if(fx>=0&&fx<n&&fy>=0&&fy<m&&Map[fx][fy]!='#')
{
Left_Look(step+1,d,fx,fy);
break;
}
}
}
void Right_Look(int step,int dir,int X,int Y)
{
if(Map[X][Y]=='E')
{
Right=step;
return ;
}
dir++;
dir%=4;
int d,fx,fy;
for(int i=0;i<4;i++)
{
d=dir-i;
if(d<0)
{
d+=4;
}
fx=Dir[d][0]+X;
fy=Dir[d][1]+Y;
if(fx>=0&&fx<n&&fy>=0&&fy<m&&Map[fx][fy]!='#')
{
Right_Look(step+1,d,fx,fy);
break;
}
}
}
void BFS(int X,int Y)
{
node a,b;
a.x=X;
a.y=Y;
a.step=1;
memset(vis,false,sizeof(vis));
vis[X][Y]=true;
queue<node >Q;
Q.push(a);
while(!Q.empty())
{
a=Q.front();
Q.pop();
if(Map[a.x][a.y]=='E')
{
Short=a.step;
return ;
}
for(int i=0;i<4;i++)
{
b.x=a.x+Dir[i][0];
b.y=a.y+Dir[i][1];
b.step=a.step+1;
if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&!vis[b.x][b.y]&&Map[b.x][b.y]!='#')
{
vis[b.x][b.y]=true;
Q.push(b);
}
}
}
}
int main()
{
int T; int x;
int y;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&m,&n);
for(int i=0; i<n; i++)
{
scanf("%s",Map[i]);
for(int j=0;j<m;j++)
{
if(Map[i][j]=='S')
{
x=i;
y=j;
}
}
}
if(x==0)
{ Left_Look(1,2,x,y);
Right_Look(1,2,x,y);
}
else if(x==n-1)
{
Left_Look(1,0,x,y);
Right_Look(1,0,x,y);
}
else if(y==0)
{
Left_Look(1,1,x,y);
Right_Look(1,1,x,y);
}
else if(y==m-1)
{
Left_Look(1,3,x,y);
Right_Look(1,3,x,y);
}
BFS(x,y);
cout<<Left<<" "<<Right<<" "<<Short<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Children of the Candy Corn 分类: POJ 2015-07-14 08:19 7人阅读 评论(0) 收藏的更多相关文章
- Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏
效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 浅谈new operator、operator new和placement new 分类: C/C++ 2015-05-05 00:19 41人阅读 评论(0) 收藏
浅谈new operator.operator new和placement new C++中使用new来产生一个存在于heap(堆)上对象时,实际上是调用了operator new函数和placeme ...
- Binary Indexed Tree 2D 分类: ACM TYPE 2014-09-01 08:40 95人阅读 评论(0) 收藏
#include <cstdio> #include <cstdlib> #include <climits> #include <cstring> # ...
- Brush Mode --- Nyoj 737 分类: Brush Mode 2014-03-25 08:10 202人阅读 评论(0) 收藏
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...
- Ubuntu vim+ ctags(包含系统函数) + taglist 配置 分类: vim ubuntu 2015-06-09 18:19 195人阅读 评论(0) 收藏
阅读大型代码,我们经常需要打开很多的代码文件,搜索各种定义.windows下用惯了ide的朋友,转战Linux的时候可能会觉得很难受,找不到合适的阅读工具.其实万能的vim就可以实现.下面介绍一下vi ...
- 跨服务器备注SQL数据库 分类: SQL Server 2015-03-05 08:52 227人阅读 评论(0) 收藏
任务:把服务器1上的SQL数据库自动备份到服务器2上,命名格式=数据库名+年月日+小时. 说明: 服务器2=>192.168.0.22 数据库名=>Book 共享文件夹路径:192.168 ...
- SQL Server阻止了对组件xp_cmdshell过程的解决方案 分类: SQL Server 2015-03-05 08:31 305人阅读 评论(0) 收藏
SQL Server阻止了对组件xp_cmdshell过程的解决方案 错误描述:SQL Server阻止了对组件'xp_cmdshell'的过程'sys.xp_cmdshell'的访问.因为此组件已作 ...
- iOS搜索框UISearchBar 分类: ios技术 2015-04-03 08:55 82人阅读 评论(0) 收藏
当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...
- 网络请求工具--AFNetworking 分类: ios技术 2015-02-03 08:17 76人阅读 评论(0) 收藏
在我们开发过程中,网络请求是必不可少的,对于网络框架,现在主流的大概只有三类:ASI框架: HTTP终结者(已经停止更新了),MKNetworkKit ,AFN.今天我就来浅谈一下这个AFN AFNe ...
随机推荐
- Three.js基础探寻九——网格
在学习了几何形状和材质之后,我们就能使用他们来创建物体了.最常用的一种物体就是网格(Mesh),网格是由顶点.边.面等组成的物体:其他物体包括线段(Line).骨骼(Bone).粒子系统(Partic ...
- [转]java工程师成神之路
转载http://www.hollischuang.com/archives/489https://linux.cn/article-6739-1.html 一.基础篇 1.1 JVM 1.1.1. ...
- 经常遇到Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be e
遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...
- 。。。Hibernate注解配置的注意事项。。。
今天本来打算录视频的,突然遇到一个拦路虎,Hibernate注解配置,有一个注意点:要么都在属性上面注解配置,要么都在getXX()方法上面用注解配置,要不然就会报错: Caused by: org. ...
- ASP.NET 中通过Form身份验证 来模拟Windows 域服务身份验证的方法
This step-by-step article demonstrates how an ASP.NET application can use Forms authentication to ...
- Swift 注释
注释就是对代码的解释和说明.目的是为了让别人和自己很容易看懂.为了让别人一看就知道这段代码是做什么用的. 正确的程序注释一般包括序言性注释和功能性注释.序言性注释的主要内容包括模块的接口.数据的描述和 ...
- xcode简介
Xcode 是苹果公司开发的编程软件,是开发人员建立OS X 和 iOS 应用程序的最快捷方式.Xcode 具有统一的用户界面设计,编码.测试.调试都在一个简单的窗口内完成. Xcode前身是继承自N ...
- selenium+phantomJS学习使用记录
背景知识: phantomjs是一个基于webkit的没有界面的浏览器,所以运行起来比完整的浏览器要高效. selenium是一个测试web应用的工具,目前是2.42.1版本,和1版的区别在于2.0+ ...
- mysql单表查询&&多表查询(职员表14+9)
dept(deptno,dname,loc) emp(empno,ename,job,mgr,hiredate,sal,COMM,deptno) salgrade(grade,losal,hisal) ...
- java运算符优先级记忆口诀
尊重原创:(口诀)转自http://lasombra.iteye.com/blog/991662 今天看到<java编程思想>中的运算符优先级助记口诀,不过"Ulcer Addi ...