Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11011 | Accepted: 4743 |
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.
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.
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
Source
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
char mp[50][50];
bool vis[50][50];
int m,n;
int xx,xy,yx,yy;
int chx[]= {-1,0,1,0}; //rr; you
int chy[]= {0,1,0,-1};
int chlx[]= {-1,0,1,0}; //zuo;
int chly[]= {0,-1,0,1};
int k;
struct node
{
int x,y;
int step;
};
int bfs(int x ,int y)
{
node now,tmp;
memset(vis,0,sizeof(vis));
now.x=x;
now.y=y;
now.step=1;
vis[x][y]=1;
queue<node>Q;
Q.push(now);
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(mp[now.x][now.y]=='E')
{
return now.step;
}
for(int i=0; i<4; i++)
{
tmp=now;
tmp.x+=chx[i];
tmp.y+=chy[i];
if(mp[tmp.x][tmp.y]!='#'&&!vis[tmp.x][tmp.y]&&tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m)
{
vis[tmp.x][tmp.y]=1;
tmp.step++;
Q.push(tmp);
}
}
}
return -1;
}
void DFS_LEFT(int sx,int se,int fx,int cnt)
{
if(mp[sx][se]=='E')
{
k=cnt;
return;
}
int x,y;
int point=(fx+1)%4;
x=sx+chlx[point];
y=se+chly[point];
if(x>=0 && x<n&&y>=0&&y<m &&mp[x][y]!='#')
{
DFS_LEFT(x,y,point,cnt+1);
return;
}
for(int i=4; i>1; i--)
{
point=(fx+i)%4;
x=sx+chlx[point];
y=se+chly[point];
if(x>=0 && x<n&&y>=0&&y<m &&mp[x][y]!='#')
{
DFS_LEFT(x,y,point,cnt+1);
break;
}
}
}
void DFS_R(int sx,int se,int fx,int cnt)
{
if(mp[sx][se]=='E')
{
k=cnt;
return;
}
int x,y;
int point=(fx+1)%4;
x=sx+chx[point];
y=se+chy[point];
if(x>=0 && x<n&&y>=0&&y<m &&mp[x][y]!='#')
{
DFS_R(x,y,point,cnt+1);
return;
}
for(int i=4; i>1; i--)
{
point=(fx+i)%4;
x=sx+chx[point];
y=se+chy[point];
if(x>=0 && x<n&&y>=0&&y<m &&mp[x][y]!='#')
{
DFS_R(x,y,point,cnt+1);
break;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=0; i<n; i++)
{
scanf("%s",mp[i]);
for(int j=0; j<m; j++)
{
if(mp[i][j]=='S')
{
xx=i;
xy=j;
}
if(mp[i][j]=='E')
{
yx=i;
yy=j;
}
}
}
mp[xx][xy]='#';
DFS_LEFT(xx,xy,0,1);
printf("%d",k);
DFS_R(xx,xy,0,1);
printf(" %d",k);
printf(" %d\n",bfs(xx,xy));
}
return 0;
}
L方向就是顺时针;R就是逆时针;但是显然这种方法太挫啦,
S-->E顺时针,S-->E逆时针(相当于E-->S顺时针),只需一个就可以啦,
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
int ch[][2]={{0,1},{-1,0},{0,-1},{1,0}};
char mp[50][50];
int n,m;
struct node
{
int x,y,step;
};
bool vis[50][50];
bool ok(int x,int y) //判断常识是否满足条件;
{
if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='#'&&mp[x][y]!='S'&&!vis[x][y]) //切记‘S’;
return 1;
return 0;
}
int DFS(int sx,int sy,int ex,int ey,int fx) //fx用来记录当前的方向;
{
if(sx==ex&&sy==ey)
return 1;
int xx,yy;
int tmp;
for(int i=5;i>1;i--) //尝试在走下一步时的方向;一个即可;
{
xx=sx,yy=sy,tmp=(i+fx)%4;
xx+=ch[tmp][0];
yy+=ch[tmp][1];
if(ok(xx,yy))
return DFS(xx,yy,ex,ey,tmp)+1;
}
return -1;
}
int BFS(int sx,int sy,int ex,int ey) //BFS 求最短;
{
queue<node>Q;
node tmp,now;
now.x=sx,now.y=sy;
now.step=1;
Q.push(now);
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(now.x==ex&&now.y==ey)
return now.step;
for(int i=0;i<4;i++)
{
tmp=now;
tmp.x+=ch[i][0];
tmp.y+=ch[i][1];
if(ok(tmp.x,tmp.y))
{
vis[tmp.x][tmp.y]=1;
tmp.step++;
Q.push(tmp);
}
}
}
return -1;
}
int main()
{
int t;
int sx,sy,ex,ey;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%s",mp[i]);
for(int j=0;j<m;j++)
{
if(mp[i][j]=='S')
{
sx=i,sy=j;
}
else if(mp[i][j]=='E')
{
ex=i,ey=j;
}
}
}
printf("%d ",DFS(sx,sy,ex,ey,0));
mp[sx][sy]='E';mp[ex][ey]='S';
printf("%d ",DFS(ex,ey,sx,sy,0));
mp[sx][sy]='S';mp[ex][ey]='E';
printf("%d\n",BFS(sx,sy,ex,ey));
}
return 0;
}