QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 1024 MB 總分: 100

#5583. 彩色试管

统计

社交媒体上出现了一款名为“彩色试管”(Color Tubes)的新益智游戏。规则相对简单:你有 $n+1$ 个试管,里面装有 $3n$ 个彩色球。每个试管最多可容纳 3 个球,且每种颜色恰好有 3 个球(因此共有 $n$ 种颜色)。

通过一系列移动,你需要达到“彩色试管”状态——即每个试管要么装有同一种颜色的球,要么为空。

唯一允许的移动操作是:从一个试管中取出最上面的球,并将其放入另一个有空间的试管中(即移动前该试管内最多只有 2 个球)。

你需要编写一个程序来解决这个谜题。目前,你不需要寻找最优解,但要求程序足够高效,能在最多 $20n$ 次移动内解决任何谜题配置。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 1000$),表示颜色的数量。

接下来的 $n+1$ 行,每行包含三个整数 $b, m$ 和 $t$ ($0 \le b, m, t \le n$),描述每个试管的情况,其中 $b$ 是底部的球的颜色,$m$ 是中间的球的颜色,$t$ 是顶部的球的颜色。

试管编号为 $1$ 到 $n+1$,按输入顺序排列。颜色编号为 $1$ 到 $n$。数字 $0$ 表示空位。保证不会出现空位位于有色球下方的情况。

输出格式

第一行输出一个整数 $m$,表示你的程序解决该谜题所使用的移动次数。请记住,$m$ 必须不超过 $20n$。

接下来的 $m$ 行,每行输出两个空格分隔的整数 $u$ 和 $v$,描述一次移动 ($1 \le u, v \le n+1$)。在每次移动中,你从试管 $u$ 中取出最上面的球,并将其放入试管 $v$ 中,球会落到该试管中已有球的最上方,如果试管为空,则落到底部。

如果你的解法使用的移动次数超过 $20n$,或者包含任何不合法的移动,或者最终状态不是“彩色试管”状态,则你的答案将被判定为错误。

样例

样例输入 1

3
2 2 0
1 3 1
3 1 2
3 0 0

样例输出 1

6
3 1
2 3
2 4
3 2
3 2
3 4

样例输入 2

1
0 0 0
1 1 1

样例输出 2

0

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.