poj3083,BFS+DFS,优先左右

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使用优先左转需要步数,优先右转步数,以及最短步数。
优先左转的理解为
当前方向    检索顺序

 ↑ :      ← ↑ → ↓

→ :        ↑ → ↓ ← 

 ↓ :      → ↓ ← ↑ 

← :        ↓ ← ↑ → 
优先右转理解为
当前方向     检索顺序
↑ :        → ↑ ← ↓

→:       ↓ → ↑  ←

↓ :        ←  ↓  → ↑

←:      ↑  ←  ↓  → 
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 50;
char Map[N][N];
int book[N][N];
int m,n,sx,sy,ex,ey,flag;
int ans1,ans2;
int dir[4][2] = {-1,0,1,0,0,1,0,-1};///上,下,右,左。
struct T
{
    int x,y,step;
}now,eed;
bool judge(int xx,int yy)
{
    if(xx>=1 && xx<=m && yy>=1 && yy<=n && book[xx][yy]==0 && Map[xx][yy]!='#')
        return true;
              return false;
}
//face表示的是当前的朝向,step表示的是当前的步数
void dfs1(int x,int y,int step ,int face)//向左走,进行的是顺序的,递归
{
    if(flag == 1)
      return ;
      if(x==ex && y==ey)
      {
          flag=1;
          ans1 = step;
          return ;
      }
      //根据上一个的朝向判断下一个应该向那转弯
      if(face == 1)
      {
          if(judge(x+1,y)) dfs1(x+1,y,step+1,4);
          if(judge(x,y-1)) dfs1(x,y-1,step+1,1);
          if(judge(x-1,y)) dfs1(x-1,y,step+1,2);
          if(judge(x,y+1)) dfs1(x,y+1,step+1,3);
      }
      else if(face == 2)
      {
          if(judge(x,y-1)) dfs1(x,y-1,step+1,1);
          if(judge(x-1,y)) dfs1(x-1,y,step+1,2);
          if(judge(x,y+1)) dfs1(x,y+1,step+1,3);
          if(judge(x+1,y)) dfs1(x+1,y,step+1,4);
      }
      else if(face == 3)
      {
          if(judge(x-1,y)) dfs1(x-1,y,step+1,2);
          if(judge(x,y+1)) dfs1(x,y+1,step+1,3);
          if(judge(x+1,y)) dfs1(x+1,y,step+1,4);
          if(judge(x,y-1)) dfs1(x,y-1,step+1,1);
      }
      else
      {
          if(judge(x,y+1)) dfs1(x,y+1,step+1,3);
          if(judge(x+1,y)) dfs1(x+1,y,step+1,4);
          if(judge(x,y-1)) dfs1(x,y-1,step+1,1);
          if(judge(x-1,y)) dfs1(x-1,y,step+1,2);
      }
}
void dfs2(int x,int y,int step,int face)//向右走,进行的是逆序的,递归
{
     if(flag == 1)
      return ;
      if(x==ex && y==ey)
      {
          flag=1;
          ans2 = step;
          return ;
      }
      if(face == 1)
      {
          if(judge(x-1,y)) dfs2(x-1,y,step+1,2);
          if(judge(x,y-1)) dfs2(x,y-1,step+1,1);
          if(judge(x+1,y)) dfs2(x+1,y,step+1,4);
          if(judge(x,y+1)) dfs2(x,y+1,step+1,3);
      }
     else  if(face == 2)
      {
          if(judge(x,y+1)) dfs2(x,y+1,step+1,3);
          if(judge(x-1,y)) dfs2(x-1,y,step+1,2);
          if(judge(x,y-1)) dfs2(x,y-1,step+1,1);
          if(judge(x+1,y)) dfs2(x+1,y,step+1,4);
      }
      else if(face == 3)
      {
          if(judge(x+1,y)) dfs2(x+1,y,step+1,4);
          if(judge(x,y+1)) dfs2(x,y+1,step+1,3);
          if(judge(x-1,y)) dfs2(x-1,y,step+1,2);
          if(judge(x,y-1)) dfs2(x,y-1,step+1,1);
      }
      else
      {
          if(judge(x,y-1)) dfs2(x,y-1,step+1,1);
          if(judge(x+1,y)) dfs2(x+1,y,step+1,4);
          if(judge(x,y+1)) dfs2(x,y+1,step+1,3);
          if(judge(x-1,y)) dfs2(x-1,y,step+1,2);
      }
}
int bfs()
{
    queue< T > Q;
    while(!Q.empty())
        Q.pop();
    now.x=sx,now.y=sy,now.step=1;
    Q.push(now);
    book[sx][sy]=1;
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if(now.x == ex && now.y == ey)
             return now.step;
             for(int i=0; i<4; i++)
                {
                    eed.x=now.x+dir[i][0];
                    eed.y=now.y+dir[i][1];
                    if(judge(eed.x,eed.y))
                    {
                        eed.step=now.step+1;
                        book[eed.x][eed.y]=1;
                        Q.push(eed);
                    }
                }
    }
    return 0;
}
int main()
{
    int T;
scanf("%d",&T);
    while(T--)
    {
        memset(book,0,sizeof(book));
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++)
            for(int j=1; j<=n; j++)
            {
               scanf(" %c",&Map[i][j]);
                if(Map[i][j] == 'S')
                {
                    sx=i; sy=j;
                }
                if(Map[i][j] == 'E')
                {
                    ex=i; ey=j;
                }
            }
        flag=0;ans1=0;
        dfs1(sx,sy,1,1);//一直向左走
        flag=0;ans2=0;
        dfs2(sx,sy,1,1);//一直向右走
        int ans3=bfs();
        printf("%d %d %d\n",ans1,ans2,ans3);
    }
return 0;
}

#include <stdio.h>
#include <string.h>
#include<queue>
int right[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };///下,右,上,左
int left[4][2] = { {1,0},{0,-1},{-1,0},{0,1} };///下,左,上,右
char map[45][45];
int si, sj, ei, ej;
int dir;
int q[2500][2];
int flag[45][45];
int n, m;
int dfs_left(int ni, int nj, int step)
{
    if ( ni==ei && nj==ej )
        return step+1;
    if ( ni<0 || ni>=n || nj<0 || nj>=m )
        return 0;
    if ( map[ni][nj]=='#' )
        return 0;
    dir = (dir+3)%4;
    while ( 1 )
    {
        int tmp = dfs_left( ni+left[dir][0], nj+left[dir][1], step+1 );
        if ( tmp>0 )
            break;
        dir = (dir+1)%4;
    }
}
int dfs_right(int ni, int nj, int step)
{
    if ( ni==ei && nj==ej )
        return step+1;
    if ( ni<0 || ni>=n || nj<0 || nj>=m )
        return 0;
    if ( map[ni][nj]=='#' )
        return 0;
    dir = (dir+3)%4;
    while ( 1 )
    {
        int tmp = dfs_right( ni+right[dir][0], nj+right[dir][1], step+1 );
        if ( tmp>0 )
            break;
        dir = (dir+1)%4;
    }

}
int bfs()//BFS最短路
{
    memset( flag, 0, sizeof(flag) );
    int head = 0;
    int tail = 0;
    q[tail][0] = si;
    q[tail++][1] = sj;
    flag[si][sj] = 1;
    int step = 0;
    while ( head<tail && flag[ei][ej]==0 )
    {
        int tmp = tail;
        step++;
        while ( head<tmp && flag[ei][ej]==0 )
        {
            int ni = q[head][0];
            int nj = q[head++][1];
            for ( int i=0; i<4; i++ )
            {
                int ti = ni+right[i][0];
                int tj = nj+right[i][1];
                if ( ti>=0 && ti<n && tj>=0 && tj<m && flag[ti][tj]==0
                    && map[ti][tj]!='#' )
                {
                    q[tail][0] = ti;
                    q[tail++][1] = tj;
                    flag[ti][tj] = 1;
                }
            }
        }
    }
    return step+1;
}
int main ()
{
    int t;
    scanf("%d", &t );
    while ( t-- )
    {
        scanf("%d %d", &m, &n );
        for ( int i=0; i<n; i++ )
        {
            scanf("%s", map[i] );
            for ( int j=0; j<m; j++ )
            {
                if ( map[i][j] =='S' )
                    si = i, sj=j;
                if ( map[i][j] =='E' )
                    ei = i, ej= j;
            }
        }
        dir = 0;
        printf("%d %d %d\n", dfs_left(si,sj,0), dfs_right(si,sj,0), bfs() );
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starry_Sky_Dream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值