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. elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解

    一.快速入门1. 查看集群的健康状况http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 状 ...

  2. Bellman-Ford与SPFA

    一.Bellman-Ford Bellman-Ford 算法是一种用于计算带权有向图中单源最短路径(当然也可以是无向图).与Dijkstra相比的优点是,也适合存在负权的图. 若存在最短路(不含负环时 ...

  3. 改变console.log的输出样式

    console.log允许你通过css来格式化输出,格式如下: console.log(‘%c字符串%c字符串’, 样式1, [样式2]) 其中”%c”为模板字符串 例子: 1 console.log ...

  4. iview table里面 插入下拉列表组件(自定义组件)一定要加key,不加key,table开始会加载所有数据,然后再从第2页点回第一页,就会走onChange事件,混乱的逻辑,切记加:key

    iview table里面 插入下拉列表组件(自定义组件)一定要加key,不加key,table开始会加载所有数据,然后再从第2页点回第一页,就会走onChange事件,混乱的逻辑,切记加:key 关 ...

  5. java 正则表达式如何提取中文的问题

    String regex="([\u4e00-\u9fa5]+)"; String str="132更新至456"; Matcher matcher = Pat ...

  6. Lucene入门基础教程

    http://www.linuxidc.com/Linux/2014-06/102856.htm

  7. React项目搭建及依赖安装

    一.前提 首先保证node.js已安装完成... 安装完成后,打开cmd命令行,输入 node -v 和 npm -v 来查看版本号,如果显示则安装完成. 二.安装react脚手架 在cmd命令行中输 ...

  8. 【OS_Linux】Linux 基本命令整理

    1. 查看目录文件:ls2. 打印当前工作目录:pwd3. 查看文件内容:cat 文件名4. 打开编辑器:vim 文件名 1 2 3 4 5 修改:按Insert键 退出修改模式:按Esc 键 进入输 ...

  9. Ubuntu sudo 出现 is not in the sudoers file解决方案

    前言: 自己想额外创建一个Linux账户,但是发现新创建的用户(lgq)并不能使用sudo指令. 但是在安装系统时创建的用户(abc)是可以正常使用的. 原因是新创建的用户并没有被赋予使用sudo指令 ...

  10. python 游戏(滑动拼图Slide_Puzzle)

    1. 游戏功能和流程图 实现16宫格滑动拼图,实现3个按钮(重置用户操作,重新开始游戏,解密游戏),后续难度,额外添加重置一次的按钮,解密算法的植入,数字改变为图片植入 游戏流程图 2. 游戏配置 配 ...