题目链接:https://codeforces.com/contest/1375/problem/B
给出一个 $n \times m$ 的方阵,每个方格中有一个非负整数,一个好方格定义如下:
可以增加任意方格中的数字任意次,判断能否将初始方阵变为全是好方格的方阵。
初始方阵合法的条件:
若初始方阵合法将每个方格中的数字变为与它相邻的方格个数即可。
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; int MP[n][m] = {}; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> MP[i][j]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int adj = 4 - (i == 0) - (i == n - 1) - (j == 0) - (j == m - 1); if (MP[i][j] > adj) { cout << "NO" << "\n"; return; } MP[i][j] = adj; } } cout << "YES" << "\n"; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cout << MP[i][j] << " \n"[j == m - 1]; } int main() { int t; cin >> t; while (t--) solve(); }
Codeforces Global Round 9 B. Neighbor Grid
原文:https://www.cnblogs.com/Kanoon/p/13258084.html