[Solution] Knowledge Cards Codeforces Solution
Pak Chanek, a renowned scholar, invented a card puzzle using his knowledge. In the puzzle, you are given a board with rows and columns. Let represent the cell in the -th row and the -th column.
Initially, there are cards stacked in cell . Each card has an integer from to written on it. More specifically, the -th card from the top of the stack in cell has the number written on it. It is known that no two cards have the same number written on them. In other words, the numbers written on the cards are a permutation of integers from to . All other cells are empty.
You need to move the cards to cell to create another stack of cards. Let be the number written on the -th card from the top of the stack in cell . You should create the stack in cell in such a way so that for all .
In one move, you can remove the top card from a cell and place it onto an adjacent cell (a cell that shares a common side). If the target cell already contains one or more cards, you place your card on the top of the stack. You must do each operation while satisfying the following restrictions:
- Each cell other than and must not have more than one card on it.
- You cannot move a card onto cell .
- You cannot move a card from cell .
Given the values of , , and the array , determine if the puzzle is solvable.
Each test contains multiple test cases. The first line contains an integer () — the number of test cases. The following lines contain the description of each test case.
The first line of each test case contains three integers , , and (, , ) — the size of the board and the number of cards.
The second line of the test case contains integers — the array , representing the numbers written on the cards. The values of are a permutation of integers from to .
It is guaranteed that the sum of and over all test cases do not exceed and respectively.
For each test case, output "YA" (without quotes) if it is possible and "TIDAK" (without quotes) otherwise, which mean yes and no in Indonesian respectively.
You can output "YA" and "TIDAK" in any case (for example, strings "tiDAk", "tidak", and "Tidak" will be recognised as a negative response).
No comments:
Post a Comment