码迷,mamicode.com
首页 > 其他好文 > 详细

poj3083

时间:2014-06-01 10:28:57      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9108   Accepted: 3958

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

Source

题目意思就是求从S到E,按照靠左走、靠右走和最少走的步数。
bubuko.com,布布扣
如上图0,1,2,3代表当前位置方向,箭头向右,方向为1.
     靠左走的情况下则先尝试,当前位置的左边,行的通则走,不通则走当前位置的前面,依次类推
     靠右走的情况也是差不多的 
     
     然后就是确定下一个位置方向了:
     int dl[4]={-1,0,1,2};   //左 前 右 后   靠左走的方向确定方程为(dir+dl[i]+4)%4 
     int dr[4]={1,0,-1,-2};  //右 前 左 后   靠左走的方向确定方程为(dir+dr[i]+4)%4
AC代码:
#include<stdio.h>
#include<queue>
using namespace std;
struct Node{
    int x,y,s;
    friend bool operator <(const Node t1,const Node t2){
        return t1.s>t2.s;         //按从小到大 
    }
};
int go[4][2]={{-1,0},{0,1},{1,0},{0,-1}};  //上右下左 
int dl[4]={-1,0,1,2};   //左 前 右 后 
int dr[4]={1,0,-1,-2};  //右 前 左 后
char a[45][45]; 
int s0,s1,s2;
int T,w,h; 
void dfs1(int x,int y,int dir){            //靠左走的情况 
    if(a[x][y]=='E')
        return ; 
    for(int i=0;i<4;i++){                 //分别按左 前 右 后的方式走 
        int tmpdir=(dir+dl[i]+4)%4;        
        if(a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='.' || 
           a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='E' || a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='S'){
           
            s0++;
            dfs1(x+go[tmpdir][0],y+go[tmpdir][1],tmpdir);
            return ;
        }
    }
}
void dfs2(int x,int y,int dir){              //靠右走的情况 
    if(a[x][y]=='E')
        return ; 
    for(int i=0;i<4;i++){                   //分别按右 前 左 后的方式走 
        int tmpdir=(dir+dr[i]+4)%4;
        if(a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='.' || 
           a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='E' || a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='S'){
           
            s1++;
            dfs2(x+go[tmpdir][0],y+go[tmpdir][1],tmpdir);
            return ;
        }
    }
}
int bfs(int x,int y){
    int vis[45][45];   //标记数组,不要也是可以的,但会超时 
    for(int i=0;i<h;i++)
        for(int j=0;j<w;j++)
            vis[i][j]=0;
    priority_queue <Node> qu;   //直接用queue也是可以的,但是优先队列可以优化很多 
    while(!qu.empty())
        qu.pop();
    Node tmp; tmp.x=x; tmp.y=y; tmp.s=1;
    qu.push(tmp);
    while(!qu.empty()){
        tmp=qu.top(); qu.pop();
        if(a[tmp.x][tmp.y]=='E'){
            return tmp.s;
        }
        if(tmp.x+1<h && !vis[tmp.x+1][tmp.y] && (a[tmp.x+1][tmp.y]=='.' || a[tmp.x+1][tmp.y]=='E')){   //向下 
            Node k; k.x=tmp.x+1; k.y=tmp.y; k.s=tmp.s+1;
            qu.push(k);
            vis[tmp.x+1][tmp.y]=1;
        }
        if(tmp.y+1<w && !vis[tmp.x][tmp.y+1] && (a[tmp.x][tmp.y+1]=='.' || a[tmp.x][tmp.y+1]=='E')){   //向右 
            Node k; k.x=tmp.x; k.y=tmp.y+1; k.s=tmp.s+1;
            qu.push(k);
            vis[tmp.x][tmp.y+1]=1;
        }
        if(tmp.x-1>=0 && !vis[tmp.x-1][tmp.y] && (a[tmp.x-1][tmp.y]=='.' || a[tmp.x-1][tmp.y]=='E')){  //向上 
            Node k; k.x=tmp.x-1; k.y=tmp.y; k.s=tmp.s+1;
            qu.push(k);
            vis[tmp.x-1][tmp.y]=1;
        }
        if(tmp.y-1>=0 && !vis[tmp.x][tmp.y-1] && (a[tmp.x][tmp.y-1]=='.' || a[tmp.x][tmp.y-1]=='E')){  //向左 
            Node k; k.x=tmp.x; k.y=tmp.y-1; k.s=tmp.s+1;
            qu.push(k);
            vis[tmp.x][tmp.y-1]=1;
        }
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&w,&h);
        for(int i=0;i<h;i++)
            scanf("%s",a[i]);
        int flag=0,x,y,dir;
        s0=s1=s2=1;
        for(int i=0;i<w;i++){      //找到 S 所在位置并确定方向 
            if(a[0][i]=='S'){
                flag=1;
                x=0; y=i; dir=2;
                break;
            }
            if(a[h-1][i]=='S'){
                flag=1;
                x=h-1; y=i; dir=0;
                break;
            }
        }
        if(!flag){
            for(int i=0;i<h;i++){
                if(a[i][0]=='S'){
                    x=i; y=0; dir=1;
                    break;
                }
                if(a[i][w-1]=='S'){
                    x=i; y=w-1; dir=3;
                    break;
                }
            }
        }
        dfs1(x,y,dir);
        dfs2(x,y,dir);
        s2=bfs(x,y);
        printf("%d %d %d\n",s0,s1,s2);
    }
    return 0;
}


poj3083,布布扣,bubuko.com

poj3083

标签:des   c   style   class   blog   code   

原文地址:http://blog.csdn.net/my_acm/article/details/27807459

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!