POJ 3083Children of the Candy Corn(BFS + DFS)
来源:互联网 发布:淘宝店铺亲属过户 编辑:程序博客网 时间:2024/06/10 07:44
题目链接:http://poj.org/problem?id=3083
题意:给定
思路:最短路径直接利用bfs搜索可得。对于沿着墙壁走,记录当前的状态为(当前点所在的坐标,当前前行的方向),例如沿着左边墙壁走,若一下不会做,可以分情况考虑,以当前所在点为中心的
代码:
#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
- POJ 3083 Children of the Candy Corn (bfs+dfs)
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- POJ 3083 - Children of the Candy Corn(BFS+DFS)
- POJ 3083 Children of the Candy Corn(bfs+dfs)
- POJ 3083 Children of the Candy Corn(BFS + DFS)
- POJ 3083 Children of the Candy Corn (DFS+BFS)
- POJ 3083Children of the Candy Corn(BFS + DFS)
- POJ 3083 Children of the Candy Corn(DFS+BFS)
- POJ 3083 Children of the Candy Corn (BFS+DFS)
- POJ 3083 Children of the Candy Corn dfs+bfs
- poj 3083 Children of the Candy Corn(dfs+bfs)
- POJ 3083Children of the Candy Corn(DFS*2+BFS)
- POJ 3083 Children of the Candy Corn (DFS + BFS)
- POJ 3083-Children of the Candy Corn(dfs+bfs)
- poj 3083 Children of the Candy Corn(bfs+dfs)
- poj 3083 Children of the Candy Corn (DFS+BFS)
- poj 3083 Children of the Candy Corn dfs+bfs
- poj-3083 Children of the Candy Corn-DFS+BFS
- 一万小时的刻意训练
- 出现乱码问题
- android提高应用启动速度_splash页面瞬间响应_避免APP启动闪白屏
- Mybatis学习笔记---day03缓存
- wireshark怎么抓包、wireshark抓包详细图文教程
- POJ 3083Children of the Candy Corn(BFS + DFS)
- 网站第三方登录
- CodeForces 705A Hulk(字符串输出)
- 在js中添加< br/>
- Oracle 数据库web客户端管理工具软件
- JavaScript 资源大全中文版
- IAR 工具使用AStyle工具格式化代码
- (Java)LeetCode-40. Combination Sum II
- subline Text3 Package Control安装