Children of the Candy Corn

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 55
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,m;
int c,c1,c2;
char s[N][N];
int cc[N][N];
int dirx[]={,-,,};
int diry[]={-,,,};
int flag; typedef struct
{
int x;
int y;
int now;
int dir;
}PP; PP start,end; void ini()
{
int i,j;
c=c1=c2=;
//memset(cc,0,sizeof(cc));
scanf("%d%d",&m,&n);
for(int i=;i<n;i++){
scanf("%s",s[i]);
}
for(i=;i<n;i++){
for(j=;j<m;j++){
cc[i][j]=;
if(s[i][j]=='S'){
start.x=i;
start.y=j;
start.now=;
}
if(s[i][j]=='E'){
end.x=i;
end.y=j;
}
}
}
} void solve()
{ } int isok(PP o,PP pre)
{
if( o.x>= && o.x<n && o.y>= && o.y<m && s[o.x][o.y]!='#' && cc[o.x][o.y]>pre.now+)
{
o.now=pre.now+;
cc[o.x][o.y]=o.now;
return o.now;
}
return ;
} void bfs()
{
int i;
PP te,next;
queue<PP> q;
start.now=;
q.push(start);
while(q.size()>){
te=q.front();
q.pop();
// printf(" %d %d %d\n",te.x,te.y,te.now);
for(i=;i<;i++){
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(isok(next,te)!=){
next.now=te.now+;
q.push(next);
}
}
}
c=cc[end.x][end.y];
} int ok(PP o)
{
if( o.x>= && o.x<n && o.y>= && o.y<m && s[o.x][o.y]!='#' )
{
return ;
}
return ;
} void dfs1(PP te)
{
//flag=0;
// printf(" %d %d %d %d\n",te.x,te.y,te.dir,te.now);
int i;
PP next;
if(te.x==start.x && te.y==start.y){
for(i=;i<;i++){
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
}
}
} if(te.x==end.x && te.y==end.y){
c1=te.now;
flag=;
return;
} if(flag==) return;
// for(int k=t;k<4;k++){
i=te.dir-;
if(i<) i+=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} if(flag==) return; i=te.dir;
// if(i<0) i+=4;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} if(flag==) return;
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} if(flag==) return;
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs1(next);
} return;
// } } void dfs2(PP te)
{
//flag=0;
// printf(" %d %d %d %d\n",te.x,te.y,te.dir,te.now);
int i;
PP next;
if(te.x==start.x && te.y==start.y){
for(i=;i<;i++){
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
}
}
} if(te.x==end.x && te.y==end.y){
c2=te.now;
flag=;
return;
} if(flag==) return;
// for(int k=t;k<4;k++){
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} if(flag==) return; i=te.dir;
// if(i<0) i+=4;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} if(flag==) return;
i=te.dir-;
if(i<) i+=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} if(flag==) return;
i=te.dir+;
if(i>=) i-=;
next.x=te.x+dirx[i];
next.y=te.y+diry[i];
if(ok(next)!=){
next.now=te.now+;
next.dir=i;
dfs2(next);
} return;
// }
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
//while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
flag=;
dfs1(start);
flag=;
dfs2(start);
bfs();
printf("%d %d %d\n",c1,c2,c);
} return ;
}

poj 3083 dfs,bfs的更多相关文章

  1. 小鼠迷宫问题【sdut1157】【dfs,bfs综合题目】

    小鼠迷宫问题 Time Limit: 1500ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 小鼠a与小鼠b身处一个m×n的迷宫中,如图所示.每一个方格表示迷宫中 ...

  2. 递归,回溯,DFS,BFS的理解和模板【摘】

    递归:就是出现这种情况的代码: (或者说是用到了栈) 解答树角度:在dfs遍历一棵解答树 优点:结构简洁缺点:效率低,可能栈溢出 递归的一般结构: void f() { if(符合边界条件) { // ...

  3. 邻接矩阵实现图的存储,DFS,BFS遍历

    图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...

  4. 判断图连通的三种方法——dfs,bfs,并查集

    Description 如果无向图G每对顶点v和w都有从v到w的路径,那么称无向图G是连通的.现在给定一张无向图,判断它是否是连通的. Input 第一行有2个整数n和m(0 < n,m < ...

  5. 递归,回溯,DFS,BFS的理解和模板

    LeetCode 里面很大一部分题目都是属于这个范围,例如Path Sum用的就是递归+DFS,Path Sum2用的是递归+DFS+回溯 这里参考了一些网上写得很不错的文章,总结一下理解与模板 递归 ...

  6. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  7. 迷宫问题(DFS,BFS)

    /******************************** 啊哈!算法 深度优先搜索算法 迷宫问题 输入: 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 ...

  8. UVA10410 TreeReconstruction 树重建 (dfs,bfs序的一些性质,以及用栈处理递归 )

    题意,给你一颗树的bfs序和dfs序,结点编号小的优先历遍,问你可能的一种树形: 输出每个结点的子结点. 注意到以下事实: (1)dfs序中一个结点的子树结点一定是连续的. (2)bfs,dfs序中的 ...

  9. dfs,bfs的二分匹配模板(模板题hdu1150)

    如果不懂匈牙利算法,请点击:该趣味算法http://blog.csdn.net/dark_scope/article/details/8880547 模板: //DFS版本下的二分匹配算法 http: ...

随机推荐

  1. C++类构造函数、析构函数运行机理

    http://blog.sina.com.cn/s/blog_6fd68d5f0100n60h.html 前言--构造函数.析构函数的简单理解:1)构造函数---对象被创建时候调用的函数:2)析构函数 ...

  2. 分布式锁----浅析redis实现

    引言大概两个月前小伙伴问我有没有基于redis实现过分布式锁,之前看redis的时候知道有一个RedLock算法可以实现分布式锁,我接触的分布式项目要么是github上开源学习的,要么是小伙伴们公司项 ...

  3. PHP框架深度解析

    PHP成为世界上最流行的脚本语言有许多原因:灵活性,易用性等等.但通常只用PHP或者其他语言编码就会显得单调.重复,这时候就需要一个PHP框架来代替程序员完成那些重复不变的部分.本文通过回答What, ...

  4. Luogu P3627 抢掠计划

    题目传送门 \(Tarjan\)缩点+SPFA最长路 #include<iostream> #include<cstdio> #include<cstring> # ...

  5. Bootstrap图片支持响应式

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. ASIHTTPRequest简单学习

    ASIHTTPRequest框架是优秀的第三方Objective-C的HTTP框架,支持Mac OS X和iOS下的HTTP开发. 一.ASIHTTPRequest框架的安装和配置 (1)首先要在项目 ...

  7. swift中使用sqlite3

    import Foundation /** 1. 打开数据库 2. 如果没有数据表,需要首先创表 3. 数据操作 */ class SQLite { var db: COpaquePointer = ...

  8. js的工厂模式

    工厂模式: 什么叫工厂模式,工厂就是大家大脑里想的那样,一个流水线作业的一个东西,只需要满足刚需就可以了,一个人,只干一件事,最后串起来,就是一个事件. 首先需要说一下工厂模式.工厂模式根据抽象程度的 ...

  9. CSS在线压缩

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. [LUOGU] P2543 [AHOI2004]奇怪的字符串

    LCS //Writer:GhostCai && His Yellow Duck #include<iostream> #include<cstring> #d ...