Codeforces Round #734 (Div. 3)-D2. Domino (hard version)
传送门 Time Limit: 1 second Memory Limit: 256 megabytes
Problem Description
The only difference between this problem and D1 is that you don’t have to provide the way to construct the answer in D1, but you have to do it in this problem.
There’s a table of
n
×
m
n \times m
n×m cells (
n
n
n rows and
m
m
m columns). The value of
n
?
m
n \cdot m
n?m is even.
A domino is a figure that consists of two cells having a common side. It may be horizontal (one of the cells is to the right of the other) or vertical (one of the cells is above the other).
You need to place
n
m
2
\frac{nm}{2}
2nm? dominoes on the table so that exactly
k
k
k of them are horizontal and all the other dominoes are vertical. The dominoes cannot overlap and must fill the whole table.
Input
The first line contains one integer
t
t
t (
1
≤
t
≤
10
1 \le t \le 10
1≤t≤10) — the number of test cases. Then
t
t
t test cases follow.
Each test case consists of a single line. The line contains three integers
n
n
n,
m
m
m,
k
k
k (
1
≤
n
,
m
≤
100
1 \le n,m \le 100
1≤n,m≤100,
0
≤
k
≤
n
m
2
0 \le k \le \frac{nm}{2}
0≤k≤2nm?,
n
?
m
n \cdot m
n?m is even) — the count of rows, columns and horizontal dominoes, respectively.
Output
For each test case:
Sample Input
8
4 4 2
2 3 0
3 2 3
1 2 0
2 4 2
5 2 2
2 17 16
2 1 1
Sample Onput
YES
accx
aegx
bega
bdda
YES
aha
aha
YES
zz
aa
zz
NO
YES
aaza
bbza
NO
YES
bbaabbaabbaabbaay
ddccddccddccddccy
NO
题目大意
D2和D1的唯一区别就是D2需要输出具体如何放置。
解题思路
既然D1已经知道如何放置了,那么D2就简单了,只需要用程序生成放置信息就行了。 根据图着色问题的四色定理,4种字母其实就够了。但是为什么要那么逼自己呢,多几种字母它不香吗?
因此采用水平的骨牌用a,b;竖直的骨牌用c,d;特殊一点的骨牌用e,f。
- 水平骨牌用a,b,可以看这个水平骨牌是所有水平骨牌中的第几行第几列。行+列 为偶数就用a,奇数就用b。
- 竖直的同理,行+列 为偶数就用c,奇数就用d。
- 特殊的特殊在哪里呢,特殊在放完水平骨牌但是没把一列放满的地方,这些地方也要放竖直骨牌,但是其实位置不好确定,容易与后面的骨牌颜色相同。总之这一小部分用ef就行了。
具体如何实现可以参考一下代码。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
char c[105][105];
void prt(char c[][105], int n, int m)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
putchar(c[i][j]);
}
puts("");
}
}
void prt(char c[][105], int n, int m, bool inverse)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
putchar(c[j][i]);
}
puts("");
}
}
void analyse(int n, int m, int k, bool inverse)
{
if(k%2)
{
puts("NO");
}
else
{
int lie=k/n;
lie += (k%n!=0);
if(lie*2>m)
{
puts("NO");
}
else
{
puts("YES");
for(int i=0;i<n;i++)
{
for(int j=0;j<lie-1;j++)
{
c[i][j*2]=c[i][j*2+1]=(i+j)%2?'b':'a';
}
}
int remain=k-(lie-1)*n;
for(int i=0;i<remain;i++)
{
int j=lie-1;
c[i][j*2]=c[i][j*2+1]=(i+j)%2?'b':'a';
}
if(lie>0)
{
int lieRemain=n-remain;
for(int i=0;i*2<lieRemain;i++)
{
int j=(lie-1)*2;
c[i*2+remain][j]=c[i*2+1+remain][j]=(i+j)%2?'d':'c';
c[i*2+remain][j+1]=c[i*2+1+remain][j+1]=(i+j+1)%2?'d':'c';
}
}
for(int j=lie*2;j<m;j++)
{
for(int i=0;i*2<n;i++)
{
c[i*2][j]=c[i*2+1][j]=(i+j)%2?'e':'f';
}
}
if(inverse)
{
prt(c, n, m, 1);
}
else
{
prt(c, n, m);
}
}
}
}
int main()
{
int N;
cin>>N;
while(N--)
{
int n,m,k;
cin>>n>>m>>k;
if(n%2==0)
{
analyse(n,m,k,0);
}
else
{
swap(n,m);
k=n*m/2-k;
analyse(n,m,k,1);
}
}
return 0;
}
原创不易,转载请附上原文链接哦~ Tisfy:https://letmefly.blog.csdn.net/article/details/119065553
|