POJ训练计划3083_Children of the Candy Corn(BFS+DFS+回溯)

来源:互联网 发布:其 犹 橐 龠 乎 。 编辑:程序博客网 时间:2024/05/16 11:26
Children of the Candy Corn

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
解题报告
POJ计划的搜索一题,可以说很早之前就开始看这题了,敲了好几次,看了好几次题解都没A。。。
本渣就是弱。。。
今天想继续把POJ计划刷下去,连想连看题解,到晚上终于做出来了,还是最土的方法。。。

题意:有一个迷宫,#代表墙,..代表能走,S是起点,E是终点M为宽,列数N为高;

先输出左转优先时,从S到E的步数(DFS)

再输出右转优先时,从S到E的步数(DFS)

最后输出S到E的最短步数(BFS)

左优先时,

依左上右下的顺时针方向走。根据上一个来的方向判断当前坐标开始走的方向
右优先时,
依右上左下的逆时针方向走。根据上一个来的方向判断当前坐标开始走的方向,可以走四个方向,最后才走当前位置的对面方向。
我是假设向左是0,向上是1,向右是2,向下是3,
左优先时,当前位置如果是向左(0),那么下一步依次走下,左,上,右。
看了好多题解都有公式直接算,我是实在算不出来,只好一个一个判断。。。真是渣渣。。。
(*)左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int n,m;struct node{    int x,y,step;};char mmap[50][50];int vis[50][50];int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};int cnt=1,cnt1=1;void ldfs(int s,int e,int t)//左优先{    int i,j,x,y;    if(mmap[s][e]=='E')    {        cout<<cnt<<" ";        return;    }    cnt++;    if(t==1)//当前状态向上,下一状态左上右下    {        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,0);            return ;        }        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,1);            return ;        }        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,2);            return ;        }        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,3);            return ;        }    }    else if(t==0)//当前状态向左,下一状态下左上右    {        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,3);            return ;        }        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,0);            return ;        }        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,1);            return ;        }        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,2);            return ;        }    }    else if(t==2)//当前状态向右,下一状态上右下左    {        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,1);            return ;        }        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,2);            return ;        }        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,3);            return ;        }        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,0);            return ;        }    }    else if(t==3)//当前状态向下,下一状态右下左上    {        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,2);            return ;        }        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,3);            return ;        }        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,0);            return ;        }        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            ldfs(x,y,1);            return ;        }    }}void rdfs(int s,int e,int t){    int i,j,x,y;    if(mmap[s][e]=='E')    {        cout<<cnt1<<" ";        return;    }    cnt1++;    if(t==1)    {        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,2);            return ;        }        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,1);            return ;        }        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,0);            return ;        }        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,3);            return ;        }    }    else if(t==0)    {        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,1);            return ;        }        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,0);            return ;        }        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,3);            return ;        }        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,2);            return ;        }    }    else if(t==2)    {        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,3);            return ;        }        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,2);            return ;        }        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,1);            return ;        }        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,0);            return ;        }    }    else if(t==3)    {        x=s+(0);        y=e+(-1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,0);            return ;        }        x=s+(1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,3);            return ;        }        x=s+(0);        y=e+(1);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,2);            return ;        }        x=s+(-1);        y=e+(0);        if(x>=0&&x<n&&y>=0&&y<m&&mmap[x][y]!='#')        {            rdfs(x,y,1);            return ;        }    }}void bfs(int s,int e){    int i,j;    memset(vis,0,sizeof(vis));    queue<node>Q;    node now ,next;    now.step=1;    now.x=s;    now.y=e;    Q.push(now);    vis[s][e]=1;    while(!Q.empty())    {        now = Q.front();        Q.pop();        if(mmap[now.x][now.y]=='E')        {            cout<<now.step;            return;        }        for(i=0;i<4;i++)        {            next.x=now.x+dx[i];            next.y=now.y+dy[i];            if(next.x<0||next.x>=n||next.y<0||next.y>=m||vis[next.x][next.y]||mmap[next.x][next.y]=='#')                continue;            next.step=now.step+1;            vis[next.x][next.y]=1;            Q.push(next);        }    }}int main(){    int t,j,i,si,sj,ei,ej;    cin>>t;    while(t--)    {        cnt=1;        cnt1=1;        cin>>m>>n;        for(i=0;i<n;i++)            cin>>mmap[i];        for(i=0;i<n;i++)        {            for(j=0;j<m;j++)            {                if(mmap[i][j]=='S')                {                    si=i;                    sj=j;                }                 if(mmap[i][j]=='E')                {                    ei=i;                    ej=j;                }            }        }        ldfs(si,sj,0);        rdfs(si,sj,2);        bfs(si,sj);        cout<<endl;    }    return 0;}


0 0
原创粉丝点击