poj3083

来源:互联网 发布:删除网络映射驱动器 编辑:程序博客网 时间:2024/06/05 01:09

http://www.cnblogs.com/tanhehe/archive/2013/06/25/3154483.html

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12568 Accepted: 5404

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

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########

Sample Output

37 5 517 17 9

题意:

先沿着左边的墙从 S 一直走,求到达 E 的步数。

再沿着右边的墙从 S 一直走,求到达 E 的步数。

最后求最短路。

 

分析:

最短路好办,关键是沿着墙走不太好想。

但只要弄懂如何转,这题就容易了。

单就沿着左走看一下:

当前方向     检索顺序

     ↑ :      ← ↑ → ↓

    → :        ↑ → ↓ ← 

     ↓ :      → ↓ ← ↑ 

    ← :        ↓ ← ↑ → 

如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。

在DFS时,加一个参数,用来保存当前的方向。


#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<queue>using namespace std;const int maxn=110;int dx[]={0,-1,0,1};int dy[]={-1,0,1,0};int dl[][2]={{0,-1},{-1,0},{0,1},{1,0}};int dr[][2]={{0,1},{-1,0},{0,-1},{1,0}};int sx,sy,ex,ey,n,m;char G[maxn][maxn];struct pos{    int x,y,s;};int dfs(int x,int y,int d,int step,int dir[][2]){    for(int i=0;i<4;i++){        int j=((d-1+4)%4+i)%4;        int nx=x+dir[j][0];        int ny=y+dir[j][1];        if(nx==ex&&ny==ey)return step+1;        if(nx<0||ny<0||nx>n||ny>m)continue;        if(G[nx][ny]=='#')continue;        return dfs(nx,ny,j,step+1,dir);    }}int bfs(int sx,int sy){    bool vis[maxn][maxn];    memset(vis,false,sizeof(vis));    queue<pos> Q;    Q.push((pos){sx,sy,1});    vis[sx][sy]=true;    while(!Q.empty()){        pos p=Q.front();        Q.pop();        if(p.x==ex&&p.y==ey)return p.s;        pos np;        for(int d=0;d<4;d++){            np.x=p.x+dx[d];            np.y=p.y+dy[d];            np.s=p.s+1;            if(np.x<0||np.x>n||np.y<0||np.y>m)                continue;            if(vis[np.x][np.y])                continue;            if(G[np.x][np.y]!='#'){                vis[np.x][np.y]=true;                Q.push(np);            }        }    }    return -1;}int main(){    int T,d1,d2;    scanf("%d",&T);    int i,j;    while(T--){        scanf("%d%d",&m,&n);        for(i=0;i<n;i++){            scanf("%s",G[i]);            for(j=0;j<m;j++){                if(G[i][j]=='S'){sx=i;sy=j;}                else if(G[i][j]=='E'){ex=i;ey=j;}            }        }        if(sx==0){d1=3;d2=3;}        else if(sx==n-1){d1=1;d2=1;}        else if(sy==0){d1=2;d2=0;}        else if(sy==m-1){d1=0;d2=2;}        printf("%d ",dfs(sx,sy,d1,1,dl));        printf("%d ",dfs(sx,sy,d2,1,dr));        printf("%d\n",bfs(sx,sy));    }    return 0;}




0 0
原创粉丝点击