Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8120   Accepted: 3547

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<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std; struct node
{
int x,y;
int step;
};
int n,m;
char map[][];
int vis[][];
int dir[][] = {{-,},{,-},{,},{,}};//上左下右顺时针;
queue <node> que;
int ans3; int dfs(int sx,int sy, int ex, int ey, int d, int ans)
{
if(sx == ex && sy == ey)
return ans;
d = (d+)%;//左转;
while(map[sx+dir[d][]][sy+dir[d][]] == '#')
d = (d+)%;//当有墙的时候就右转直到没有墙;
return dfs(sx+dir[d][],sy+dir[d][],ex,ey,d,ans+);
} void bfs(int sx,int sy, int ex, int ey)
{
while(!que.empty())
que.pop();
memset(vis,,sizeof(vis)); que.push((struct node){sx,sy,});
vis[sx][sy] = ; while(!que.empty())
{
struct node u = que.front();
que.pop();
if(u.x == ex && u.y == ey)
{
ans3 = u.step;
return;
}
if(u.x- >= && !vis[u.x-][u.y] && map[u.x-][u.y] != '#')
{
vis[u.x-][u.y] = ;
que.push((struct node){u.x-,u.y,u.step+});
}
if(u.x+ <= n && !vis[u.x+][u.y] && map[u.x+][u.y] != '#')
{
vis[u.x+][u.y] = ;
que.push((struct node){u.x+,u.y,u.step+});
}
if(u.y- >= && !vis[u.x][u.y-] && map[u.x][u.y-] != '#')
{
vis[u.x][u.y-] = ;
que.push((struct node){u.x,u.y-,u.step+});
}
if(u.y+ <= m && !vis[u.x][u.y+] && map[u.x][u.y+] != '#')
{
vis[u.x][u.y+] = ;
que.push((struct node){u.x,u.y+,u.step+});
}
}
}
int main()
{
int t;
int sx,sy,ex,ey;
scanf("%d",&t);
while(t--)
{
memset(map,'#',sizeof(map));//外面加一道墙,从s进去之后只有一条路可走;
scanf("%d %d",&m,&n);
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
cin>>map[i][j];
if(map[i][j] == 'S')
{
sx = i;
sy= j;
}
if(map[i][j] == 'E')
{
ex = i;
ey = j;
}
}
}
printf("%d %d ",dfs(sx,sy,ex,ey,,),dfs(ex,ey,sx,sy,,));
bfs(sx,sy,ex,ey);
printf("%d\n",ans3);
}
return ;
}
												

最新文章

  1. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest
  2. 【转】aspx与aspx.cs的关系
  3. Objective-C 利用OC的消息机制,使用Method Swizzling进行方法修改
  4. input上传按钮 文字修改办法
  5. 解决hibernate向mysql插入中文乱码问题
  6. Windows 10正式版官方原版ISO镜像下载
  7. NSFileManager文件管理
  8. Codeforces Round #226 (Div. 2)A. Bear and Raspberry
  9. 【HDOJ】2389 Rain on your Parade
  10. source code of MES Data
  11. Dollar Dayz poj3181
  12. MSIL实用指南-加载bool、sbyte、byte、char、short等值
  13. (NO.00004)iOS实现打砖块游戏(八):游戏中小球与砖块的碰撞
  14. Android开发工具下载地址
  15. 遗传算法解决TSP问题实现以及与最小生成树的对比
  16. c# MD5及盐值加密
  17. python小总结3(异常、单例设计模式)
  18. NMON使用以及nmon_analyse生成分析报表
  19. 【推荐】Hutool 的通用工具类库
  20. ADO.NET的五大对象【转】

热门文章

  1. Android系统中设置TextView的行间距(非行高)
  2. spring定时器任务多任务串行执行问题排查
  3. IDL绘制黑体辐射曲线
  4. ubuntu wine卸载程序并删除图标
  5. Python 代码实现模糊查询
  6. new Date()在IE,谷歌,火狐上的一些注意项
  7. OD: Windows Driver Fuzz
  8. 我的vimrc配置
  9. Spring3.0提供的表达式语言spel
  10. android异常之emulator-arm.exe已停止工作