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

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();
}
全部评论

相关推荐

10-29 18:20
济南大学 Java
无语的太平湖水怪在看...:刷KPI的吧
点赞 评论 收藏
分享
美团开奖了,谁说测开比后端薪资低?谁说前端比后端薪资低?好了你又要说后端可以争取sp、ssp,但是能拿到美团白菜offer的就已经算是人中龙凤了,拿到sp、ssp更是凤毛麟角!依旧劝退后端!你后端学历内卷炼狱!实习经历卷的爆!甚至无法入行!入行了也只是和测开、前端的一般!1.学历,最痛的一击!后端工程师的第一步,走得不是技术,而是学历!想要进入大厂?好好看清楚自己的身份证:没有名校背景,别想着进美团、字节、腾讯!&nbsp;面试官看你的第一眼就会想:“呵,去,给你点面试机会,看看你的技术!”什么?你说自己有技术?不好意思,来点GitHub链接,Project经历,能让面试官笑着赶你走。你没个985、211,双一流,根本就无法站稳在这场技术竞赛的起点。你想进大厂,没学历,没技术!永远只有一个词——&nbsp;“被无情拒绝”。2.&nbsp;薪资:你不过是和前端、测开的一匹马“后端工程师薪资高?能进SSP就是牛逼!”SSP?&nbsp;听起来像是你梦想的银河,但实际上能拿到这个级别的人&nbsp;凤毛麟角,除非你在面试官面前像神话人物一样打了个响指,否则你连SSP的尾巴都摸不着。至于你说的“前端薪资不高”?别逗了,前端都在笑你呢,&nbsp;他们搞个页面,工资比你写个亿级请求接口还多。你说你辛辛苦苦优化API、调度缓存,别人搞个UI设计就能多拿几千块。前端已经不止是个展示层了,他们赚得比你还轻松,而你不过是服务器上疯狂跑“CRUD操作”的那只笨重的工蚁。3.&nbsp;后端的真正意义:修&nbsp;Bug,解决问题,下一份工作还是修&nbsp;Bug有多少人觉得后端是系统架构、数据库优化的高端战场?醒醒吧!&nbsp;后端的真正使命:维护旧项目,修复别人留下的烂摊子。你觉得自己能构建一个完美的系统?不!你只会一边修复技术债务,一边打着&nbsp;“重构”&nbsp;的旗号,换来的是&nbsp;“重构再重构”&nbsp;的无尽循环。而且,别告诉我你能专心写代码。你又要写代码,又要看服务器日志,没事还得帮别人&nbsp;修崩的数据库,给前端数据源做“格式化”。你就是那块永远处于消耗型工作的&nbsp;“万金油”。4.&nbsp;晋升?哈哈哈,你是在做梦!你以为后端开发是一条顺风顺水的快速晋升路线?错!&nbsp;你永远只能在一个“程序员”的岗位上打转,或者你为自己设立目标:“我要成为架构师”,那真的是在妄想。架构师?高级开发?靠近那条道路,你的心脏会先被晋升难度给捏住,你前方只有一座座高不可攀的技术山。别看那些SSP,架构师,架构啥呀?公司里的架构都是前端架构师,你就坐在后端的角落里,照顾着你那些满是错误的API和服务器。5.&nbsp;加班?还是加班!你以为后端开发能像文艺片那样“偶尔加个班”?哈哈,傻了吧!&nbsp;后端开发的生活是无休止的加班和修bug,你不仅要写接口,还得守夜调度、监控系统性能。就连你写的那个“完美的数据库查询”,也可能在&nbsp;第二天&nbsp;被前端因为“页面卡顿”给打回原形。“没有加班,你还能吃什么饭?”你说你是程序员,结果你的生活全是&nbsp;熬夜加班、调试、重启。前端跑个页面,喝个咖啡就能过关,而你呢,熬夜跟数据库调试,最后还是那个穷忙的死循环。6.&nbsp;技术天花板:架构?技术深度?笑死了!后端开发的天花板?那不过是个永远也摸不着的架构师“梦想”,你能掌握几款框架、几种数据库、两三套微服务架构,最后也不过是个&nbsp;管理端的“搬运工”。你没办法“打破天花板”,更没有机会跳出“自己写个爬虫”或者“API接口”的死循环。技术深度?你也不过是&nbsp;“技术债务”的修复者,一天到晚都在修补“老旧系统”的缺陷,偶尔听前端同学聊聊他们React、Vue的最新版本,你根本无法理解他们说的是什么。
开心小狗🐶:感觉后端有点像考研的0812,报名的时候都想冲0812,看不上0854。但是真入学了,不都是众生平等
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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