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. mac 上使用移动硬盘

    1. 打开终端,查看赢盘的Volume Name diskutil list 2. 更新fstab文件,此步骤需要输入密码 sudo nano /etc/fstab 3. 在fstab文件中写入一下内 ...

  2. C-基础:表达式中存在有符号类型和无符号类型时,都自动转换为无符号类型

    void foo(void) { unsigned ; ; (a+b > ) puts("> 6") : puts("<= 6"); } 答案 ...

  3. call和apply方法的异同

    基本作用:改变对象的执行上下文. this指向执行上下文.(执行环境) this指向的永远是调用该方法的对象 function func(){ this.a=1; console.log(this.a ...

  4. iOS 优秀博客

    中文 iOS/Mac 开发博客列表 GitHub 上排名前 100 的 Objective-C 项目简介 GitHub 上都有哪些值得关注学习的 iOS 开源项目? iOS开发系列文章(持续更新……) ...

  5. http post get 同步异步

    下面首先介绍一下一些基本的概念---同步请求,异步请求,GET请求,POST请求. 1.同步请求从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作.也 ...

  6. 牛客网NOIP赛前集训营-普及组(第二场)和 牛客网NOIP赛前集训营-提高组(第二场)解题报告

    目录 牛客网NOIP赛前集训营-普及组(第二场) A 你好诶加币 B 最后一次 C 选择颜色 D 合法括号序列 牛客网NOIP赛前集训营-提高组(第二场) A 方差 B 分糖果 C 集合划分 牛客网N ...

  7. 初涉k-d tree

    听说k-d tree是一个骗分的好东西?(但是复杂度差评??? 还听说绍一的kdt常数特别小? KDT是什么 KDT的全称是k-degree tree,顾名思义,这是一种处理多维空间的数据结构. 例如 ...

  8. Linux下安装以及使用MySQL数据库

    1.官网下载mysql数据库:https://dev.mysql.com/downloads/mysql/ 2.linux 下可直接下载:wget https://cdn.mysql.com//Dow ...

  9. MySQL的索引知识

    一.什么是索引. 索引是用来加速查询的技术的选择之一,在通常情况下,造成查询速度差异 的因素就是索引是否使用得当.当我们没有对数据表的某一字段段或者多个 字段添加索引时,实际上执行的全表扫描操作,效率 ...

  10. JavaWeb项目中集成Swagger API文档

    1.增加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...