1. 程式人生 > 實用技巧 >牛客網Grid Coloring

牛客網Grid Coloring

連結:https://ac.nowcoder.com/acm/contest/5671/G
來源:牛客網

題目描述:

Roundgod draws a grid graph of size nnn with n×nn \times nn×n cells. She can use one of kkk 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 hatesmonotone. 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≤T≤100)T\ (1\le T\le100)T(1T100).

In the following TTT lines, each line contains two integers n,k(1≤n≤200,1≤k≤2(n+1)n)n,k\ (1\le n\le200,1\le k\le2(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)2(n+1) lines.
For the firstn+1n+1n+1 lines, each line contains nnn integers, denoting colors of edges on every horizontal line. For the lastn+1n+1n+1 lines, each line contain nnn integers, denoting colors of edges on everyverticalline. 翻譯自行解決。 分析:

構造題。

首先判斷是否存在構造。當n=1n=1時,不能滿足條件3,當k=1k=1時,不能滿足條件2和3,當(2n(n+1))modk0(2∗n∗(n+1))modk≠0時,不能滿足條件1。對以上三種情況進行特判,輸出1−1。

然後將行和列提取出來,將行構成一個(n+1)n(n+1)∗n的表,將列構成一個n(n+1)n∗(n+1)的表。

對於奇偶不同的nn,分類討論即可。

下方程式碼

#include<bits/stdc++.h>
using namespace std;
int l;
int n,k,t;
int a[210][210];
int b[210][210];
int main()
{
    scanf("%d",&l);
    while(l--)
    {
        scanf("%d%d",&n,&k);
        if (n==1 || k==1 || (2*n*(n+1))%k)
        {
            printf("-1\n");
            continue;
        }
        t=0;
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<n; j++)
            {
                t=t%k+1;
                a[i][j]=t;
            }
            for (int j=0; j<=n; j++)
            {
                t=t%k+1;
                b[j][i]=t;
            }
        }
        for (int i=0; i<n; i++)
        {
            t=t%k+1;
            a[n][i]=t;
        }
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<n; j++)
                printf("%d ",a[i][j]);
            cout<<endl;
        }
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<n; j++)
                printf("%d ",b[i][j]);
            cout<<endl;
        }
    }
    return 0;
}
View Code