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

来源:互联网 发布:淘宝店铺亲属过户 编辑:程序博客网 时间:2024/06/10 07:44

题目链接: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 leftint 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;}
0 0
原创粉丝点击