Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11015   Accepted: 4744

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

题意是求从S到E的最短路,有意思的是这次还要求两个问题,一个是沿着左手边走的最短路,一个是沿着右手边走的最短路。

仔细思考的话这题不难,就是自己感觉写得比较繁琐,分各种情况和方向考虑,应该可以优化成更简便的代码。

另外这个题被分到深搜里面,我一开始写的也是深搜,但最终结果是广搜更简单一些嘛。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#pragma warning(disable:4996)
using namespace std; #define up 1
#define down 2
#define left 3
#define right 4 int row,col;
char value[50][50];
int bushu[50][50]; int dfs_left(int i,int j,int dir)
{
memset(bushu,0,sizeof(bushu));
queue<int>x;
queue<int>y;
queue<int>z; x.push(i);
y.push(j);
z.push(dir); int temp1,temp2,temp3;
while(x.size())
{
i=x.front();
j=y.front();
temp3=z.front(); x.pop();
y.pop();
z.pop(); if(temp3==up)
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1; }
else if(value[i][j-1]=='#')
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
}
}
} }
else if(temp3==down)
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1; }
else if(value[i][j+1]=='#')
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==left)
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1; }
else if(value[i+1][j]=='#')
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==right)
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1; }
else if(value[i-1][j]=='#')
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
}
}
}
}
}
return 0;
} int dfs_right(int i,int j,int dir)
{
memset(bushu,0,sizeof(bushu));
queue<int>x;
queue<int>y;
queue<int>z; x.push(i);
y.push(j);
z.push(dir); int temp1,temp2,temp3;
while(x.size())
{
i=x.front();
j=y.front();
temp3=z.front(); x.pop();
y.pop();
z.pop(); if(temp3==up)
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
}
}
} }
else if(temp3==down)
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1; }
else if(value[i][j-1]=='#')
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==left)
{
if(value[i-1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1; }
else if(value[i-1][j]=='#')
{
if(value[i][j-1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
else if(value[i][j-1]=='#')
{
if(value[i+1][j]=='E')
return bushu[i][j]+1;
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j+1]=='E')
return bushu[i][j]+1;
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
}
}
}
}
else if(temp3==right)
{
if(value[i+1][j]=='E')
{
return bushu[i][j]+1;
}
if(value[i+1][j]=='.')
{
x.push(i+1);
y.push(j);
z.push(down);
bushu[i+1][j]=bushu[i][j]+1;
}
else if(value[i+1][j]=='#')
{
if(value[i][j+1]=='E')
{
return bushu[i][j]+1;
}
if(value[i][j+1]=='.')
{
x.push(i);
y.push(j+1);
z.push(right);
bushu[i][j+1]=bushu[i][j]+1;
}
else if(value[i][j+1]=='#')
{
if(value[i-1][j]=='E')
return bushu[i][j]+1;
if(value[i-1][j]=='.')
{
x.push(i-1);
y.push(j);
z.push(up);
bushu[i-1][j]=bushu[i][j]+1;
}
else if(value[i-1][j]=='#')
{
if(value[i][j-1]=='E')
return bushu[i][j]+1;
if(value[i][j-1]=='.')
{
x.push(i);
y.push(j-1);
z.push(left);
bushu[i][j-1]=bushu[i][j]+1;
}
}
}
}
}
}
return 0;
} int dfs(int i,int j,int dir)
{
memset(bushu,0,sizeof(bushu));
queue<int>x;
queue<int>y; x.push(i);
y.push(j); while(x.size())
{
i=x.front();
j=y.front(); x.pop();
y.pop(); if(value[i+1][j]=='.'&&!bushu[i+1][j])
{
x.push(i+1);
y.push(j);
bushu[i+1][j]=bushu[i][j]+1;
}
if(value[i][j+1]=='.'&&!bushu[i][j+1])
{
x.push(i);
y.push(j+1);
bushu[i][j+1]=bushu[i][j]+1;
}
if(value[i-1][j]=='.'&&!bushu[i-1][j])
{
x.push(i-1);
y.push(j);
bushu[i-1][j]=bushu[i][j]+1;
}
if(value[i][j-1]=='.'&&!bushu[i][j-1])
{
x.push(i);
y.push(j-1);
bushu[i][j-1]=bushu[i][j]+1;
}
if(value[i+1][j]=='E'||value[i][j+1]=='E'||value[i-1][j]=='E'||value[i][j-1]=='E')
return bushu[i][j]+1;
}
return 0;
} void solve()
{
int i,j;
for(i=2;i<col;i++)
{
if(value[1][i]=='S')
{
cout<<dfs_left(1,i,down)+1<<" ";
cout<<dfs_right(1,i,down)+1<<" ";
cout<<dfs(1,i,down)+1<<endl;
return;
}
} for(i=2;i<col;i++)
{
if(value[row][i]=='S')
{
cout<<dfs_left(row,i,up)+1<<" ";
cout<<dfs_right(row,i,up)+1<<" ";
cout<<dfs(row,i,up)+1<<endl;
return;
}
}
for(i=2;i<row;i++)
{
if(value[i][1]=='S')
{
cout<<dfs_left(i,1,right)+1<<" ";
cout<<dfs_right(i,1,right)+1<<" ";
cout<<dfs(i,1,right)+1<<endl;
return;
}
}
for(i=2;i<row;i++)
{
if(value[i][col]=='S')
{
cout<<dfs_left(i,col,left)+1<<" ";
cout<<dfs_right(i,col,left)+1<<" ";
cout<<dfs(i,col,left)+1<<endl;
return;
}
} } int main()
{
int Test,i,j;
scanf("%d",&Test); while(Test--)
{
scanf("%d%d",&col,&row);
for(i=1;i<=row;i++)
{
scanf("%s",value[i]+1);
}
solve();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3083:Children of the Candy Corn的更多相关文章

  1. POJ 3083:Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

  2. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  3. Children of the Candy Corn 分类: POJ 2015-07-14 08:19 7人阅读 评论(0) 收藏

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10933   Acce ...

  4. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  5. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

  6. POJ3083——Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are ...

  7. poj3083 Children of the Candy Corn BFS&&DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  8. K - Children of the Candy Corn(待续)

    K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d ...

  9. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

随机推荐

  1. memset函数总结

    之前有一个程序栽在了memset函数上面,对memset函数一直耿耿于怀,于是想总结一下这个常用但是总出错的函数. memset在string.h文件中是这么定义的: void*代表这个函数的返回值可 ...

  2. simplecheck

    环境:win10 工具:jadx .夜神模拟器.pycharm 下载好了之后加载到模拟器 输入正确的flag验证 加载到jadx.查看MainAtivity if语句进行验证是否正确,如果正确就输出y ...

  3. ROS-1 : Ubuntu16.04中安装ROS Kinetic

    1.安装 ROS Kinetic仅支持Wily(Ubuntu 15.10).Xenial( Ubuntu16.04)和Jessie(Debian 8)的debian软件包. 1.1 配置Ubuntu ...

  4. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-pause

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  5. Docker 搭建开源 CMDB平台 之 “OpsManage”

    说明:  我一次build 完 所以images 包 有1G多   可分层build   bash 环境一层   应用程序及启动脚本(shell.sh) 一层 步骤:           1     ...

  6. 对上一篇Logstash的补充

    主要补充内容: 1.同步多表 2.配置的参数个别说明 3.elasticsearch的"_id"如果有相同的,那么会覆盖掉,相同"_id"的数据只会剩下最后一条 ...

  7. 剑指offer自学系列(三)

    题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变,例如{5,1,4,2 ...

  8. 【php】PHP现代框架代表-Laravel框架核心技术特性

    一.php语言特点及发展现状 1.服务端脚本语言,自由度太大 ,一个业务逻辑可言写在模型里,控制器里,也可以单独封装成一个类,甚至可以嵌入到html里,这样势必会造成混乱,业务逻辑散落在各处,尤其对于 ...

  9. 2018年Android面试题含答案--适合中高级(下)(转)

    这里是我整理出来的 面试题,答案我花了很久的时间.加上我自己的理解整理出来的,作者不易,请谅解.有答案的的:https://xiaozhuanlan.com/topic/6132940875   1. ...

  10. objectarx 批量倒角

    这个插件支持AcDbPolyline的倒角,封闭的和没封闭的都可以.刚开始做的时候,发现倒一个角借助云幽课堂里的代码就可以做,后来做一条从左开始画的非封闭的多段线,发现向上凹和向下凹的角不能同时被倒, ...