CF1316D.Nash Matrix
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Nash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands.
This board game is played on the n×n board. Rows and columns of this board are numbered from 1 to n. The cell on the intersection of the r-th row and c-th column is denoted by (r,c).
Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following 5 characters — U, D, L, R or X — instructions for the player. Suppose that the current cell is (r,c). If the character is R, the player should move to the right cell (r,c+1), for L the player should move to the left cell (r,c−1), for U the player should move to the top cell (r−1,c), for D the player should move to the bottom cell (r+1,c). Finally, if the character in the cell is X, then this cell is the blocked zone. The player should remain in this cell (the game for him isn't very interesting from now on).
It is guaranteed that the characters are written in a way that the player will never have to step outside of the board, no matter at which cell he starts.
As a player starts from a cell, he moves according to the character in the current cell. The player keeps moving until he lands in a blocked zone. It is also possible that the player will keep moving infinitely long.
For every of the n2 cells of the board Alice, your friend, wants to know, how will the game go, if the player starts in this cell. For each starting cell of the board, she writes down the cell that the player stops at, or that the player never stops at all. She gives you the information she has written: for each cell (r,c) she wrote:
- a pair (x,y), meaning if a player had started at (r,c), he would end up at cell (x,y).
- or a pair (−1,−1), meaning if a player had started at (r,c), he would keep moving infinitely long and would never enter the blocked zone.
It might be possible that Alice is trying to fool you and there's no possible grid that satisfies all the constraints Alice gave you. For the given information Alice provided you, you are required to decipher a possible board, or to determine that such a board doesn't exist. If there exist several different boards that satisfy the provided information, you can find any of them.
纳什设计了一款有趣而简单的棋盘游戏,玩家只需遵循其所处格子上所写的指令即可。
该游戏在 n×n 的棋盘上进行。棋盘的行与列均从 1 编号至 n。第 r 行与第 c 列相交的格子记为 (r,c)。
棋盘上某些格子被称为阻塞区。每个格子上都写着以下 5 个字符之一:U、D、L、R 或 X,这些是给玩家的移动指令。假设当前格子为 (r,c):
- 若字符为 R,玩家应向右移动至格子 (r,c+1);
- 若字符为 L,玩家应向左移动至格子 (r,c−1);
- 若字符为 U,玩家应向上移动至格子 (r−1,c);
- 若字符为 D,玩家应向下移动至格子 (r+1,c);
- 若字符为 X,则该格子为阻塞区,玩家须停留在该格子(此后游戏对他而言便不再有趣)。
题目保证:无论玩家从哪个格子出发,按格子上的字符指示移动时,绝不会走出棋盘边界。
当玩家从某一格子出发后,便依照当前格子上的字符持续移动,直至抵达一个阻塞区为止。也存在一种可能:玩家将永远持续移动,永不停止。
对于棋盘全部 n2 个格子,你的朋友爱丽丝希望知道:若玩家从该格子出发,游戏将如何进行。对每个起始格子 (r,c),她记录下如下信息之一:
- 一对坐标 (x,y),表示若玩家从 (r,c) 出发,则最终会停在格子 (x,y);
- 或一对 (−1,−1),表示若玩家从 (r,c) 出发,则将无限移动下去,永不会进入任何阻塞区。
但有可能爱丽丝在故意捉弄你——她给出的信息可能根本无法对应到任何一个合法的棋盘。因此,针对爱丽丝所提供的信息,你需要判断:是否存在一个满足所有约束的棋盘;若存在,请构造出任意一个这样的棋盘;若不存在,则判定其不可能。
输入格式
The first line of the input contains a single integer n (1≤n≤103) — the side of the board.
The i-th of the next n lines of the input contains 2n integers x1,y1,x2,y2,…,xn,yn, where (xj,yj) (1≤xj≤n,1≤yj≤n, or (xj,yj)=(−1,−1)) is the pair written by Alice for the cell (i,j).
输入的第一行包含一个整数 n(1≤n≤103)—— 表示棋盘的边长。
接下来 n 行中的第 i 行包含 2n 个整数 x1,y1,x2,y2,…,xn,yn,其中 (xj,yj)(满足 1≤xj≤n 且 1≤yj≤n,或 (xj,yj)=(−1,−1))是 Alice 为单元格 (i,j) 所写的数对。
输出格式
If there doesn't exist a board satisfying the information that Alice gave you, print a single line containing INVALID.
Otherwise, in the first line print VALID. In the i-th of the next n lines, print the string of n characters, corresponding to the characters in the i-th row of the suitable board you found. Each character of a string can either be U, D, L, R or X. If there exist several different boards that satisfy the provided information, you can find any of them.
如果不存在满足爱丽丝所提供信息的棋盘,则输出一行,内容为 INVALID。
否则,第一行输出 VALID。接下来的 n 行中,第 i 行输出一个长度为 n 的字符串,表示你所找到的符合条件的棋盘的第 i 行。该字符串中的每个字符只能是 U、D、L、R 或 X。若存在多个满足条件的不同棋盘,输出其中任意一个即可。
输入输出样例
输入#1
2 1 1 1 1 2 2 2 2
输出#1
VALID XL RX
输入#2
3 -1 -1 -1 -1 -1 -1 -1 -1 2 2 -1 -1 -1 -1 -1 -1 -1 -1
输出#2
VALID RRD UXD ULL
说明/提示
For the sample test 1 :
The given grid in output is a valid one.
- If the player starts at (1,1), he doesn't move any further following X and stops there.
- If the player starts at (1,2), he moves to left following L and stops at (1,1).
- If the player starts at (2,1), he moves to right following R and stops at (2,2).
- If the player starts at (2,2), he doesn't move any further following X and stops there.
The simulation can be seen below :

For the sample test 2 :
The given grid in output is a valid one, as a player starting at any cell other than the one at center (2,2), keeps moving in an infinitely long cycle and never stops. Had he started at (2,2), he wouldn't have moved further following instruction X .
The simulation can be seen below :

对于样例测试 1:
输出中给出的网格是合法的。
- 若玩家从 (1,1) 出发,则根据指令 X 不再移动,停留在该位置。
- 若玩家从 (1,2) 出发,则根据指令 L 向左移动,最终停在 (1,1)。
- 若玩家从 (2,1) 出发,则根据指令 R 向右移动,最终停在 (2,2)。
- 若玩家从 (2,2) 出发,则根据指令 X 不再移动,停留在该位置。
模拟过程如下所示:

对于样例测试 2:
输出中给出的网格是合法的,因为除中心格 (2,2) 外,从任意其他格子出发的玩家都将无限循环移动而永不停止;若从 (2,2) 出发,则根据指令 X 不再移动。
模拟过程如下所示:

输入解题思路,AI测评打分。不知道怎么写?