poj 3083 DFS+BFS

来源:互联网 发布:个人数字图书馆软件2.1 编辑:程序博客网 时间:2024/05/17 12:46

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12748 Accepted: 5477

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






题意:

题目中有三问:

1  求一直靠着左边墙走需要走多久才可以走到出口                   (DFS)

2  求一直靠着右边墙走需要走多久才可以走到出口                   (DFS)

3  求最短路径                                                                                   (BFS)


第三问比较简单就不用说了


第二问和第一问可以综合在一起:因为从入口一直靠着右边走等效于从出口一直靠着左边走


由下面知道:

相对当前位置进行左上右下(右前左后)探索(这个相对性非常重要),最初的方向由起点S确定,而下一步的方向则由前一步的走向决定

当前方向     检索顺序             #

     ↑ :      ← ↑ → ↓        #  ^  #

    → :        ↑ → ↓ ←            |

     ↓ :      → ↓ ← ↑ 

    ← :        ↓ ← ↑ → 

如何左转90度?定义0,1,2,3代表四个方向,显示直接-1是不行的,可能变成负值了

那么可以用(d+3)%4,就可以左转90度到优先位置




注意这里的队列的使用,这里可以直接push进去一个结构体




#include<queue>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int w,h;struct node{    int x,y;    int rout;}s,e;char str[50][50];int dx[]={0,0,-1,1};int dy[]={-1,1,0,0};int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};int flag,cnt;void DFS(int x,int y,int tx,int ty,int d){    if(x==tx&&y==ty){        flag=1;        return ;    }    d=(d+3)%4;    for(int i=d;i<d+4;i++){        int tempx=x+dir[i%4][0];        int tempy=y+dir[i%4][1];        if(tempx>=0&&tempx<h&&tempy>=0&&tempy<w&&str[tempx][tempy]!='#'){            cnt++;            d=i;//***注意这里的理解            DFS(tempx,tempy,tx,ty,d);            if(flag)                return;        }    }}int BFS(node k){    bool visit[110][110];    memset(visit,false,sizeof(visit));    queue<node>q;    q.push(k);    //q.push((node){k.x,k.y,k.rout});    visit[k.x][k.y]=true;    while(!q.empty())    {        node t=q.front();        q.pop();        if(t.x==e.x&&t.y==e.y)            return t.rout;        for(int i=0;i<4;i++){            node temp;            temp.x=t.x+dx[i];            temp.y=t.y+dy[i];            temp.rout=t.rout+1;            if(temp.x<0||temp.y<0||temp.x>=h||temp.y>=w)                continue;            if(visit[temp.x][temp.y])                continue;            if(str[temp.x][temp.y]!='#'){                visit[temp.x][temp.y]=true;                q.push(temp);            }        }    }}int main(){    int t;    //freopen("in.txt","r",stdin);    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&w,&h);        for(int i=0;i<h;i++){            scanf("%s",str[i]);            for(int j=0;j<w;j++){                if(str[i][j]=='S'){                    s.x=i;                    s.y=j;                    s.rout=1;                }                else if(str[i][j]=='E'){                    e.x=i;                    e.y=j;                }            }        }        cnt=1;flag=0;        DFS(s.x,s.y,e.x,e.y,0);        int ans_left=cnt;        cnt=1;flag=0;        DFS(e.x,e.y,s.x,s.y,0);        int ans_right=cnt;        int ans=BFS(s);        printf("%d %d %d\n",ans_left,ans_right,ans);    }    return 0;}


0 0
原创粉丝点击