【Codeforces】Codeforces Round #682 (Div. 2)C. Engineer Artem | 思维

156 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第25天,点击查看活动详情

【Codeforces】Codeforces Round #682 (Div. 2)C. Engineer Artem | 思维

题目链接

Problem - 1438C - Codeforces

题目

Artem is building a new robot. He has a matrix aa consisting of nn rows and mm columns. The cell located on the ii-th row from the top and the jj-th column from the left has a value ai,ja_{i,j} written in it.

If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side.

Artem wants to increment the values in some cells by one to make a good.

More formally, find a good matrix bb that satisfies the following condition —

For all valid (i,j)(i,j), either bi,j=ai,jb_{i,j}=a_{i,j} or bi,j=ai,j+1b_{i,j}=a_{i,j}+1.

For the constraints of this problem, it can be shown that such a matrix bb always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments.

题目大意

给定一个 nnmm 列的矩阵 aa。自顶向下第 ii 行和自左向右第 jj 列的单元格里有值 aija_{i,j}

如果没有两个相邻单元格包含相同的值,则矩阵是好的,如果两个单元格共享一条边,则称其为相邻。对于给定的矩阵 aa,找到满足以下条件的矩阵 bb

  • 矩阵 bb 是好的矩阵。
  • 对于所有单元格 (i,j)(i,j),要么 bi,j=ai,jb_{i,j}=a_{i,j},要么 bi,j=ai,j+1b_{i,j}=a_{i,j}+1.

可以证明这样的矩阵 bb 总是存在的。如果有多个可能的答案,输出其中的任何一个,不必最小化增量的数量。

思路

我们的目标是让矩阵任意两个相邻的格子上的值不一样。容易发现我们的操作是独立为每个单元格选择是否增加 11,那么我们就可以独立的为每个单元格选择奇偶性。

我们可以将整个棋盘如下图所示进行黑白染色:

image.png

其中黑色格子放奇数,白色格子放偶数,就可以保证任意两个相邻的格子里的数字均不相同了。

代码

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
using LL=long long;
const int N=1e6+5;
//const LL mod=998244353;
const LL mod=1e9+7;
int n,m,k;
LL solve()
{
	scanf("%d%d",&n,&m);
	for (int i=1,x;i<=n;++i,printf("\n"))
		for (int j=1;j<=m;++j)
		{
			scanf("%d",&x);
			if ((i+j)&1)
				if (x&1) printf("%d ",x+1);
				else printf("%d ",x);
			else
				if (x&1) printf("%d ",x);
				else printf("%d ",x+1);
				
		}
	return 0;
}
int main()
{
	int T=1;
	scanf("%d",&T); 

	while (T--) solve();
	return 0;
}