D. Nash Matrix
- 题目描述了一个
n
×
n
n\times n
n×n,的矩阵, 给出每个格点自己终止与哪个格点, 或者自己永远不能停止, 让你构造一个矩阵满足上述要求
数据范围
1
≤
n
≤
1000
1\leq n\leq 1000
1≤n≤1000
原以为是一定要成环, 然后考虑多种环交叉的情况, 结果只用保证不会停止就行了. 首先找到所有节点是自己的, 然后从该点开始dfs, 每次解决一个连通块儿, 对于是-1的点, 类似找连通块, 找到一个-1, 就看他相邻的有没有-1, 如果有自己就指向它, 如果没有就invalid
Attention:
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3fll
#define ll long long
#define db double
#define eps 1e-8
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define pi acos(-1.0)
const ll maxn = 1e3 + 10;
const ll mod = 1e9;
vector<vector<pii>> G(maxn, vector<pii>(maxn));
bool vis[maxn][maxn];
char mat[maxn][maxn];
ll n;
ll d[4][2]{ -1, 0, 1, 0, 0, 1, 0, -1 };
char x[]{ 'D', 'U', 'L', 'R' }, xx[]{ 'U', 'D', 'R', 'L' };
void dfs(ll curx, ll cury, char c) {
if (mat[curx][cury] != '\0')return;
mat[curx][cury] = c;
_for(i, 0, 4) {
ll tox = curx + d[i][0], toy = cury + d[i][1];
if (G[tox][toy].first == G[curx][cury].first && G[tox][toy].second == G[curx][cury].second)
dfs(tox, toy, x[i]);
}
}
bool merge(ll curx, ll cury, ll tox, ll toy, ll i) {
if (tox <= 0 || toy <= 0)return 0;
if (G[tox][toy].first != -1) {
return 0;
}
else {
mat[curx][cury] = xx[i];
return 1;
}
}
void nope() {
cout << "INVALID" << endl;
exit(0);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
vector<pii> end;
_rep(i, 1, n) {
_rep(j, 1, n) {
cin >> G[i][j].first >> G[i][j].second;
}
}
_rep(i, 1, n) {
_rep(j, 1, n) {
if (G[i][j].first == i && G[i][j].second == j)
dfs(i, j, 'X');
else if (G[i][j].first == -1) {
bool con = mat[i][j] != '\0';
_for(c, 0, 4) {
ll tox = i + d[c][0], toy = j + d[c][1];
if (!con) con |= merge(i, j, tox, toy, c);
}
if (!con) {
nope();
}
}
}
}
_rep(i, 1, n) {
_rep(j, 1, n) {
if (mat[i][j] == '\0')
nope();
}
}
cout << "VALID" << endl;
_rep(i, 1, n) {
cout << mat[i] + 1 << endl;
}
}