CF1863D-Two-Colored Dominoes

CF1863D-Two-Colored Dominoes

题目大意

你有一个 \(n*m\) 的棋盘,这些棋盘上有一些多米诺骨牌,每个骨牌覆盖相邻的两个格子,没有两个骨牌会重叠。

你要把这些骨牌涂上黑白两种颜色,并满足以下条件

· 对于每个多米诺骨牌,其中一个涂成白色,另一个涂成黑色。

· 对于每一行(列),这一行中黑色格子的数量等于白色格子的数量

请将所有骨牌涂色并输出涂色方案,如果不可能满足以上条件,则输出 \(-1\)

题解

如果一行或一列上有奇数个格子被覆盖,则一定不可能满足。

否则,那么占据同两行,或同两列的骨牌数量一定为偶数,只需要将这些骨牌,交替黑白涂色即可。

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define umap unordered_map
#define endl '\n'
using namespace std;
using i128 = __int128;
const int mod =1e9+7;
template <typename T>void read(T&x){
    x=0;int f = 1;
    char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    x*=f;
}
template <typename T>void print(T x) {
     if (x < 0) { putchar('-'); x = -x; }
     if (x > 9) print(x / 10);
     putchar(x % 10 + '0');
}
#define int long long
const int N=500005;
const int M=2000005;
inline void solve()
{
	int n,m;
	cin>>n>>m;
	vector<string> s(n);
	vector<int> c(m+1),r(n+1);
	for(int i=0;i<n;i++) cin>>s[i];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(s[i][j]=='L')
			{
				if(c[j]%2==0)
				{
					s[i][j]='W';
					s[i][j+1]='B';
				}
				else
				{
					s[i][j]='B';
					s[i][j+1]='W';
				}
				c[j]++;
			}
			if(s[i][j]=='U')
			{
				if(r[i]%2==0)
				{
					s[i][j]='W';
					s[i+1][j]='B';
				}
				else
				{
					s[i][j]='B';
					s[i+1][j]='W';
				}
				r[i]++;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		if(r[i]%2)
		{
			cout<<-1<<endl;
			return;
		}
	}
	for(int i=0;i<m;i++)
	{
		if(c[i]%2)
		{
			cout<<-1<<endl;
			return;
		}
	}
	for(int i=0;i<n;i++) cout<<s[i]<<endl;
}

signed main()
{
	ios;
	int T=1;
	cin>>T;
	for(;T--;) solve();
	return 0;
}

posted @ 2025-11-22 00:10  NDAKJin  阅读(4)  评论(0)    收藏  举报