相邻网格——输出任意答案的思路

B. Neighbor Grid
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given a grid with n rows and m 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 k>0 written on it, then exactly k 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 t (1≤t≤5000) — the number of test cases. The description of the test cases follows.

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

The following n lines contain m integers each, the j-th element in the i-th line ai,j is the number written in the j-th cell of the i-th row (0≤ai,j≤109).

It is guaranteed that the sum of n⋅m 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 n lines each containing m 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.

这道题目的意思是给你一个n*m的大方格,每个小方格里面有个数,你可以通过增加任意小方格的数使这个大方格满足:如果这个数字是非0的,那么这个数的值大小就代表和它相邻有几个填有非0的小方格。如果可以达到就输出这个大方格的所有数,并输出YES,不能就输出NO。

这道题一开始我是打算遍历,然后动态的增加数,但会很复杂。但使这道题有个使他变得简单的条件——输出任意符合条件的方格就行。那么我们只需要输出一个一定可以满足的,不需要那么麻烦。

那么就可以直接输出有多少个cell和该cell相邻,每一个数都达到最大,如果原方格的数比它相邻的cell的数要多,就不能达到这个要求,那么就始终无法满足条件,其他情况时无论何时全取最大肯定能满足。

这给我们一个启示,如果要你输出任意的一个答案,就选一个最容易的方案去得到。
代码:

#include<iostream>
#include<algorithm>
#include<math.h>

using namespace std;

int a[305][305];
int main()
{
    int t,n,m,x;
    cin>>t;
    while(t--)
    {
        bool flag=true;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>x;
                if((i==1||i==n)&&(j==1||j==m))
                a[i][j]=2;
                else if(i==1||j==1||i==n||j==m)
                a[i][j]=3;
                else 
                a[i][j]=4;
                if(a[i][j]<x)
                {
                    flag=false;
                }
            }
        }
        if(flag)
        {
            cout<<"YES"<<endl;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    cout<<a[i][j]<<" ";
                }
                cout<<endl;
            }
        }
        else 
        cout<<"NO"<<endl;

    }
    getchar();
    getchar();
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
2025-12-04 02:00
offer1:字节跳动(北京)-&nbsp;后端开发岗-&nbsp;薪资:总包42w(基本工资30w+绩效6w+年终奖6w),15薪,加班费按法定标准发放-&nbsp;福利:公积金按12%缴纳,无宿舍,每月住房补贴2000元,餐补1500元,每年2次体检,免费健身房-&nbsp;工作强度:996是常态,忙的时候可能到凌晨,团队节奏快,压力大-&nbsp;其他:平台大,技术氛围浓,晋升路径清晰,对转行选手来说履历加分多,但北京生活成本高,租房压力大offer2:美团(上海)-&nbsp;客户端开发岗-&nbsp;薪资:总包38w(基本工资26w+绩效5w+年终奖7w),14薪,加班无加班费,可调休-&nbsp;福利:公积金按10%缴纳,无宿舍,每月住房补贴1800元,餐补800元,每年1次体检,节日福利丰富-&nbsp;工作强度:995为主,偶尔周末加班,项目紧急时会通宵,整体压力中等-&nbsp;其他:公司业务成熟,行业地位稳固,客户端岗位需求稳定,上海生活节奏比北京稍缓,但租房成本仍较高offer3:网易(杭州)-&nbsp;测试开发岗-&nbsp;薪资:总包32w(基本工资22w+绩效4w+年终奖6w),13薪,加班较少,无加班费-&nbsp;福利:公积金按12%缴纳,提供员工宿舍(单人间,前两年免费,第三年按市场价5折),每月餐补1000元,每年1次体检+1次旅游补贴-&nbsp;工作强度:965为主,几乎无强制加班,团队氛围轻松,摸鱼文化盛行-&nbsp;其他:杭州生活成本低于北上,宿舍省房租,测试开发岗入门难度低,适合转行过渡,但技术成长速度可能不如开发岗,未来跳槽竞争力未知本人情况:传统工科转行,编程基础一般,想快速提升技术能力,同时也希望工作生活能平衡,未来不确定是否留在一线城市。有没有同款转行选手或互联网前辈给点建议呀?
森七菜:梦到什么说什么属于是
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务