P1126 机器人搬重物·题解
lll_huge_juruo · · 题解
背景
还是太弱了,绿题代码写了
调这一代码调了
题目描述
题目传送门。
解法:
坐标范围
首先,题目中写道,机器人只能走格点,但是障碍物都是格子,于是可以把障碍物抽象成
int dx[4] = { 0, 0, 1, 1 };
int dy[4] = { 0, 1, 0, 1 };
// 原始数据
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j )
tmp[i][j] = read();
// 转化过程
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j )
if (tmp[i][j])
for (int k = 0; k < 4; ++ k )
g[i + dx[k]][j + dy[k]] = tmp[i][j];
n ++ , m ++ ;
对于上面的 dx 和 dy 数组,就是把格子坐标转化成格点坐标的偏移量,看不懂的可以玩一下样例给的图片,这里不再赘述。
最后一行的 n ++ , m ++ 是更新一下坐标范围,因为格点的坐标比格子的坐标大
操作
这里我们先定义一下方向,方便后文叙述。
往上是
又定义一个结构体名为 Node,方便后续操作。
struct Node
{
int x, y; // 坐标
int direction, dist; // 方向以及距离
// 空构造函数
Node () : x(0), y(0), direction(0), dist(0) {}
// 给起点用的
Node (int x, int y, char d) : x(x), y(y), direction(change(d)), dist(0) {}
// 完整的构造函数
Node (int x, int y, int d, int s) : x(x), y(y), direction(d), dist(s) {}
// 重载运算符
bool operator==(const Node &a) const
{
return x == a.x && y == a.y;
}
} q[N * N * 6];
如果不懂构造函数,可以出门左转这里。
1.左转
模拟一组:
往右是
于是就有了代码,注意:C++ 负数取模和数学上的不一致,要进行等价转换。
Node TurnLeft()
{
return Node(x, y, (direction - 1 + 4) % 4, dist + 1);
}
2.右转
同左转,这里直接给代码。
Node TurnRight()
{
return Node(x, y, (direction + 1) % 4, dist + 1);
}
3.移动
这里发现移动几步都是把偏移量乘上步数,于是就把
自行模拟一下就可以得到偏移量数组。
int Frontx[4] = { -1, 0, 1, 0 };
int Fronty[4] = { 0, 1, 0, -1 };
这里的偏移量都是基准量,方便后续合并。
然后就有了移动的代码。
bool check(int x, int y)
{
return x >= 1 && x <= n && y >= 1 && y <= m && !g[x][y];
}
Node Move(int steps)
{
if (!check(x, y)) return Node(n + 1, m + 1, 0, -1);
return Node(x + steps * Frontx[direction], y + steps * Fronty[direction], direction, dist + 1);
}
等等!这样写对吗?
我们重新审视一下题目:
它说机器人有直径,给的地图是在一个储藏室内,说明不能碰墙壁,所以 check 函数里的判断条件都是小于。
于是就有了正确的 check 函数。
bool check(int x, int y)
{
return x > 1 && x < n && y > 1 && y < m && !g[x][y];
}
等等!这样对吗?
有这样一个例子:
我们在 move(2) 会直接到
贴一下正确的代码。
bool check(int x, int y)
{
return x > 1 && x < n && y > 1 && y < m && !g[x][y];
}
// 检查每步得合法性
bool Move_check(int x, int y, int steps, int d)
{
if (!check(x, y)) return false;
for (int i = 0; i < steps; ++ i )
{
x += Frontx[d];
y += Fronty[d];
if (!check(x, y)) return false;
}
return true;
}
Node Move(int steps)
{
if (!Move_check(x, y, steps, direction)) return Node(n + 1, m + 1, 0, -1)/*这是一个哨兵,详见后文*/;
return Node(x + steps * Frontx[direction], y + steps * Fronty[direction], direction, dist + 1);
}
主函数逻辑
1.输入处理
这里还得读入起点和终点坐标 sx、sy、ex 和 ey,同时,题目中的方向是东南西北,和我们的不对应,要另外写一个转换函数。
int change(char c)
{
switch (c)
{
case 'N': return 0;
case 'E': return 1;
case 'S': return 2;
case 'W': return 3;
default: return -1;
}
}
对于 switch-case 语句,大家可能有点生疏了,~因为这东西太鸡肋了~,有忘记的可以出门左拐向前走。
同时,如果不写构造函数的话,这部分代码就会显得有些难看,于是之前的那个构造函数就派上用场了。
int sx = read() + 1, sy = read() + 1, ex = read() + 1, ey = read() + 1;
char op[2]; scanf("%s", op); // 警钟敲烂,这行我放错位置调了半天2333
Node start(sx, sy, *op), end(ex, ey, *op);
哨兵
还记得刚刚在 move 函数里面的哨兵吗?这个哨兵可以防止越界,详见代码。
sign[n + 1][m + 1][0] = true; // 越界哨兵
如果 move 函数越界就会返回无效元素 { n + 1, m + 1, 0, -1 },这里就可以和重复入队元素一起判掉。
BFS
BFS函数逻辑较为简单,和普通广搜别无二致,这里直接给代码。
inline int bfs(Node start, Node end)
{
int hh = 0, tt = -1;
q[ ++ tt ] = start;
sign[start.x][start.y][start.direction] = true;
while (hh <= tt)
{
Node t = q[hh ++ ];
if (t == end) return t.dist;
Node tmp = t.TurnLeft();
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.TurnRight();
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.Move(1);
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.Move(2);
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.Move(3);
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
}
return -1; // 无解
}
思路总结
1.读入,转换坐标
2.BFS
2.1插入起点并标记
2.2循环弹出对头并扩展
2.2.1扩展到不重复的点就入队
2.3返回
完整代码
#include <stdio.h>
#define N 66
// 卡常部分
#ifndef ONLINE_JUDGE
#define getchar1 _getchar_nolock
#define putchar1 _putchar_nolock
#else
#define getchar1 getchar_unlocked
#define putchar1 putchar_unlocked
#endif
int n, m;
bool sign[N][N][4];
int g[N][N], tmp[N][N];
int dx[4] = { 0, 0, 1, 1 };
int dy[4] = { 0, 1, 0, 1 };
struct Node
{
int x, y;
int direction, dist;
int Frontx[4] = { -1, 0, 1, 0 };
int Fronty[4] = { 0, 1, 0, -1 };
int change(char c)
{
switch (c)
{
case 'N': return 0;
case 'E': return 1;
case 'S': return 2;
case 'W': return 3;
default: return -1;
}
}
bool check(int x, int y)
{
return x > 1 && x < n && y > 1 && y < m && !g[x][y];
}
Node TurnLeft()
{
return Node(x, y, (direction - 1 + 4) % 4, dist + 1);
}
Node TurnRight()
{
return Node(x, y, (direction + 1) % 4, dist + 1);
}
bool Move_check(int x, int y, int steps, int d)
{
if (!check(x, y)) return false;
for (int i = 0; i < steps; ++ i )
{
x += Frontx[d];
y += Fronty[d];
if (!check(x, y)) return false;
}
return true;
}
Node Move(int steps)
{
if (!Move_check(x, y, steps, direction)) return Node(n + 1, m + 1, 0, -1);
return Node(x + steps * Frontx[direction], y + steps * Fronty[direction], direction, dist + 1);
}
Node () : x(0), y(0), direction(0), dist(0) {}
Node (int x, int y, char d) : x(x), y(y), direction(change(d)), dist(0) {}
Node (int x, int y, int d, int s) : x(x), y(y), direction(d), dist(s) {}
bool operator==(const Node &a) const
{
return x == a.x && y == a.y;
}
} q[N * N * 6];
inline int bfs(Node start, Node end)
{
int hh = 0, tt = -1;
q[ ++ tt ] = start;
sign[start.x][start.y][start.direction] = true;
while (hh <= tt)
{
Node t = q[hh ++ ];
if (t == end) return t.dist;
Node tmp = t.TurnLeft();
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.TurnRight();
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.Move(1);
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.Move(2);
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
tmp = t.Move(3);
if (!sign[tmp.x][tmp.y][tmp.direction] && tmp.dist >= 0 && tmp.check(tmp.x, tmp.y))
{
sign[tmp.x][tmp.y][tmp.direction] = true;
q[ ++ tt ] = tmp;
}
}
return -1;
}
// 快读快写卡常
inline int read()
{
int x = 0, f = 1;
char c = getchar1();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar1();
}
while (c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar1();
}
return x * f;
}
inline void write(int x)
{
if (x < 0)
{
x = -x;
putchar1('-');
}
if (x > 9) write(x / 10);
putchar1(x % 10 + 48);
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1126.in", "r", stdin);
freopen("1126.out", "w", stdout);
#endif
n = read(), m = read();
// 坐标转换逻辑
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j )
tmp[i][j] = read();
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j )
if (tmp[i][j])
for (int k = 0; k < 4; ++ k )
g[i + dx[k]][j + dy[k]] = tmp[i][j];
n ++ , m ++ ;
int sx = read() + 1, sy = read() + 1, ex = read() + 1, ey = read() + 1;
char op[2]; scanf("%s", op);
Node start(sx, sy, *op), end(ex, ey, *op);
sign[n + 1][m + 1][0] = true; // 越界哨兵
write(bfs(start, end));
return 0;
}
总结
很好的一道题,细节很多,但是细心一点就好了 ~(但是我很不细心)~ 。请读者务必在理解之后自行编码。
关于调试的一些心得
遇到玄学 bug 可以先静查,静查查不出可以在关键部分输出调试信息。同时,任务管理器可以给你一些思路,如果内存一下飙升,大概率是栈溢出或无限开动态数组(如 vector 死循环插入元素)导致的,如果 CPU 频率突然变高,大概率是死循环/递归(或,把 CPU 一栏右键➡️将图形更改为➡️逻辑处理器,看到某核心几乎吃满)。
一些建议
C++ 14 标准基本够用,但是些许好用的特性不支持,大家的编译器如果不支持 C++ 17,可以去MSYS官网下载支持 C++ 26 的编译器。
注:关于构造函数和 switch-case 语句的介绍由 Deepseek 提供。