Codeforces B. Neighbor Grid 1375

You are given a grid with 𝑛 rows and 𝑚 columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number 𝑘>0 written on it, then exactly 𝑘 of its neighboring cells have a number greater than 0 written on them. Note that if the number in the cell is 0, there is no such restriction on neighboring cells.

You are allowed to take any number in the grid and increase it by 1. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.

Two cells are considered to be neighboring if they have a common edge.

Input
The input consists of multiple test cases. The first line contains an integer 𝑡 (1≤𝑡≤5000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers 𝑛 and 𝑚 (2≤𝑛,𝑚≤300) — the number of rows and columns, respectively.

The following 𝑛 lines contain 𝑚 integers each, the 𝑗-th element in the 𝑖-th line 𝑎𝑖,𝑗 is the number written in the 𝑗-th cell of the 𝑖-th row (0≤𝑎𝑖,𝑗≤109).

It is guaranteed that the sum of 𝑛⋅𝑚 over all test cases does not exceed 105.

Output
If it is impossible to obtain a good grid, print a single line containing “NO”.

Otherwise, print a single line containing “YES”, followed by 𝑛 lines each containing 𝑚 integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero).

If there are multiple possible answers, you may print any of them.

Example
inputCopy
5
3 4
0 0 0 0
0 1 0 0
0 0 0 0
2 2
3 0
0 0
2 2
0 0
0 0
2 3
0 0 0
0 4 0
4 4
0 0 0 0
0 2 0 1
0 0 0 0
0 0 0 0
outputCopy
YES
0 0 0 0
0 1 1 0
0 0 0 0
NO
YES
0 0
0 0
NO
YES
0 1 0 0
1 4 2 1
0 2 0 0
1 3 1 0
Note
In the first test case, we can obtain the resulting grid by increasing the number in row 2, column 3 once. Both of the cells that contain 1 have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid

0100
0210
0000
All of them are accepted as valid answers.

In the second test case, it is impossible to make the grid good.

In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.

亂搞,這場全是亂搞。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <map>
 
using namespace std;
 
typedef long long ll;
 
const int maxn = 305;
int a[maxn][maxn];
int n,m;
 
bool check1(int x,int y) {
    if(x == 1 && y == 1) return true;
    if(x == 1 && y == m) return true;
    if(x == n && y == 1) return true;
    if(x == n && y == m) return true;
    return false;
}
 
bool check2(int x,int y) {
    if(x == 1 || x == n || y == 1 || y == m) return true;
    return false;
}
 
int main() {
    int T;scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        int flag = 1;
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= m;j++) {
                scanf("%d",&a[i][j]);
                if(check1(i,j) && a[i][j] > 2) {
                    flag = 0;
                } else if(check2(i,j) && a[i][j] > 3) {
                    flag = 0;
                } else if(a[i][j] > 4) {
                    flag = 0;
                }
            }
        }
        if(flag) {
            printf("YES\n");
            for(int i = 1;i <= n;i++) {
                for(int j = 1;j <= m;j++) {
                    if(check1(i,j)) {
                        printf("2 ");
                    } else if(check2(i,j)) {
                        printf("3 ");
                    } else {
                        printf("4 ");
                    }
                }
                printf("\n");
            }
        } else {
            printf("NO\n");
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章