QOJ.ac

QOJ

Límite de tiempo: 5 s Límite de memoria: 1024 MB Puntuación total: 100

#3885. 拼图

Estadísticas

你在清理阁楼时发现了一盒旧游戏,其中包含一副拼图。不幸的是,包装已经损坏,一些拼图块散落在盒底,你怀疑其中一些碎片可能丢失了。事实上,考虑到你阁楼的整洁程度,盒子里的一些碎片甚至可能来自完全不同的拼图!现在,你面前有一堆拼图块,你正试图将它们拼成一个完整的拼图。

图 J.1:第一个样例的示意图。

更正式地说: 有 $n$ 个正方形拼图块,编号从 $1$ 到 $n$,需要将它们并排排列以组成一个矩形。必须使用所有 $n$ 个拼图块。 拼图块的边缘要么是直的,要么是不规则的。直边必须放置在拼好的矩形的边界上,而不规则边必须放置在内部。 所有不规则边的形状各不相同,因此每种边形状恰好出现两次,分别位于两个不同的拼图块上。只有当对应边的形状匹配时,两个拼图块才能放置在一起。 你可以旋转拼图块,但不能翻转它们。

输入格式

输入包含: 一行一个整数 $n$ ($1 \le n \le 3 \cdot 10^5$),表示拼图块的数量; $n$ 行,每行四个整数,第 $i$ 行给出了第 $i$ 个拼图块按逆时针顺序的连接(边形状)。

类型为 $0$ 的连接代表直边。其他连接类型用从 $1$ 开始的连续正整数编号,每种类型恰好出现两次,位于两个不同的行中。

输出格式

如果拼图块无法按上述要求拼装,输出 impossible。否则,按以下格式输出拼好的拼图: 一行两个整数 $h, w$ ($h, w \ge 1, h \cdot w = n$),表示网格的高度和宽度; $h$ 行,每行 $w$ 个整数,表示拼图块的编号。

正确解的任何旋转形式均可被接受。

样例

样例输入 1

6
0 0 1 6
0 7 4 0
0 0 2 1
5 3 0 6
3 7 0 0
4 5 2 0

样例输出 1

2 3
1 4 5
3 6 2

样例输入 2

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

样例输出 2

impossible

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.