开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第25天,点击查看活动详情
【Codeforces】Codeforces Round #682 (Div. 2)C. Engineer Artem | 思维
题目链接
题目
Artem is building a new robot. He has a matrix consisting of rows and columns. The cell located on the -th row from the top and the -th column from the left has a value 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 that satisfies the following condition —
For all valid , either or .
For the constraints of this problem, it can be shown that such a matrix 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.
题目大意
给定一个 行 列的矩阵 。自顶向下第 行和自左向右第 列的单元格里有值 。
如果没有两个相邻单元格包含相同的值,则矩阵是好的,如果两个单元格共享一条边,则称其为相邻。对于给定的矩阵 ,找到满足以下条件的矩阵 :
- 矩阵 是好的矩阵。
- 对于所有单元格 ,要么 ,要么 .
可以证明这样的矩阵 总是存在的。如果有多个可能的答案,输出其中的任何一个,不必最小化增量的数量。
思路
我们的目标是让矩阵任意两个相邻的格子上的值不一样。容易发现我们的操作是独立为每个单元格选择是否增加 ,那么我们就可以独立的为每个单元格选择奇偶性。
我们可以将整个棋盘如下图所示进行黑白染色:
其中黑色格子放奇数,白色格子放偶数,就可以保证任意两个相邻的格子里的数字均不相同了。
代码
#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;
}