1. 程式人生 > 實用技巧 >codeforces 1438 C. Engineer Artem (構造)

codeforces 1438 C. Engineer Artem (構造)

題目連結: https://codeforces.com/contest/1438/problem/C

可以加 \(1\) 或者不變,意味著我們可以改變奇偶性
將矩形黑白染色,令不同顏色的格子奇偶性不同即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 110;

int T;
int n, m; 
int a[maxn][maxn], b[maxn][maxn]; 

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
	T = read();
	while(T--){
		n = read(), m = read();
		for(int i = 1 ; i <= n ; ++i){
			for(int j = 1 ; j <= m ; ++j){
				a[i][j] = read();
			}
		}
		
		for(int i = 1 ; i <= n ; ++i){
			for(int j = 1 ; j <= m ; ++j){
				if(((i + j) & 1) == (a[i][j] & 1)){
					b[i][j] = a[i][j];
				} else{
					b[i][j] = a[i][j] + 1;
				}
			}
		} 
		
		for(int i = 1 ; i <= n ; ++i){
			for(int j = 1 ; j <= m ; ++j){
				printf("%d ", b[i][j]);
			} printf("\n");
		}
	} 
		
	return 0;
}