Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10933   Accepted: 4708

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.




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

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters
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

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single
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) 收藏的更多相关文章

  1. Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏

    效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  2. 浅谈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 ...

  3. Binary Indexed Tree 2D 分类: ACM TYPE 2014-09-01 08:40 95人阅读 评论(0) 收藏

    #include <cstdio> #include <cstdlib> #include <climits> #include <cstring> # ...

  4. Brush Mode --- Nyoj 737 分类: Brush Mode 2014-03-25 08:10 202人阅读 评论(0) 收藏

    石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...

  5. Ubuntu vim+ ctags(包含系统函数) + taglist 配置 分类: vim ubuntu 2015-06-09 18:19 195人阅读 评论(0) 收藏

    阅读大型代码,我们经常需要打开很多的代码文件,搜索各种定义.windows下用惯了ide的朋友,转战Linux的时候可能会觉得很难受,找不到合适的阅读工具.其实万能的vim就可以实现.下面介绍一下vi ...

  6. 跨服务器备注SQL数据库 分类: SQL Server 2015-03-05 08:52 227人阅读 评论(0) 收藏

    任务:把服务器1上的SQL数据库自动备份到服务器2上,命名格式=数据库名+年月日+小时. 说明: 服务器2=>192.168.0.22 数据库名=>Book 共享文件夹路径:192.168 ...

  7. SQL Server阻止了对组件xp_cmdshell过程的解决方案 分类: SQL Server 2015-03-05 08:31 305人阅读 评论(0) 收藏

    SQL Server阻止了对组件xp_cmdshell过程的解决方案 错误描述:SQL Server阻止了对组件'xp_cmdshell'的过程'sys.xp_cmdshell'的访问.因为此组件已作 ...

  8. iOS搜索框UISearchBar 分类: ios技术 2015-04-03 08:55 82人阅读 评论(0) 收藏

    当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...

  9. 网络请求工具--AFNetworking 分类: ios技术 2015-02-03 08:17 76人阅读 评论(0) 收藏

    在我们开发过程中,网络请求是必不可少的,对于网络框架,现在主流的大概只有三类:ASI框架: HTTP终结者(已经停止更新了),MKNetworkKit ,AFN.今天我就来浅谈一下这个AFN AFNe ...

随机推荐

  1. java io读书笔记(4)字符数据

    Number只是java程序中需要读出和写入的一种数据类型.很多java程序需要处理有一大堆的字符组成的text,因为计算机真正懂得的只有数字,因此,字符按照某种编码规则,和数字对应. 比如:在ASC ...

  2. 安装windowbuilder错误一例

    eclipse是3.7版本,安装了windowbuilder,大致步骤如下: http://www.cnblogs.com/gladto/archive/2011/07/21/2112836.html ...

  3. A*啦啦啦

    ...A*是个啥都不知道.. 大家注意K短路可能不存在!!!! 果然是s==t的问题……加个if(s==t) k++就A了…… 单用Dij,tle到死 原来是单向k短路........开始以为是双向的 ...

  4. 最新RubyMine2016.2开发Ruby ON Rails(ROR)程序的流程

    1.RubyMine新建ROR工程 File->New Project     选择Rails下的"New Application"     点击OK 后生成ROR项目   ...

  5. C++之路进阶——边表

    边表:利用边的关系来表示一个图. 用到数组: head//head[i]表示从i点出发的第一条边的编号; next[i]//与第i条边起点相同的下一条边的编号; a[i]//第i条边的终点; val[ ...

  6. android中在代码中设置margin属性

    1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...

  7. js获取url的参数值

    var match = new RegExp('[?&]voucherSn=([^&]*)').exec("http://m.v3beta.tootoo.cn/index.p ...

  8. 我使用的vim配置文件

    各种搜,拼凑出了这么一个配置文件,以下是文件的内容 syntax onfiletype onset linespace=0set rulerset nocompatibleset confirmset ...

  9. javascript 正则表达式(二)

    /* 正则表达式方法:test(),exec(),String对象方法:match(),search(),replace(),split() 1.test()方法: 用法:  regexp对象实例.t ...

  10. HTML5动画软件工具编辑器 HTML5动画分类 工具推荐

    接下来介绍几款制作HTML5动画的工具,它们可以分为几类: 1.导出canvas动画: Flash CC(13.1).Animation.Radi 2.导出DIV+CSS3动画: HTML5 Make ...