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 ...
随机推荐
- php实用类
<?php class DBDA { public $host="localhost";//服务器地址 public $uid="root";//用户名 ...
- Excel操作增强包
一.前期准备1) pywin32安装包(根据系统要求选择32位装包还是64位安装包)2) xlwings安装包二.安装过程1) pywin32安装包为exe文件,直接进行安装即可2) xlwi ...
- JQuery权限管理
<title></title> <script src="JS/jquery-1.7.1.js"></script> <scr ...
- [转] java编程规范
原文链接: 资料推荐--Google Java编码规范 之前已经推荐过Google的Java编码规范英文版了: http://google-styleguide.googlecode.com/svn/ ...
- paper 44 :颜色矩和颜色相关图(color correlogram)
- 夺命雷公狗---Thinkphp----2之快快速搭建TP环境
<?php //定义项目目录 define('APP_PATH','./WEB/'); //开启调试 define('APP_DEBUG',True); //包含thinkphp项目入口文件 r ...
- dbms_sql包的用法
http://blog.itpub.net/20948385/viewspace-691398 对于一般的select操作,如果使用动态的sql语句则需要进行以下几个步骤: open cursor ...
- 【php】目录、路径和文件 操作
目录操作 解析路径: basename() - 返回路径的文件名部分 获取目录部分: dirname() - 返回路径的目录部分 路径信息: pathinfo() - 返回数组(目录名,基本名,扩展名 ...
- C#和JavaScript交互(asp.net前台和后台互调)总结 (转)
http://www.cnblogs.com/poleices/archive/2011/02/24/1963727.html C#代码与javaScript函数的相互调用: 1.如何在JavaScr ...
- C#中Attribute的继承
在C#中Attribute是个非常有用的语法,本文不会介绍Attribute的使用方法,如果想了解Attribute的详细信息请查阅MSDN及网上相关文档.C#中的Attribute有两个地方是和继承 ...