POJ 3083 Children of the Candy Corn(BFS + DFS)

本文解析了一种迷宫寻路算法,通过bfs和特定的dfs策略寻找从起点到终点的不同路径,包括沿左右墙壁行走及最短路径。文章详细介绍了如何利用状态记录和方向判断来实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=3083

题意:给定 w×h 的矩阵,分别求从S到T:沿着左边墙壁的走的路径,沿着右边墙壁走的路径,最短路径。

思路:最短路径直接利用bfs搜索可得。对于沿着墙壁走,记录当前的状态为(当前点所在的坐标,当前前行的方向),例如沿着左边墙壁走,若一下不会做,可以分情况考虑,以当前所在点为中心的 3×3 的矩阵中,分别讨论剩下8个格子的是点还是墙时的走法。最后可以发现规律,沿着左边墙壁走,按照左上右下,即顺时针的方向判断,若先判断的方向可以走,就直接走。同理,沿着右边墙壁走,按照右上左下,即逆时针的方向判断。

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

const int INF = 0x3f3f3f3f;

struct Node {
  int x, y;
  int path; // dfs时记录为当前的方向
};

char _map[50][50];
int n, m;
// 0 up 1 right 2 down 3 left
int x[4] = {-1, 0, 1, 0};
int y[4] = {0, 1, 0, -1};
int vis[50][50];

bool inFiled(Node to) {
  if (to.x >= 0 && to.x < n && to.y >= 0 && to.y < m)
    return true;
  return false;
}

int dfs(Node now, int s) {
  if (_map[now.x][now.y] == 'E')
    return 1;
  for (int i = s, j = 0; j < 4; i = (i + (4 - s)) % 4, j++) {
    Node to;
    to.path = (now.path + i) % 4;
    to.x = now.x + x[to.path];
    to.y = now.y + y[to.path];
    if (_map[to.x][to.y] != '#')
      return dfs(to, s) + 1;
  }
  return 0;
}


int bfs(Node s, Node e) {
  memset(vis, INF, sizeof(vis));
  queue<Node> q;
  vis[s.x][s.y] = 1;
  q.push(s);
  while (!q.empty()) {
    Node top = q.front();
    q.pop();
    if (top.x == e.x && top.y == e.y)
      return top.path;
    for (int i = 0; i < 4; i++) {
      Node to;
      to.x = top.x + x[i];
      to.y = top.y + y[i];
      to.path = top.path + 1;
      if (inFiled(to) && _map[to.x][to.y] != '#' && vis[to.x][to.y] > to.path) {
        vis[to.x][to.y] = to.path;
        q.push(to);
      }
    }
  }
  return -1;
}

int main() {
  int t_case;
  scanf("%d", &t_case);
  for (int i_c = 0; i_c < t_case; i_c++) {
    scanf("%d%d", &m, &n);
    for (int i = 0; i < n; i++)
      scanf("%s", _map[i]);
    Node st, ed;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (_map[i][j] == 'S') {
          st.x = i, st.y = j;
        }
        else if (_map[i][j] == 'E') {
          ed.x = i, ed.y = j;
        }
      }
    }
    st.path = 1;
    int minpath = bfs(st, ed);
    int leftpath, rightpath;
    for (int i = 3, j = 0; j < 4; i = (i + 1) % 4, j++) {
      Node to;
      to.path = i;
      to.x = st.x + x[to.path];
      to.y = st.y + y[to.path];
      if (inFiled(to) && _map[to.x][to.y] != '#') {
        leftpath = dfs(to, 3) + 1;
        break;
      }
    }
    for (int i = 1, j = 0; j < 4; i = (i + 3) % 4, j++) {
      Node to;
      to.path = i;
      to.x = st.x + x[to.path];
      to.y = st.y + y[to.path];
      if (inFiled(to) && _map[to.x][to.y] != '#') {
        rightpath = dfs(to, 1) + 1;
        break;
      }
    }
    printf("%d %d %d\n", leftpath, rightpath, minpath);
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值