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("");
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值