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