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 ...
随机推荐
- [c++基本语法]——构造函数初始化列表
c++构造函数初始化成员变量列表: #pragma once class Node { public: int data; // 权值 Node *parent; // 父节点 Node *left; ...
- leetcode96 Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- sql set xact_abort on 用例
set xact_abort on 设置事务回滚的当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚为OFF时,只回滚错误的地方 例子 : ALTER proc [dbo].[BuC ...
- ETM and PTM
ETM:embedded Trace Macrocell PTM:Program Flow Trace Macrocell ETM-A7 macrocell提供Cortex-A7 MPcore的ins ...
- C#中Attribute的继承
在C#中Attribute是个非常有用的语法,本文不会介绍Attribute的使用方法,如果想了解Attribute的详细信息请查阅MSDN及网上相关文档.C#中的Attribute有两个地方是和继承 ...
- Watir资源列表【转】
Watir简介 "Watir" (发音与 water相近) 全写是 "Web Application Testing in Ruby".Watir是一款用Rub ...
- Junit单步调试
单步调试:主要查看变量内容的变化 1.设置断点位置,设置在可能出现问题的代码 2.点击项目右键以Debug as方式运行程序 3.F5 --> step into 进入方法内部进行调试 ...
- 图解IoC 依赖注入
- linux端口
1.查看开放的端口 netstat -anp 来查看哪些端口被打开. 注:加参数'-n'会将应用程序转为端口显示,即数字格式的地址,如:nfs->2049, ftp->21,因此可以开启两 ...
- scala伴生对象
package com.test.scala.test /** * 伴生对象指的是在类中建立一个object */ class AssociatedObject { private var count ...