求调
查看原帖
求调
1528563
lyt_tcsn楼主2025/2/8 10:09
// P8642
#include<bits/stdc++.h>
using namespace std;
int n, a[30], b[30], g[30][30];
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int vis[30][30];
vector<int > path;
bool check() {
	for (int i = 1; i <= n; i++) {
		if (!a[i] || !b[i]) return false;
	}
	return true;
}
void dfs(int x, int y) {
	if (x == n && y == n) {
		if (check()) {
			for (int p : path) cout << p << " ";
			exit(0);
		}
		return;
	}
	for (int i = 0; i < 4; i++) {
		int tx = x + dx[i], ty = y + dy[i];
		if (tx>=1&&ty>=1&&tx<=n&&ty<=n&&(!vis[tx][ty])&&a[ty]&&b[tx]) {
			vis[tx][ty] = 1;
			path.push_back(g[tx][ty]);
			a[ty]--, b[tx]--;
			dfs(tx, ty);
			vis[tx][ty] = 0;
			path.pop_back();
			a[ty]++, b[tx]++;
		}
	}
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	int k = 0;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			g[i][j] = k++;
	vis[1][1] = 1;
	path.push_back(0);
	a[1]--, b[1]--;
	dfs(1, 1);
	return 0;
}
// 0 4 5 1 2 3 7 11 10 9 13 14 15
2025/2/8 10:09
加载中...