CodeCraft-20 (Div. 2) D. Nash Matrix /詳解

D. Nash Matrix
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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×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.

Input
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).

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 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.

Examples
inputCopy
2
1 1 1 1
2 2 2 2
outputCopy
VALID
XL
RX
inputCopy
3
-1 -1 -1 -1 -1 -1
-1 -1 2 2 -1 -1
-1 -1 -1 -1 -1 -1
outputCopy
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), 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 :
在這裏插入圖片描述
題意:
給出n*n的網格,再給出每個點的終點,-1 -1爲永不停止的運動,問你是否能構造出這樣的地圖。

思路:

  1. 比賽的時候想到的是以x開始搜下去,但是不會處理重疊路徑的問題。
  2. 講一下思路吧,在輸入的時候處理好終點是自身的點,也就是X點,然後從這些X點開始dfs下去,搜完後就是那些跟X相關的點搜完了,接下來就是處理循環-1的點了。
  3. 如果只有單獨-1的點,那麼肯定無法構造出來,所以在遍歷到終點爲-1的點時,要繼續判斷一下四周是否存在同樣終點爲-1的點,如果有纔可以dfs下去。
  4. 值得注意的是,我們在從X點dfs下去時,因爲是從四周走到X點,所以賦值是逆向的,而從-1的點dfs下去時,因爲是從本點走向四周,所以-1 -1自身的點賦值是正向的,四周-1 -1的點纔是逆向的。
  5. 如果有什麼講的不清楚的地方歡迎提出來,一起交流~

代碼:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define mp make_pair
#define pb push_back
#define ins insert
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
using namespace std;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;}void put1(){ puts("YES") ;}void put2(){ puts("NO") ;}
void debug(){printf("T   A   T\n");}void put3(){ puts("-1"); }ll qpf(ll a, ll b, ll p)
{ll ret = 0;while(b){if(b & 1) ret = (ret + a) % p;a = (a + a) % p;b >>= 1;}
return ret % p ;}ll qp(ll a, ll n, ll p){ll ret = 1;while(n){if(n & 1) ret = qpf(ret, a, p);
a = qpf(a, a, p);n >>= 1;}return ret % p ;}//��=acos(L/2R);
//void say(){ cout<<"I CAN AC"<<endl;}
 
const int manx=1e3+5;
 
char c[manx][manx];
pair<ll,ll>a[manx][manx];
ll dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
char t1[4]={'R','L','D','U'},t2[4]={'L','R','U','D'};
ll n;
 
bool check(ll x,ll y){
    if(x<1||y<1||x>n||y>n||c[x][y]!='0') return true;
    return false;
}
 
void dfs(ll x,ll y,ll fx,ll fy){
    for(int i=0;i<4;i++){
        ll xx=x+dx[i],yy=y+dy[i];
        if(check(xx,yy)) continue;
        if(a[xx][yy].fi==fx&&a[xx][yy].se==fy)
            c[xx][yy]=t2[i],dfs(xx,yy,fx,fy);
    }
}
 
int main(){
    n=read();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            a[i][j].fi=read(),a[i][j].se=read();
            if(a[i][j].fi==i&&a[i][j].se==j) c[i][j]='X';
            else c[i][j]='0';
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(c[i][j]=='X')
                dfs(i,j,i,j);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            if(c[i][j]!='0') continue;
            if(a[i][j].fi!=-1&&a[i][j].se!=-1) continue;
            bool flag=0;
            for(int k=0;k<4;k++){
                ll x=i+dx[k],y=j+dy[k];
                if(check(x,y)) continue;
                if(a[x][y].fi==-1&&a[x][y].se==-1){
                    flag=1;
                    c[i][j]=t1[k];
                    break;
                }
            }
            if(flag) dfs(i,j,-1,-1);
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(c[i][j]=='0'){
                puts("INVALID");
                return 0;
            }
    puts("VALID");
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            printf("%c",c[i][j]);
        printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章