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

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

题目描述

Roundgod draws a grid graph of size n×n cells. She can use one of k 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≤T≤100).
In the following T lines, each line contains two integers n,k (1≤n≤200,1≤k≤2n(n+1)) describing one test case.

输出描述

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

样例输入

2
2 3
2 5

样例输入

1 2
3 1
3 2
1 3
2 1
2 3
-1

题目大意

Roundgod绘制了一个包含n*n格子的图,他可以用k种颜色,对每一条边进行染色,但有一些限制:

  1. 每种颜色必须出现相同次数
  2. 图上不能有单色环
  3. 每一行和每一列,至少包含2种颜色。
    在这里插入图片描述
    请帮Roundgod构造这个方格图。

题解

在这里插入图片描述

AC Code

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

猜你喜欢

转载自blog.csdn.net/cxkdad/article/details/107642946
今日推荐