English · Original
Chinese · Translation
Formal · Original
Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem.
Allen's future parking lot can be represented as a rectangle with $4$ rows and $n$ ($n \le 50$) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having $k$ ($k \le 2n$) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places.
<center> Illustration to the first example.</center>However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space.
Allen knows he will be a very busy man, and will only have time to move cars at most $20000$ times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.
## Input
The first line of the input contains two space-separated integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 2n$), representing the number of columns and the number of cars, respectively.
The next four lines will contain $n$ integers each between $0$ and $k$ inclusive, representing the initial state of the parking lot. The rows are numbered $1$ to $4$ from top to bottom and the columns are numbered $1$ to $n$ from left to right.
In the first and last line, an integer $1 \le x \le k$ represents a parking spot assigned to car $x$ (you can only move this car to this place), while the integer $0$ represents a empty space (you can't move any car to this place).
In the second and third line, an integer $1 \le x \le k$ represents initial position of car $x$, while the integer $0$ represents an empty space (you can move any car to this place).
Each $x$ between $1$ and $k$ appears exactly once in the second and third line, and exactly once in the first and fourth line.
## Output
If there is a sequence of moves that brings all of the cars to their parking spaces, with at most $20000$ car moves, then print $m$, the number of moves, on the first line. On the following $m$ lines, print the moves (one move per line) in the format $i$ $r$ $c$, which corresponds to Allen moving car $i$ to the neighboring space at row $r$ and column $c$.
If it is not possible for Allen to move all the cars to the correct spaces with at most $20000$ car moves, print a single line with the integer $-1$.
[samples]
## Note
In the first sample test case, all cars are in front of their spots except car $5$, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most $20000$ will be accepted.
In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.
Allen 梦想着有一天拥有一个庞大的电动汽车队,即未来的汽车!他知道这将给他带来巨大的地位提升。在规划他将拥有的各种汽车以及如何安排它们时,他意识到自己遇到了一个问题。
Allen 未来的停车场可以表示为一个由 4 行和 $n$($n lt.eq 50$)列组成的矩形网格,每个格子最多容纳一辆车。他设想在网格中拥有 $k$($k lt.eq 2 n$)辆车,所有车初始时都位于第二行和第三行。每辆车都有一个指定的停车位置,位于第一行或第四行。Allen 必须将每辆车移动到其对应的停车位置。
然而,由于 Allen 从不将车交给任何人,因此一次只能移动一辆车。他可以将一辆车从一个格子沿四个基本方向之一驾驶到相邻的空格。此外,Allen 只有在该格子是某辆车的指定停车位置时,才能将该车移动到第一行或第四行的格子。
Allen 知道自己会非常忙碌,每天只有时间最多移动 $20000$ 次车,之后就会意识到搬车根本不值得他花时间。请帮助 Allen 判断他是否应该亲自停车,还是交给不那么重要的人去做。
输入的第一行包含两个用空格分隔的整数 $n$ 和 $k$($1 lt.eq n lt.eq 50$,$1 lt.eq k lt.eq 2 n$),分别表示列数和车数。
接下来的四行每行包含 $n$ 个介于 $0$ 到 $k$ 之间的整数,表示停车场的初始状态。行从上到下编号为 $1$ 到 $4$,列从左到右编号为 $1$ 到 $n$。
在第一行和最后一行中,整数 $1 lt.eq x lt.eq k$ 表示分配给车 $x$ 的停车位置(你只能将车 $x$ 移动到该位置),而整数 $0$ 表示空格(你不能将任何车移动到该位置)。
在第二行和第三行中,整数 $1 lt.eq x lt.eq k$ 表示车 $x$ 的初始位置,而整数 $0$ 表示空格(你可以将任何车移动到该位置)。
每个介于 $1$ 和 $k$ 之间的 $x$ 在第二行和第三行中恰好出现一次,在第一行和第四行中也恰好出现一次。
如果存在一个序列,能在最多 $20000$ 次移动内将所有车移动到其停车位置,则在第一行输出 $m$(移动次数),随后 $m$ 行每行输出一个移动操作,格式为 $i$ $r$ $c$,表示 Allen 将车 $i$ 移动到位于第 $r$ 行、第 $c$ 列的相邻格子。
如果 Allen 无法在最多 $20000$ 次移动内将所有车移动到正确位置,则输出单行整数 $-1$。
在第一个样例测试用例中,所有车都位于其停车位置正前方,除了车 $5$,它位于相邻的停车位置前方。示例展示了最短的可能移动序列,但任何长度不超过 $20000$ 的序列均可接受。
在第二个样例测试用例中,仅有一列,且车的顺序错误,因此没有任何车可以移动,任务不可能完成。
## Input
输入的第一行包含两个用空格分隔的整数 $n$ 和 $k$($1 lt.eq n lt.eq 50$,$1 lt.eq k lt.eq 2 n$),分别表示列数和车数。接下来的四行每行包含 $n$ 个介于 $0$ 到 $k$ 之间的整数,表示停车场的初始状态。行从上到下编号为 $1$ 到 $4$,列从左到右编号为 $1$ 到 $n$。在第一行和最后一行中,整数 $1 lt.eq x lt.eq k$ 表示分配给车 $x$ 的停车位置(你只能将车 $x$ 移动到该位置),而整数 $0$ 表示空格(你不能将任何车移动到该位置)。在第二行和第三行中,整数 $1 lt.eq x lt.eq k$ 表示车 $x$ 的初始位置,而整数 $0$ 表示空格(你可以将任何车移动到该位置)。每个介于 $1$ 和 $k$ 之间的 $x$ 在第二行和第三行中恰好出现一次,在第一行和第四行中也恰好出现一次。
## Output
如果存在一个序列,能在最多 $20000$ 次移动内将所有车移动到其停车位置,则在第一行输出 $m$(移动次数),随后 $m$ 行每行输出一个移动操作,格式为 $i$ $r$ $c$,表示 Allen 将车 $i$ 移动到位于第 $r$ 行、第 $c$ 列的相邻格子。如果 Allen 无法在最多 $20000$ 次移动内将所有车移动到正确位置,则输出单行整数 $-1$。
[samples]
## Note
在第一个样例测试用例中,所有车都位于其停车位置正前方,除了车 $5$,它位于相邻的停车位置前方。示例展示了最短的可能移动序列,但任何长度不超过 $20000$ 的序列均可接受。在第二个样例测试用例中,仅有一列,且车的顺序错误,因此没有任何车可以移动,任务不可能完成。
**Definitions**
Let $ n \in \mathbb{Z} $, $ 1 \leq n \leq 50 $, be the number of columns.
Let $ k \in \mathbb{Z} $, $ 1 \leq k \leq 2n $, be the number of cars.
Let $ P \in \{0, 1, \dots, k\}^{4 \times n} $ be the parking lot grid, where:
- Rows $ 1 $ and $ 4 $: parking spots; $ P[1][j] = x > 0 $ means car $ x $ is assigned to spot $ (1,j) $, $ P[1][j] = 0 $ means empty (forbidden for parking).
- Rows $ 2 $ and $ 3 $: initial positions; $ P[2][j] = x > 0 $ or $ P[3][j] = x > 0 $ means car $ x $ starts at $ (r,j) $, $ P[r][j] = 0 $ means empty.
Each car $ x \in \{1, \dots, k\} $ appears **exactly once** in rows $ 2 $ and $ 3 $ (initial position), and **exactly once** in rows $ 1 $ and $ 4 $ (target parking spot).
Let $ s_x \in \{2,3\} \times \{1,\dots,n\} $ be the initial position of car $ x $.
Let $ t_x \in \{1,4\} \times \{1,\dots,n\} $ be the target parking spot of car $ x $.
**Constraints**
1. Only one car may be moved at a time.
2. A car may move to an adjacent (cardinal) cell **if and only if** it is empty.
3. A car $ x $ may enter row $ 1 $ or $ 4 $ **only if** the target cell is its designated spot $ t_x $.
4. Total number of moves $ m \leq 20000 $.
**Objective**
Determine whether there exists a sequence of valid moves such that every car $ x $ reaches $ t_x $.
- If possible, output $ m $ (number of moves) and the sequence of moves $ (x, r, c) $, meaning car $ x $ moves to cell $ (r,c) $.
- If impossible within $ 20000 $ moves, output $ -1 $.
API Response (JSON)
{
"problem": {
"name": "A. Tesla",
"description": {
"content": "Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of car",
"description_type": "Markdown"
},
"platform": "Codeforces",
"limit": {
"time_limit": 3000,
"memory_limit": 262144
},
"difficulty": "None",
"is_remote": true,
"is_sync": true,
"sync_url": null,
"sign": "CF995A"
},
"statements": [
{
"statement_type": "Markdown",
"content": "Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of car...",
"is_translate": false,
"language": "English"
},
{
"statement_type": "Markdown",
"content": "Allen 梦想着有一天拥有一个庞大的电动汽车队,即未来的汽车!他知道这将给他带来巨大的地位提升。在规划他将拥有的各种汽车以及如何安排它们时,他意识到自己遇到了一个问题。\n\nAllen 未来的停车场可以表示为一个由 4 行和 $n$($n lt.eq 50$)列组成的矩形网格,每个格子最多容纳一辆车。他设想在网格中拥有 $k$($k lt.eq 2 n$)辆车,所有车初始时都位于第二行和第三行。每...",
"is_translate": true,
"language": "Chinese"
},
{
"statement_type": "Markdown",
"content": "**Definitions** \nLet $ n \\in \\mathbb{Z} $, $ 1 \\leq n \\leq 50 $, be the number of columns. \nLet $ k \\in \\mathbb{Z} $, $ 1 \\leq k \\leq 2n $, be the number of cars. \n\nLet $ P \\in \\{0, 1, \\dots, k\\}^{...",
"is_translate": false,
"language": "Formal"
}
]
}