POJ3083 Children of the Candy Corn dfs+bfs

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

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




这是我目前见过dfs中最难的了(见识的很少),一个求起点一直向左走(顺时针),一个一直向右走(逆时针),和最小的

起点到终点的步数,这个题我也是看别人代码,想了一天,细节我写在代码中,不好描述,看不懂就好好想想,我也有点混沌,

我就写出我看懂的,看代码之前请拿支笔 写    右  0, 上1,左2,下3;(只要(逆时针就行)





#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 50

char a[N][N];

int step[4][2]={0,1,-1,0,0,-1,1,0};//可以改变,但是一定要是逆时针

int sx,sy,ex,ey;

struct stud{
    stud(int xx,int yy,int dd):x(xx),y(yy),d(dd){}
    stud(){}
  int x,y,d;
}s,e;

int ans,ans1,ans2;

int n,m;

void dfs(struct stud s,int time,int c)
{
      if(s.x==ex&&s.y==ey)
      {
          ans=time;
          return ;
      }

     for(int i=-1;i<3;i++)
     {
         int x,y,d;
         d=(s.d+i*c+4)%4;  //这是非常关键的一步,c有正负之分
                           //记住d记录的是上一步走完后的方向(面朝哪个方向)
                           //请看着图模拟过程

         x=s.x+step[d][0];
         y=s.y+step[d][1];

         if(x>=0&&x<n&&y>=0&&y<m&&a[x][y]!='#')
         {
             dfs(stud(x,y,d),time+1,c);
             return ;
         }
     }
}

void bfs()
{
    ans=0;
    int i,j;

     struct stud cur,next;
     cur.x=sx;
     cur.y=sy;
     cur.d=1;

     queue<stud>q;

      q.push(cur);

      while(!q.empty())
      {
          cur=q.front();
          q.pop();

          if(cur.x==ex&&cur.y==ey)
          {
              ans=cur.d;
              return ;
          }
          for(i=0;i<4;i++)
          {
              int xx=cur.x+step[i][0];
              int yy=cur.y+step[i][1];
              if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]!='#'&&a[xx][yy]!='S')
              {
                  a[xx][yy]='#';
                  next.x=xx;
                  next.y=yy;
                  next.d=cur.d+1;
                  q.push(next);
              }
          }
      }
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                }

                if(a[i][j]=='E')
                {
                    ex=i;
                    ey=j;
                }
            }
        }

        int d1,d2;
        s.x=sx;
        s.y=sy;

        if(sx==0)  //看着画的图看,人在哪边,他对面是他的方向,
                    //(其实没什么,好像可以赋值,0~3)
          s.d=3;

        if(sx==n-1)
          s.d=1;

        if(sy==0)
          s.d=0;

        if(sy==m-1)
          s.d=2;

        dfs(s,1,-1);
        ans1=ans;

        dfs(s,1,1);
        ans2=ans;

        bfs();

       printf("%d %d %d\n",ans1,ans2,ans);
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 1321 排兵布阵问题可以使用 DFS 算法求解。 题目要求在一个 n x n 的棋盘上,放置 k 个棋子,其中每行、每列都最多只能有一个棋子。我们可以使用 DFS 枚举每个棋子的位置,对于每个棋子,尝试将其放置在每一行中未被占用的位置上,直到放置了 k 个棋子。在 DFS 的过程中,需要记录每行和每列是否已经有棋子,以便在尝试放置下一个棋子时进行判断。 以下是基本的 DFS 模板代码: ```python def dfs(row, cnt): global ans if cnt == k: ans += 1 return for i in range(row, n): for j in range(n): if row_used[i] or col_used[j] or board[i][j] == '.': continue row_used[i] = col_used[j] = True dfs(i + 1, cnt + 1) row_used[i] = col_used[j] = False n, k = map(int, input().split()) board = [input() for _ in range(n)] row_used = [False] * n col_used = [False] * n ans = 0 dfs(0, 0) print(ans) ``` 其中,row 代表当前尝试放置棋子的行数,cnt 代表已经放置的棋子数量。row_used 和 col_used 分别表示每行和每列是否已经有棋子,board 则表示棋盘的状态。在尝试放置棋子时,需要排除掉无法放置的位置,即已经有棋子的行和列,以及棋盘上标记为 '.' 的位置。当放置了 k 个棋子时,即可计数一次方案数。注意,在回溯时需要将之前标记为已使用的行和列重新标记为未使用。 需要注意的是,在 Python 中,递归深度的默认限制为 1000,可能无法通过本题。可以通过以下代码来解除限制: ```python import sys sys.setrecursionlimit(100000) ``` 完整代码如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值