CodeForces 1316D Nash Matrix

Description

There is a play with $ n \ times n $ grid game.

  • When the $ (i, j) $ is filled L , you will come to $ (i, j - 1) $;
  • When the $ (i, j) $ filling is R , you will come to $ (i, j + 1) $;
  • When the $ (i, j) $ is filled U , you will come to $ (i - 1, j) $;
  • When the $ (i, j) $ filling is D , you will come to $ (i + 1, j) $;
  • When the $ (i, j) $ is filled X , you will not move.

When you're starting from $ $ (i, j), the last stop will be in $ (x _ {(i, j)}, y _ {(i, j)}) $. Or, if you never stop, $ (x _ {(i, j)}, y _ {(i, j)}) $ $ is (- 1, -1) $.

All now given $ (x _ {(i, j)}, y _ {(i, j)}) $, try to construct the original grid. If you can not be constructed, the output INVALID , otherwise the output VALID , and the output of one kind together trellis.

Solution

Two parts, a first part of the processing node will be terminated, the second portion of the processing node does not terminate

Imagine, for a $ (i, j) $, stopped at $ (ex _ {(i, j)}, ey _ {(i, j)}) $, then, let's pull out just one path, we will know, each point on the path stops in $ {(EX _ {(I, J)}, EY _ {(I, J)})} $ , so this figure must be a number of the communication block, one of the communication within the block stopping point point should be the same.

We find all the self-stop point, which is $ (i, j) = ( ex _ {(i, j)}, ey _ {(i, j)}) $, obviously this point should be the X- , then we start it in answer to meet the stopping point is configured to communicate within its block. It's very simple, as long as we DFS again, the direction of a point of access to all turn on the line.

void DFS (int p, int q, int sx, int sy) // currently in (p, q), all processing is stopped points (sx, sy) of
{
	REP (i, 0, 3) // Enumeration direction, dx [], dy [] is an array of incremental
	{
		int tx = p + dx[i], ty = q + dy[i];
		if(tx >= 1 && tx <= n && ty >= 1 && ty <= n && // 不越界
		  ex [tx] [ty] == sx && ey [tx] [ty] == sy &&! ans [tx] [ty]) // termination point (sx, sy) && no access
		{
			ans [tx] [ty] = dc [i]; // dc [] is stored in the reverse direction
			DFS (TX ty, sx);
		}
	}
}

Then the case of $ -1 $. $ $ -1 if a communication block size is $ 1 $, then the result must be INVALID , also because how a point can not do without stopping. Otherwise, we choose two adjacent points in this communication block, they point to the other side, and then let the other $ -1 $ will go to the two points above, the communication block it illegal.

This requires DFS to write one kind of it? The answer is no, we DFS above parameters $ (sx, sy) \ gets (-1, -1) $, is looking for $ -1 $ automatically block the communication.

Do not forget the usual routine questions of construction: construction completed before testing again.

#include <bits/stdc++.h>
#define REP(i, x, y) for(register int i = x; i <= y; i++)
using namespace std;
const int N = 1e3 + 5;
const int dx[4] = {0, 1, 0, -1};
const int that [4] = {1, 0, -1, 0};
const char dc[4] = {'L', 'U', 'R', 'D'};
int n, ex[N][N], ey[N][N];
char years [N] [N];
DFS void (you p, q you, you sx, sy you)
{
    REP(i, 0, 3)
    {
        int tx = p + dx[i], ty = q + dy[i];
        if(tx >= 1 && tx <= n && ty >= 1 && ty <= n &&
          ex[tx][ty] == sx && ey[tx][ty] == sy && !ans[tx][ty])
        {
            ans[tx][ty] = dc[i];
            DFS (TX ty, sx);
        }
    }
}
int main ()
{
    scanf("%d", &n);
    REP(i, 1, n) REP(j, 1, n) scanf("%d %d", &ex[i][j], &ey[i][j]);
    REP(i, 1, n) REP(j, 1, n)
        if(ex[i][j] == i && ey[i][j] == j)
            ans[i][j] = 'X', DFS(i, j, i, j); 
    REP(i, 1, n) REP(j, 1, n)
    {
        if(ex[i][j] == -1 && !ans[i][j])
        {
            // note that in both cases there can be, as in the case ...- 1 when i = i-1 / j = j-1 to the enumeration 
            if(ex[i + 1][j] == -1) 
                years old [i] [j] = 'D', years [i + 1] [j] = 'U', DFS (i, j, -1, -1), DFS (i + 1, j, -1, -1);
            if(ex[i][j + 1] == -1) 
                years old [i] [j] = 'R', years [i] [j + 1] = 'L', DFS (i, j, -1, -1), DFS (i, j + 1, -1, -1);
        }
    }
    bool invalid = false;
    REP(i, 1, n) REP(j, 1, n) invalid |= !ans[i][j];
    if(invalid) return puts("INVALID") && 0;
    puts("VALID");
    REP(i, 1, n)
    {
        REP(j, 1, n) putchar(ans[i][j]);
        puts("");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/syksykCCC/p/CF1316D.html