牛客網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.
- lzr loves balance. All colors should appear in the same number of times.
- lzr loves complexity. The graph should not contain any monochromatic cycle.
- 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(1≤T≤100).
輸出描述:
For each test case, if there's no solution, please output "-1". Otherwise, output 2(n+1)2(n+1)2(n+1) lines.構造題。
首先判斷是否存在構造。當n=1n=1時,不能滿足條件3,當k=1k=1時,不能滿足條件2和3,當(2∗n∗(n+1))modk≠0(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