CodeCraft-20 (Div. 2) D. Nash Matrix(隊列)

題目傳送門

D. Nash Matrix

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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×nn×n board. Rows and columns of this board are numbered from 11 to nn. The cell on the intersection of the rr-th row and cc-th column is denoted by (r,c)(r,c).

Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following 55 characters  — UU, DD, LL, RR or XX  — instructions for the player. Suppose that the current cell is (r,c)(r,c). If the character is RR, the player should move to the right cell (r,c+1)(r,c+1), for LL the player should move to the left cell (r,c−1)(r,c−1), for UU the player should move to the top cell (r−1,c)(r−1,c), for DD the player should move to the bottom cell (r+1,c)(r+1,c). Finally, if the character in the cell is XX, 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 n2n2 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)(r,c) she wrote:

  • a pair (xx,yy), meaning if a player had started at (r,c)(r,c), he would end up at cell (xx,yy).
  • or a pair (−1−1,−1−1), meaning if a player had started at (r,c)(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.

Input

The first line of the input contains a single integer nn (1≤n≤1031≤n≤103)  — the side of the board.

The ii-th of the next nn lines of the input contains 2n2n integers x1,y1,x2,y2,…,xn,ynx1,y1,x2,y2,…,xn,yn, where (xj,yj)(xj,yj) (1≤xj≤n,1≤yj≤n1≤xj≤n,1≤yj≤n, or (xj,yj)=(−1,−1)(xj,yj)=(−1,−1)) is the pair written by Alice for the cell (i,j)(i,j).

Output

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 ii-th of the next nn lines, print the string of nn characters, corresponding to the characters in the ii-th row of the suitable board you found. Each character of a string can either be UU, DD, LL, RR or XX. If there exist several different boards that satisfy the provided information, you can find any of them.

Examples

input

Copy

2
1 1 1 1
2 2 2 2

output

Copy

VALID
XL
RX

input

Copy

3
-1 -1 -1 -1 -1 -1
-1 -1 2 2 -1 -1
-1 -1 -1 -1 -1 -1

output

Copy

VALID
RRD
UXD
ULL

Note

For the sample test 1 :

The given grid in output is a valid one.

  • If the player starts at (1,1)(1,1), he doesn't move any further following XX and stops there.
  • If the player starts at (1,2)(1,2), he moves to left following LL and stops at (1,1)(1,1).
  • If the player starts at (2,1)(2,1), he moves to right following RR and stops at (2,2)(2,2).
  • If the player starts at (2,2)(2,2), he doesn't move any further following XX and stops there.

The simulation can be seen below :

f596571dc15c869f9d93cc890d61b0d0e477ff99.pnguploading.4e448015.gif正在上傳…重新上傳取消

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)(2,2), keeps moving in an infinitely long cycle and never stops. Had he started at (2,2)(2,2), he wouldn't have moved further following instruction XX .

The simulation can be seen below :

691fa09405fd11b8b2a954bc17e4a3186d0e85e8.pnguploading.4e448015.gif正在上傳…重新上傳取消

 

題意:給你一個數n(n<=1e3),表示有一個n*n的棋盤。接下讓你給每個格子填字母。填X,則一旦走到這個格子就會停止。填L,R,U,D分別對應向 左,右,上,下 的相鄰格子走。現在給你所有格子最終會停在哪個格子中(若爲-1 -1表示永遠不能停止),讓你給出一個合法的填字母方式,或者判定答案不存在。

思路:

對於停在自己所在格子的格子,顯然填X即可。我們把這些格子加入到隊列中,然後從這些格子依次往外延伸,直到無法再繼續填字母。至於-1無法停止的情況,隨便找兩個構成死循環(LR或UD),再讓其他-1的格子走到這些格子即可。最後判斷是否所有格子都已經被填滿。

代碼:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=1e3+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k;
int a[maxn][maxn];
int ans,tmp,cnt;
int flag;
char s[maxn][maxn];
vector<pair<int,int> >vc;
int dxy[]={0,1,0,-1,1,0,-1,0};
char sxy[]={'R','D','L','U'};
char ssxy[]={'L','U','R','D'};
int main()
{
    int T,cas=1;
    //scanf("%d",&T);
    //while(T--)
    {
        flag=1;
        scanf("%d",&n);
        rep(i,1,n)
        rep(j,1,n) s[i][j]='O';
        queue<int>q;
        rep(i,1,n)
        rep(j,1,n)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(x==-1) a[i][j]=-1;
            else a[i][j]=(x-1)*n+y;
            if(x==i&&y==j) {s[x][y]='X';q.push((x-1)*n+y);}
            else if(x==-1) vc.push_back({i+j,(i-1)*n+j});
        }
        while(!q.empty()){
            int val=q.front();q.pop();
            int x=(val-1)/n+1;
            int y=val-(x-1)*n;
            rep(k,0,3){
                int nx=x+dxy[k];
                int ny=y+dxy[k+4];
                if(nx>=1&&nx<=n&&ny>=1&&ny<=n){
                        if(a[nx][ny]==a[x][y]&&s[nx][ny]=='O') {
                            s[nx][ny]=ssxy[k];
                            q.push((nx-1)*n+ny);
                    }
                }
            }
        }
        for(int i=0;i<vc.size();i++){
            int x=(vc[i].second-1)/n+1;
            int y=vc[i].second-(x-1)*n;
            rep(k,0,3){
                int nx=x+dxy[k];
                int ny=y+dxy[k+4];
                if(nx>=1&&nx<=n&&ny>=1&&ny<=n){
                        if(a[nx][ny]==-1&&s[nx][ny]!='O') {
                            s[x][y]=sxy[k];break;
                        }
                        else if(a[nx][ny]==-1&&s[nx][ny]=='O') {
                            s[x][y]=sxy[k];
                            s[nx][ny]=ssxy[k];
                            break;
                        }
                }
            }
        }
        rep(i,1,n)rep(j,1,n) if(s[i][j]=='O') {flag=0;}
        if(!flag) puts("INVALID");
        else {
            puts("VALID");
            rep(i,1,n){
                s[i][n+1]='\0';
                printf("%s\n",s[i]+1);
            }
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章