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;
}

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

最新文章

  1. iOS当中一些常见的面试题
  2. spring2.0包说明【转】
  3. iOS 制作自动打包脚本 Xcode8.3.2
  4. 模板引擎ejs入门学习
  5. 洛谷 P3962 [TJOI2013]数字根 解题报告
  6. 20170720 Celery 异步任务处理到Sql Server 发生死锁
  7. java virtual machine launcher Error:Could not create the Java Virtual Machine. Error:A Fatal exception has occurred,Program will exit.
  8. echart中间显示固定的字
  9. Nodejs学习笔记2
  10. 逆袭之旅DAY16.东软实训.Oracle.索引
  11. Hibernate 再接触 继承映射
  12. 简单记录常用git 命令
  13. Java基础——死锁
  14. php bccomp的替换函数
  15. JDK Logger 简介 (zhuan)
  16. 问答项目---用户注册的那些事儿(JS验证)
  17. 第2课:jmeter总结、Charles抓包
  18. Calculator Part Ⅰ
  19. BZOJ4871 Shoi2017摧毁“树状图”(树形dp)
  20. CentOS 不间断会话(ssh关闭后如何保证程序继续运行)(nohup和screen)

热门文章

  1. spingboot2.0外部引入xml配置文件时找不到文件等报错
  2. other#一些问题的列表
  3. Metasploit学习笔记——Web应用渗透技术
  4. Day6 - B - 采花 HYSBZ - 2743
  5. jpa自定义sql语句
  6. HTML 5 &lt;blockquote&gt;&lt;p&gt;的分工与合作
  7. canvas绘制表盘时钟
  8. 树莓派—raspbian软件源
  9. java 根据值获取枚举对象
  10. UVA - 816 Abbott&#39;s Revenge(bfs)