Description
要求用 1×1×1 的白色块和黑色块组成一个 k×k×k 的立方体,使得与每一个块相邻(共面)的块中与其颜色相同的只有两个
Input
一个整数 k(1≤k≤100)
Output
如果有解则输出任意一组,即输出
k
个
Sample Input
2
Sample Output
bb
ww
bb
ww
Solution
k
为奇数,则必然有一个颜色的块有奇数个,把每个该种颜色的块看作点,相连关系看作边,则该种颜色的块构成一张图,每个点度数为2,必成若干环,且对于一个环,任选一个点,从这个点开始遍历这个环回到这个点,每一步都会让这个点的某一维坐标变化
k
为偶数,有两种构造方法,一个是一圈黑一圈白这样往中间填,然后相邻两个面把颜色反转一下,另一种是依次放
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
int main()
{
int k;
while(~scanf("%d",&k))
{
if(k&1)printf("-1\n");
else
{
char B='b',W='w';
for(int x=0;x<k;x++)
{
for(int y=0;y<k;y++)
{
for(int z=0;z<k;z++)
if((y/2+z/2)&1)printf("%c",B);
else printf("%c",W);
printf("\n");
}
printf("\n");
swap(B,W);
}
}
}
return 0;
}