在模拟赛里打到这个题然后吃到了场上最优解,写篇题解庆祝一下。
题意
要求你用 0,1,2,3,4 去填满 的表格,满足每个数字 周围有 个和它相同的数字。
分析
因为题目中的条件,我们知道如果当前的数字为 4,则它周围四个点都为 4 时才可以满足条件,因此 4 就会无限延伸下去,直到碰到边框,而边框上最多只有三个点,所以 4 是不可能填进去的。同理的 3 在网格中也会无限延伸直至碰到四角条件不成立,3 也是不可能出现在里面的。
问题简化了,因为只有 0,1,2 是可以填到网格里的,我们从小样例开始入手。
当 时,2 是不能使用的(和上一段 3,4 不能使用的原因同理),我们很容易构造出下面的表格:
| 0 | 1 | 1 | 0 | 1 | 1 | …… |
|---|
当 时,2 我们能也只能按照 的方式填入表格:
| 2 | 2 |
|---|---|
| 2 | 2 |
对于每组的 2,我们需要在中间加入东西来间隔,我们考虑加入两个 1,往复就构造出了下面的表格:
| 1 | 2 | 2 | 1 | 2 | 2 | …… |
|---|---|---|---|---|---|---|
| 1 | 2 | 2 | 1 | 2 | 2 | …… |
这个时候我们发现上文构造的两种表格 合到一起时依旧是成立的,我们还可以把它拆分成 的小单元:
| 0 | 1 | 1 |
|---|---|---|
| 1 | 2 | 2 |
| 1 | 2 | 2 |
把他们排列起来只能解决 都为 3 的倍数时的求解,但是我们根据他们取模 3 的余数来旋转每个小单元,然后单独地加上行列,于是我们就构造出了答案(为方便理解,下面是 时的表格变化)。
| 0 | 1 | 1 |
|---|---|---|
| 1 | 2 | 2 |
| 1 | 2 | 2 |
| 1 | 1 | 0 |
|---|---|---|
| 2 | 2 | 1 |
| 2 | 2 | 1 |
| 1 | 1 | 0 | 1 | 1 |
|---|---|---|---|---|
| 2 | 2 | 1 | 2 | 2 |
| 2 | 2 | 1 | 2 | 2 |
| 1 | 1 | 0 | 1 | 1 |
|---|---|---|---|---|
| 2 | 2 | 1 | 2 | 2 |
| 2 | 2 | 1 | 2 | 2 |
| 1 | 1 | 0 | 1 | 1 |
代码
#include<iostream>
#include<cstdio>
using namespace std;
int n,m;
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
//写得有些长,因为分了9种情况,但是每一个并不难
cin>>n>>m;
if(n%3==0&&m%3==0){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"0 1 1 ";
}
cout<<'\n';
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<'\n';
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<'\n';
}
}
if(n%3==1&&m%3==0){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"0 1 1 ";
}
cout<<'\n';
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<'\n';
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<'\n';
}
for(int j=1;j<=m/3;j++){
cout<<"0 1 1 ";
}
cout<<'\n';
}
if(n%3==0&&m%3==1){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"0 1 1 ";
}
cout<<"0\n";
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
}
}
if(n%3==1&&m%3==1){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"0 1 1 ";
}
cout<<"0\n";
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
}
for(int j=1;j<=m/3;j++){
cout<<"0 1 1 ";
}
cout<<"0\n";
}
if(n%3==0&&m%3==2){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
for(int j=1;j<=m/3;j++){
cout<<"1 1 0 ";
}
cout<<"1 1\n";
}
}
if(n%3==2&&m%3==0){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"\n";
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"\n";
for(int j=1;j<=m/3;j++){
cout<<"1 1 0 ";
}
cout<<"\n";
}
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"\n";
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"\n";
}
if(n%3==2&&m%3==2){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
for(int j=1;j<=m/3;j++){
cout<<"1 1 0 ";
}
cout<<"1 1\n";
}
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
}
if(n%3==1&&m%3==2){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"1 1 0 ";
}
cout<<"1 1\n";
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
for(int j=1;j<=m/3;j++){
cout<<"2 2 1 ";
}
cout<<"2 2\n";
}
for(int j=1;j<=m/3;j++){
cout<<"1 1 0 ";
}
cout<<"1 1\n";
}
if(n%3==2&&m%3==1){
for(int i=1;i<=n/3;i++){
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
for(int j=1;j<=m/3;j++){
cout<<"0 1 1 ";
}
cout<<"0\n";
}
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
for(int j=1;j<=m/3;j++){
cout<<"1 2 2 ";
}
cout<<"1\n";
}
return 0;
}