QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 1024 MB 満点: 100

#7290. 机器人

統計

有一个机器人被放置在一个 $n \times m$ 的网格上。其中一些网格单元是墙壁。

机器人接受 4 种指令:updownleftright

假设机器人当前位于坐标 $(x, y)$。执行指令后的效果如下:

  • up:如果 $x = 0$ 或 $(x - 1, y)$ 是墙壁,机器人不移动。否则,机器人移动到 $(x - 1, y)$。
  • down:如果 $x = n - 1$ 或 $(x + 1, y)$ 是墙壁,机器人不移动。否则,机器人移动到 $(x + 1, y)$。
  • left:如果 $y = 0$ 或 $(x, y - 1)$ 是墙壁,机器人不移动。否则,机器人移动到 $(x, y - 1)$。
  • right:如果 $y = m - 1$ 或 $(x, y + 1)$ 是墙壁,机器人不移动。否则,机器人移动到 $(x, y + 1)$。

已知机器人的起始位置要么是 $(a, b)$,要么是 $(c, d)$。请找到一个长度不超过 $q$ 的指令序列,使得无论机器人从 $(a, b)$ 还是 $(c, d)$ 出发,最终都能到达 $(0, 0)$。可以证明,对于所有满足题目约束的输入,都存在这样的解。

实现细节

你需要实现以下过程:

void construct_instructions(bool[][] g, int q, int a, int b, int c, int d)
  • $g$:一个大小为 $n \times m$ 的二维数组。对于每个 $i, j$ ($0 \le i \le n - 1, 0 \le j \le m - 1$),如果单元格 $(i, j)$ 是墙壁,则 $g[i][j] = 1$,否则 $g[i][j] = 0$。
  • $q$:允许使用的最大指令数。
  • $a, b, c, d$:机器人的可能起始位置为 $(a, b)$ 或 $(c, d)$。

该过程应调用以下一个或多个过程来构建指令序列:

void up()
void down()
void left()
void right()

在追加最后一条指令后,construct_instructions 应该返回。

样例

考虑以下调用:

construct_instructions([[0,0],[0,1]], 700, 1, 0, 0, 1)

该网格在 $(1, 1)$ 处有一个墙壁。

如果机器人从 $(1, 0)$ 开始,执行 up() 后紧接着执行 left(),会发生以下情况:

执行动作 新位置 备注
up() $(0, 0)$ $(0, 0)$ 不是墙壁
left() $(0, 0)$ 由于 $y = 0$,机器人不向左移动

同样,如果机器人从 $(0, 1)$ 开始,它在执行 up() 后保持在 $(0, 1)$,并在执行 left() 后移动到 $(0, 0)$。

程序应在返回以输出此构造之前,调用 up() 后紧接着调用 left()

数据范围

  • $1 \le n \le 10$
  • $1 \le m \le 10$
  • $0 \le a \le n - 1$
  • $0 \le b \le m - 1$
  • $0 \le c \le n - 1$
  • $0 \le d \le m - 1$
  • $g[0][0] = g[a][b] = g[c][d] = 0$
  • 存在将机器人从 $(a, b)$ 移动到 $(0, 0)$ 的有限指令序列
  • 存在将机器人从 $(c, d)$ 移动到 $(0, 0)$ 的有限指令序列
  • $q = 700$

子任务

  1. (20 分) $n \le 2$
  2. (20 分) $g[i][j] = 0$ (对于所有 $0 \le i \le n - 1, 0 \le j \le m - 1$)
  3. (20 分) $a = c, b = d$
  4. (40 分) 无附加约束

样例评测程序

样例评测程序按以下格式读取输入:

  • 第 1 行:$n \ m \ q \ a \ b \ c \ d$
  • 第 $2 + i$ 行 ($0 \le i \le n - 1$):$g[i][0] \ g[i][1] \dots g[i][m - 1]$

样例评测程序按以下格式打印输出:

  • 第 1 行:使用的指令数量
  • 第 2 行:包含所有已使用指令的字符串。对于每次对 up()down()left()right() 的调用,评测程序将分别打印单个字符 UDLR

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.