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

18 篇文章 0 订阅
8 篇文章 0 订阅

D. Nash Matrix

- 题目描述了一个 n × n n\times n n×n,的矩阵, 给出每个格点自己终止与哪个格点, 或者自己永远不能停止, 让你构造一个矩阵满足上述要求

数据范围 1 ≤ n ≤ 1000 1\leq n\leq 1000 1n1000

Face

  • 前置技能 dfs / bfs

原以为是一定要成环, 然后考虑多种环交叉的情况, 结果只用保证不会停止就行了. 首先找到所有节点是自己的, 然后从该点开始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;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值