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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章