Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
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
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 ;
}