P1126 机器人搬重物https://www.luogu.com.cn/problem/P1126这一题不难,细节很多,需要注意的几点
1.需要将格子转化为点
2.机器人代表的点是它所占格子的左上角一点,所以机器人不能站在点的边界
3.如果走一步或走两步时就已经遇到障碍物了后面的就也走不了
4.标记数组要多开一维表示方向
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <cstdlib>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
int n, m;
bool vis[100][100][4];
int s[100][100];
struct node {
int x, y, time, f;
node(int x1, int y1, int t1, int f1): x(x1), y(y1), time(t1), f(f1) {
}
};
int creep[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int walk[4][2] = {{-2, 0}, {0, 2}, {2, 0}, {0, -2}};
int run[4][2] = {{-3, 0}, {0, 3}, {3, 0}, {0, -3}};
int sx, sy, ex, ey;
char ch;
int face = 1 << 16;
inline void bfs() {
queue<node> que;
que.push(node(sx, sy, 0, face));
vis[sx][sy][face % 4] = true;
while (!que.empty()) {
node t = que.front();
que.pop();
//printf("%d %d %d %d\n", t.x, t.y, t.time, t.f % 4);
if (t.x == ex && t.y == ey) {
printf("%d", t.time);
return;
}
if (!vis[t.x][t.y][(t.f + 1) % 4]) {
vis[t.x][t.y][(t.f + 1) % 4] = true;
que.push(node(t.x, t.y, t.time + 1, t.f + 1));
}
if (!vis[t.x][t.y][(t.f - 1) % 4]) {
vis[t.x][t.y][(t.f - 1) % 4] = true;
que.push(node(t.x, t.y, t.time + 1, t.f - 1));
}
int tx, ty;
tx = t.x + creep[t.f % 4][0];
ty = t.y + creep[t.f % 4][1];
if (tx > 0 && ty > 0 && tx < n && ty < m && !vis[tx][ty][t.f % 4]) {
if (s[tx][ty] == 1) {
continue;
}
que.push(node(tx, ty, t.time + 1, t.f));
vis[tx][ty][t.f % 4] = true;
}
tx = t.x + walk[t.f % 4][0];
ty = t.y + walk[t.f % 4][1];
if (tx > 0 && ty > 0 && tx < n && ty < m && !vis[tx][ty][t.f % 4]) {
if (s[tx][ty] == 1) {
continue;
}
vis[tx][ty][t.f % 4] = true;
que.push(node(tx, ty, t.time + 1, t.f));
}
tx = t.x + run[t.f % 4][0];
ty = t.y + run[t.f % 4][1];
if (tx > 0 && ty > 0 && tx < n && ty < m && s[tx][ty] == 0 && !vis[tx][ty][t.f % 4]) {
que.push(node(tx, ty, t.time + 1, t.f));
vis[tx][ty][t.f % 4] = true;
}
}
printf("-1");
return;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &s[i][j]);
if (s[i][j] == 1) {
s[i - 1][j] = 1;
s[i][j - 1] = 1;
s[i - 1][j - 1] = 1;
}
}
}
scanf("%d %d %d %d %c", &sx, &sy, &ex, &ey, &ch);
if (ch == 'E') {
face += 1;
} else if (ch == 'S') {
face += 2;
} else if (ch == 'W') {
face += 3;
}
bfs();
return 0;
}