点点滴滴”

导航

poj 3083 Children of th

#include <iostream>
#include<stdio.h>
#include<string.h>

using namespace std;
int n,m;
int r[4][2]= {{0,-1},{1,0},{0,1},{-1,0}};
int l[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
int vis[50][50];
char map[50][50];
int x1,x2,y1,y2,ans;
int que[2500][2];
int dfsl(int x,int y,int num1)
{
    if(x==x2&&y==y2)
        return ++num1;
    if(x<0||x>=n||y<0||y>=m||map[x][y]=='#')
        return 0;
    ans=(ans+3)%4;
    int temp=0;
    while(1)
    {
        temp=dfsl(x+l[ans][0],y+l[ans][1],num1+1);
        if(temp>0)
            break;
        ans=(ans+1)%4;
    }
    return temp;

}

int dfsr(int x,int y,int num2)
{
    if(x==x2&&y==y2)
        return ++num2;
    if(x<0||x>n||y<0||y>m||map[x][y]=='#')
        return 0;
    ans=(ans+3)%4;
    int temp=0;
    while(1)
    {
        temp=dfsr(x+r[ans][0],y+r[ans][1],num2+1);
        if(temp>0)
            break;
        ans=(ans+1)%4;
    }
    return temp;

}

int bfs()
{
    int fir=0,sec=0;
    que[sec][0]=x1;
    que[sec++][1]=y1;
    vis[x1][y1]=1;
    int step=1;
    while(fir<sec&&!vis[x2][y2])
    {
        int tmp=sec;
        step++;
        while(fir<tmp&&!vis[x2][y2])
        {
            int x=que[fir][0];
            int y=que[fir++][1];
            for(int i=0; i<4; i++)
            {
                int x1=x+r[i][0];
                int y1=y+r[i][1];
                if(x1>=0&&x1<n&&y1>=0&&y1<m&&!vis[x1][y1]&&map[x1][y1]!='#')
                {
                    que[sec][0]=x1;
                    que[sec++][1]=y1;
                    vis[x1][y1]=1;
                }
            }
        }
    }
    return step;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        memset(que,0,sizeof(que));
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
        {
            // scanf("%s",map[i]);
            for(int j=0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')
                {
                    x1=i;
                    y1=j;
                }
                if(map[i][j]=='E')
                {
                   x2=i;
                    y2=j;
                }
            }
        }
        ans=0;
        printf("%d",dfsl(x1,y1,0));
        ans=0;
        printf(" %d",dfsr(x1,y1,0));
        printf(" %d\n",bfs());
    }
    return 0;
}

 

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

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

题意:
S为起点 E为终点 #不可以走 以左为优先方向 以右为优先方向 最短 的步骤

求最短步骤都要用广搜

posted on 2014-08-25 19:05  点点滴滴”  阅读(127)  评论(0编辑  收藏  举报