poj1376 Robot (bfs)
来源:互联网 发布:默默西风淘宝店 编辑:程序博客网 时间:2024/05/29 18:35
题意:一个机器人,在1s内可以顺时针或逆时针转身90°,或者向前走1~3步。问从起点到终点的最小步数。注意障碍的边缘也不能走。
简单的bfs,用dist[i][j][k]记录到点(i,j),方向为k的步数。
代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>using namespace std;struct Node{int x, y;int step;int dir;Node() {}Node (int a, int b, int c, int d) : x(a), y(b), step(c), dir(d) {}};int n, m;int dir[4][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int map[55][55];int ex, ey;Node start;int dist[55][55][4];int spfa() {memset(dist, 0x3f, sizeof(dist));dist[start.x][start.y][start.dir] = 0;queue<Node> q;q.push(start);while (!q.empty()) {Node cur = q.front();q.pop();if (cur.x == ex && cur.y == ey) return cur.step;if (cur.step + 1 < dist[cur.x][cur.y][(cur.dir + 1) % 4]) {dist[cur.x][cur.y][(cur.dir + 1) % 4] = cur.step + 1;q.push(Node(cur.x, cur.y, cur.step + 1, (cur.dir + 1) % 4));}if (cur.step + 1 < dist[cur.x][cur.y][(cur.dir + 3) % 4]) {dist[cur.x][cur.y][(cur.dir + 3) % 4] = cur.step + 1;q.push(Node(cur.x, cur.y, cur.step + 1, (cur.dir + 3) % 4));}for (int i = 1; i <= 3; i++) {int nx = cur.x + i * dir[cur.dir][0];int ny = cur.y + i * dir[cur.dir][1];if (map[nx][ny] == 1) break;if (nx > 0 && nx < n && ny > 0 && ny < m && cur.step + 1 < dist[nx][ny][cur.dir]) {dist[nx][ny][cur.dir] = cur.step + 1;q.push(Node(nx, ny, cur.step + 1, cur.dir));}}}return -1;}int main() {while (~scanf("%d %d", &n, &m) && n && m) {memset(map, 0, sizeof(map));int t;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &t);if (t) map[i + 1][j] = map[i][j + 1] = map[i + 1][j + 1] = map[i][j] = 1;}}char sdir[8];scanf("%d %d %d %d", &start.x, &start.y, &ex, &ey);scanf("%s", sdir);if (sdir[0] == 'e') start.dir = 0;if (sdir[0] == 's') start.dir = 1;if (sdir[0] == 'w') start.dir = 2;if (sdir[0] == 'n') start.dir = 3;start.step = 0;int ans = spfa();cout << ans << endl;}return 0;}
0 0
- poj1376 Robot (bfs)
- poj1376 robot bfs
- poj1376 搜索(BFS)
- poj1376 bfs,机器人
- POJ1376
- poj 1376 Robot BFS
- POJ 1376 Robot (BFS)
- C. Robot(BFS)
- poj 1376 Robot(bfs)
- UVa 314 - Robot (bfs)
- Patrol Robot(BFS)
- POJ2688Cleaning Robot bfs+dfs
- Robot Motion(BFS)
- Robot in Maze----BFS
- POJ1376(A*)
- PKU2688 Cleaning Robot(BFS + DFS)
- Cleaning Robot-dfs+bfs+tsp
- poj 1376 Robot (BFS)
- MACBOOK AIR 启动工具盘的制作
- Windows系统: send to folder inaccessible
- [LeetCode] Permutation II
- 解决Android Sdk Manager更新很慢
- InputStream中read()与read(byte[] b)
- poj1376 Robot (bfs)
- JQuery中$.ajax()方法参数详解
- [Elasticsearch] 控制相关度 (一) - 相关度分值计算背后的理论
- IOS进程与线程学习笔记
- Maven内置属性
- 【好程序员特训营】Java线程同步初探
- timestamp、to_char在oracle使用过程中注意的问题
- 递归实现EasyUI中Tree的Json格式
- 数据库优化