题目链接:https://codeforces.com/contest/1375/problem/B
题意
给出一个 $n imes m$ 的方阵,每个方格中有一个非负整数,一个好方格定义如下:
- 方格中的数字等于四周数字大于 $0$ 的方格总数
可以增加任意方格中的数字任意次,判断能否将初始方阵变为全是好方格的方阵。
题解
初始方阵合法的条件:
- 四个角的方格数字不能超过 $2$
- 四条边的方格数字不能超过 $3$
- 角边外的方格数字不能超过 $4$
若初始方阵合法将每个方格中的数字变为与它相邻的方格个数即可。
代码
#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" << " "; return; } MP[i][j] = adj; } } cout << "YES" << " "; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cout << MP[i][j] << " "[j == m - 1]; } int main() { int t; cin >> t; while (t--) solve(); }