跳到正文

题解

题解:QOJ10102 Puzzle: Nurikabe

drmr约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年2月28日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 12 分钟

版本

  1. v1当前

题意:给定一个只有一个线索的 『ぬりかべ』 谜题。构造一个解法或报告无解。

题解:首先先围着外圈依次染黑。具体的,对于所有 (i,j)(i, j) 满足 i=1∨i=n∨j=1∨j=mi = 1 \vee i = n \vee j = 1 \vee j = m,有当 (i,j)(i, j) 非为给定位置时,将其涂黑。特殊的,当给定位置 (x,y)(x, y) 在矩形的四角上时,需要按照放置方式撤销一格。

接下来枚举其中是以行为单位操作,还是以列为单位操作。以行为例,先将所有奇数或偶数行染黑,若剩余两行未染黑,则进行以列为单位的操作。

需要注意实现细节。

C++
#include <bits/stdc++.h>
using namespace std;
#define a(x, y) (_a[(x) * m + (y)])
#define bk(x, y) (_bk[(x) * m + (y)])

const int N = 3e6;
int x, y, c, f1, f2, n, m, p[N], _a[N], _bk[N], A, B, C;
char ch[] = {'.', '#'};

inline void output(){
	if(f1)
		for(int i = 1, j = n; i < j; i++, j--)
			for(int k = 1; k <= m; k++)
				swap(a(i, k), a(j, k));
	if(f2)
		for(int i = 1; i <= n; i++)
			for(int j = 1, k = m; j < k; j++, k--)
				swap(a(i, j), a(i, k));

	cout << "Yes\n";
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++)
			cout << ch[a(i, j)];
		cout << "\n";
	}
	return ;
}

inline void bup(){
	A = x, B = y, C = c;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			bk(i, j) = a(i, j);
	return ;
}
inline void cpy(){
	x = A, y = B, c = C;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			a(i, j) = bk(i, j);
	return ;
}

inline bool th(){
	basic_string<pair<int, int> > vec;
	for(int i = 2; i <= n; i++)
		vec.push_back(make_pair(i, m));
	for(int i = m - 1; i >= 2; i--)
		vec.push_back(make_pair(n, i));
	for(int i = 0; c && i < vec.size(); i++){
		if(vec[i].first == x && vec[i].second == y) break;
		if(x == n && y == m && vec[i].first == n && vec[i].second == m - 1) break;
		if(!a(vec[i].first, vec[i].second))
			a(vec[i].first, vec[i].second) = (c-- > 0);
	}
	for(int i = vec.size() - 1; c && i >= 0; i--){
		if(vec[i].first == x && vec[i].second == y) break;
		if(x == n && y == m && vec[i].first == n && vec[i].second == m - 1) break;
		if(!a(vec[i].first, vec[i].second))
			a(vec[i].first, vec[i].second) = (c-- > 0);
	}
	if(c <= 0){output(); return 1;}

	for(int i = n - 2; i >= 3; i -= 2){
		memset(p, 0, sizeof(int) * (m + 2));
		for(int j = 2; j <= m - 2 && c > 0; j++)
			if(!a(i, j + 1) && a(i, j - 1))
				if(i != x || j != y)
					a(i, j) = (c-- > 0);
		for(int j = m - 1; j >= 3 && c > 0 && !a(i, j); j--)
			if(!a(i, j - 1) && a(i, j + 1))
				if(i != x || j != y)
					a(i, j) = (c-- > 0);
	}
	if(c <= 0){output(); return 1;}

	if((n & 1 ^ 1) && n >= 4){
		for(int j = 3; j <= m - 2 && c > 0; j += 2){
			if(a(1, j) && (2 != x || j != y))
				a(2, j) = (c-- > 0);
			else if(a(4, j) && (3 != x || j != y))
				a(3, j) = (c-- > 0);
		}
	}
	if(c <= 0){output(); return 1;}
	return 0;
}

inline bool tw(){
	basic_string<pair<int, int> > vec;
	for(int i = 2; i <= m; i++)
		vec.push_back(make_pair(n, i));
	for(int i = n - 1; i >= 2; i--)
		vec.push_back(make_pair(i, m));
	for(int i = 0; c && i < vec.size(); i++){
		if(vec[i].first == x && vec[i].second == y) break;
		if(x == n && y == m && vec[i].first == n - 1 && vec[i].second == m) break;
		if(!a(vec[i].first, vec[i].second))
			a(vec[i].first, vec[i].second) = (c-- > 0);
	}
	for(int i = vec.size() - 1; c && i >= 0; i--){
		if(vec[i].first == x && vec[i].second == y) break;
		if(x == n && y == m && vec[i].first == n - 1 && vec[i].second == m) break;
		if(!a(vec[i].first, vec[i].second))
			a(vec[i].first, vec[i].second) = (c-- > 0);
	}
	if(c <= 0){output(); return 1;}

	for(int i = m - 2; i >= 3; i -= 2){
		memset(p, 0, sizeof(int) * (n + 2));
		for(int j = 2; j <= n - 2 && c > 0; j++)
			if(!a(j + 1, i) && a(j - 1, i))
				if(j != x || i != y)
					a(j, i) = (c-- > 0);
		for(int j = n - 1; j >= 3 && c > 0 && !a(j, i); j--)
			if(!a(j - 1, i) && a(j + 1, i))
				if(j != x || i != y)
					a(j, i) = (c-- > 0);
	}
	if(c <= 0){output(); return 1;}

	if((m & 1 ^ 1) && m >= 4){
		for(int j = 3; j <= n - 2 && c > 0; j += 2){
			if(a(j, 1) && (j != x || 2 != y))
				a(j, 2) = (c-- > 0);
			else if(a(j, 4) && (j != x || 3 != y))
				a(j, 3) = (c-- > 0);
		}
	}
	if(c <= 0){output(); return 1;}
	return 0;
}

inline void solve(){
	for(int i = 0; i <= n + 1; i++)
		for(int j = 0; j <= m + 1; j++)
			a(i, j) = bk(i, j) = 0;
	x = y = c = f1 = f2 = n = m = A = B = C = 0;

	cin >> n >> m;
	cin >> x >> y >> c;
	c = n * m - c;

	if(n == 1){
		if(y - 1 >= c){
			cout << "Yes\n";
			for(int i = 1; i <= c; i++)
				cout << "#";
			for(int i = c + 1; i <= m; i++)
				cout << ".";
			cout << "\n";
			return ;
		}
		if(m - y >= c){
			cout << "Yes\n";
			for(int i = 1; i <= m - c; i++)
				cout << ".";
			for(int i = m - c + 1; i <= m; i++)
				cout << "#";
			cout << "\n";
			return ;
		}
		cout << "No\n";
		return ;
	}
	if(m == 1){
		if(x - 1 >= c){
			cout << "Yes\n";
			for(int i = 1; i <= c; i++)
				cout << "#\n";
			for(int i = c + 1; i <= n; i++)
				cout << ".\n";
			return ;
		}
		if(n - x >= c){
			cout << "Yes\n";
			for(int i = 1; i <= n - c; i++)
				cout << ".\n";
			for(int i = n - c + 1; i <= n; i++)
				cout << "#\n";
			return ;
		}
		cout << "No\n";
		return ;
	}
	if(n == 2){
		if(x == 1) f1 = 1, x = 2;
		basic_string<pair<int, int> > vec;
		vec.push_back(make_pair(2, 1));
		for(int i = 1; i <= m; i++)
			vec.push_back(make_pair(1, i));
		vec.push_back(make_pair(2, m));
		for(pair<int, int> i : vec) if(!(i.first == x && i.second == y))
			a(i.first, i.second) = (c-- > 0);
		if(c <= 0) output();
		else cout << "No\n";
		return ;
	}
	if(m == 2){
		if(y == 1) f2 = 1, y = 2;
		basic_string<pair<int, int> > vec;
		vec.push_back(make_pair(1, 2));
		for(int i = 1; i <= n; i++)
			vec.push_back(make_pair(i, 1));
		vec.push_back(make_pair(n, 2));
		for(pair<int, int> i : vec) if(!(i.first == x && i.second == y))
			a(i.first, i.second) = (c-- > 0);
		if(c <= 0) output();
		else cout << "No\n";
		return ;
	}

	if(x <= 2) f1 = 1, x = n - x + 1;
	if(y <= 2) f2 = 1, y = m - y + 1;

	for(int i = 1; i <= m; i++)
		if(1 != x || i != y)
			a(1, i) = (c-- > 0);
	for(int i = 2; i <= n; i++)
		if(i != x || 1 != y)
			a(i, 1) = (c-- > 0);
	if(c <= 0){output(); return ;}

	bup();
	if(th()) return ;
	cpy();
	if(tw()) return ;
	cout << "No\n";
	return ;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int T; cin >> T;
	while(T--) solve();
	return 0;
}
原文