[牛客] 2020牛客暑期多校训练营(第六场) G Grid Coloring

【题目】:

Roundgod draws a grid graph of size  nn with n \times nn×n cells. She can use one of kk colors to color every edge once, but lzr gives her some limits.
  1. lzr loves balance. All colors should appear in the same number of times.
  2. lzr loves complexity. The graph should not contain any monochromatic cycle.
  3. lzr hates monotone. Each whole horizontal or vertical line of the graph should contain at least two colors.
Roundgod is so divine that she doesn't want to waste her god's power to solve this problem. Could you give her a solution?

输入描述:

The input contains multiple test cases. The first line of input contains one integer T\ (1\le T\le100)T (1T100).
In the following TT lines, each line contains two integers n,k (1<= n <=200,1
<=k<=2(n+1)n)n,k (1n200,1k2(n+1)n) describing one test case.

输出描述:

For each test case, if there's no solution, please output "-1".
Otherwise, output 2(n+1)2(n+1) lines.
For the first  n+1n+1 lines, each line contains nn integers, denoting colors of edges on every horizontal line.
For the last  n+1n+1 lines, each line contain nn integers, denoting colors of edges on every vertical line.
示例1

输入

复制
2
2 3
2 5

输出

复制
1 2
3 1
3 2
1 3
2 1
2 3
-1

【题意】:

给一个n*n的田字格,用k种颜色给格子边涂色

要求:

1.每个颜色涂的边数相同

2.没有单色环

3.没有单色行,没有单色列

给出每条边的涂色

题解:

一个其实非常简单但就是没想到的构造
满足要求肯定要 n*(n+1)*2) | k  并且k!=1,n!=1(每个水平竖直边至少两个色
然后1~k顺次给一横排的水平边涂色,满足横边至少两色的要求
再1~k顺次给一横排的竖直边涂色,满足了每个小格不是同色
这样可能出现一列边同色,当(n+1)|k 时,这样每一横排都能放同样数量的不同颜色,只要让开始的边颜色和上面一排不同就行了

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int const maxn=205;
 4 int flag,a[maxn][maxn],n,k,b[maxn][maxn];
 5 void work(){
 6 /*
 7 一个其实非常简单但就是没想到的构造
 8 满足要求肯定要 n*(n+1)*2) | k  并且k!=1,n!=1(每个水平竖直边至少两个色
 9 然后1~k顺次给一横排的水平边涂色,满足横边至少两色的要求
10 再1~k顺次给一横排的竖直边涂色,满足了每个小格不是同色
11 这样可能出现一列边同色,当(n+1)|k 时,这样每一横排都能放同样数量的不同颜色,只要让开始的边颜色和上面一排不同就行了
12 */
13     if((n*(n+1)*2)%k||n==1||k==1){
14         //注意判n=1和k=1的情况下
15         flag=0;
16         return ;
17     }
18     int now=0;
19     flag=1;
20     for(int i=1;i<=n+1;i++){
21         for(int j=1;j<=n;j++){
22             now++;
23             if(now>k)now=1;
24             a[i][j]=now;
25         }
26     }
27     for(int i=1;i<=n;i++){
28         if((n+1)%k==0){
29             if(i==1)now=0;
30             else now=b[i-1][1];
31         }
32         for(int j=1;j<=n+1;j++){
33             now++;
34             if(now>k)now=1;
35             b[i][j]=now;
36         }
37     }
38 }
39 void print(){
40     if(!flag){
41         printf("-1\n");return;
42     }
43     for(int i=1;i<=n+1;i++){
44         for(int j=1;j<=n;j++){
45             printf("%d ",a[i][j]);
46         }
47         printf("\n");
48     }
49     for(int i=1;i<=n+1;i++){
50         for(int j=1;j<=n;j++){
51             printf("%d ",b[j][i]);
52         }
53         printf("\n");
54     }
55 }
56 int main(){
57     int t;
58     scanf("%d",&t);
59     while(t--){
60         scanf("%d%d",&n,&k);
61         work();
62         print();
63     }
64     return 0;
65 }

猜你喜欢

转载自www.cnblogs.com/conver/p/13396799.html