Knowledge Cards solution codeforces

1 second

256 megabytes

standard input

standard output

Pak Chanek, a renowned scholar, invented a card puzzle using his knowledge. In the puzzle, you are given a board withĀ šnĀ rows andĀ šmĀ columns. LetĀ (š,š)(r,c)Ā represent the cell in theĀ šr-th row and theĀ šc-th column.

Initially, there areĀ škĀ cards stacked in cellĀ (1,1)(1,1). Each card has an integer fromĀ 11Ā toĀ škĀ written on it. More specifically, theĀ ši-th cardĀ from the topĀ of the stack in cellĀ (1,1)(1,1)Ā has the numberĀ ššaiĀ 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Ā 11Ā toĀ šk. All other cells are empty.

You need to move theĀ škĀ cards to cellĀ (š,š)(n,m)Ā to create another stack of cards. LetĀ ššbiĀ be the number written on theĀ ši-th cardĀ from the topĀ of the stack in cellĀ (š,š)(n,m). You should create the stack in cellĀ (š,š)(n,m)Ā in such a way so thatĀ šš=šbi=iĀ for allĀ 1ā¤šā¤š1ā¤iā¤k.

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Ā (1,1)(1,1)Ā andĀ (š,š)(n,m)Ā must not have more than one card on it.
- You cannot move a card onto cellĀ (1,1)(1,1).
- You cannot move a card from cellĀ (š,š)(n,m).

Given the values ofĀ šn,Ā šm,Ā škĀ and the arrayĀ ša, determine if the puzzle is solvable.

Each test contains multiple test cases. The first line contains an integerĀ š”tĀ (1ā¤š”ā¤2ā 1041ā¤tā¤2ā 104) ā 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Ā šn,Ā šm, andĀ škĀ (3ā¤š,šā¤1063ā¤n,mā¤106,Ā ššā¤106nmā¤106,Ā 1ā¤šā¤1051ā¤kā¤105) ā the size of the board and the number of cards.

The second line of the test case containsĀ škĀ integersĀ š1,š2,ā¦,šša1,a2,ā¦,akĀ ā the arrayĀ ša, representing the numbers written on the cards. The values ofĀ šaĀ are a permutation of integers fromĀ 11Ā toĀ šk.

It is guaranteed that the sum ofĀ ššnmĀ andĀ škĀ over all test cases do not exceedĀ 106106Ā andĀ 105105Ā 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).

YA TIDAK YA YA

In the first test case, the following is one way the puzzle can be done:

- Move the card withĀ 33Ā written on it from cellĀ (1,1)(1,1)Ā to cellĀ (1,2)(1,2), then cellĀ (1,3)(1,3).
- Move the card withĀ 66Ā written on it from cellĀ (1,1)(1,1)Ā to cellĀ (2,1)(2,1), then cellĀ (3,1)(3,1), then cellĀ (3,2)(3,2), then cellĀ (3,3)(3,3).
- Move the card withĀ 44Ā written on it from cellĀ (1,1)(1,1)Ā to cellĀ (1,2)(1,2).
- Move the card withĀ 11Ā written on it from cellĀ (1,1)(1,1)Ā to cellĀ (2,1)(2,1), then cellĀ (2,2)(2,2), then cellĀ (2,3)(2,3).
- Move the card withĀ 22Ā written on it from cellĀ (1,1)(1,1)Ā to cellĀ (2,1)(2,1), then cellĀ (2,2)(2,2).
- Move the card withĀ 55Ā written on it from cellĀ (1,1)(1,1)Ā to cellĀ (2,1)(2,1), then cellĀ (3,1)(3,1), then cellĀ (3,2)(3,2), then cellĀ (3,3)(3,3).
- Move the card withĀ 22Ā written on it from cellĀ (2,2)(2,2)Ā to cellĀ (2,1)(2,1).
- Move the card withĀ 44Ā written on it from cellĀ (1,2)(1,2)Ā to cellĀ (2,2)(2,2), then cellĀ (3,2)(3,2), then cellĀ (3,3)(3,3).
- Move the card withĀ 33Ā written on it from cellĀ (1,3)(1,3)Ā to cellĀ (1,2)(1,2), then cellĀ (2,2)(2,2), then cellĀ (3,2)(3,2), then cellĀ (3,3)(3,3).
- Move the card withĀ 22Ā written on it from cellĀ (2,1)(2,1)Ā to cellĀ (3,1)(3,1), then cellĀ (3,2)(3,2), then cellĀ (3,3)(3,3).
- Move the card withĀ 11Ā written on it from cellĀ (2,3)(2,3)Ā to cellĀ (3,3)(3,3).

An animated illustration regarding the process mentioned above is as follows: