B. Neighbor Grid

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.
题目的大意是给出一个二维数组,其中里面的元素>0,表示与这个元素相邻的元素大于0的个数,要求不全这个二维数组。
思路:暴力枚举每一个大于0的元素,判断它是否大于4,如果大于了就不符合,因为一个元素最多就四个角与自己相邻。找到一个大于0的元素,判断四周,统计一下,四个方向0的个数,和非0的个数,如果0和非0的个数总和小于这个元素的值,也是不符合题意的,然后这个元素的四周放上1,知道放上去1的个数和原来非0的个数之和等于这个元素值是,就退出。最后为了防止边边角角新增添的1而漏加的,总的进行一次操作。
代码:

#include<bits/stdc++.h>
using namespace std;
const int N=310;
int a[N][N];
int t,n,m;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof a);
        scanf("%d%d",&n,&m);
        int f=0;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        scanf("%d",&a[i][j]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
        {
            if(a[i][j]>4){
                f=1;break;
            }
            else if(a[i][j]>0){
                int l=0,r=0;
                for(int k=0;k<4;k++)
                {
                    int x=i+dx[k],y=j+dy[k];
                    if(x>=1&&x<=n&&y>=1&&y<=m)
                    {
                        if(a[x][y]>0)l++;
                        else r++;
                    }
 
                }   
                if(a[i][j]<=l)a[i][j]=l;//四周的非0元素的个数大于这个数的,则更新
                else if(a[i][j]>l+r){
                        f=1;break;
                    }
 
                else if(a[i][j]>l){
                    int s=a[i][j];
                    for(int k=0;k<4;k++)
                    {
                        int x=i+dx[k],y=j+dy[k];
                        if(x>=1&&x<=n&&y>=1&&y<=m)
                        {
                            if(a[x][y]==0){
                                a[x][y]=1;
                            }
                           s--;
                        }
                        //元素等于新增添的1的个数和原来非0的个数时,就完成操作,退出
                        if(s==0)break;
                    }
                }
            }
            if(f==1)break;
 
        }
        if(f==1)break;
        }
        if(f){
            printf("NO\n");continue;
        }
        printf("YES\n");
                //防止某个元素漏加的,就进行总的查询,保证没有任何漏加的元素
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]>0){
                    int l=0;
                    for(int k=0;k<4;k++)
                    {
 
                        int x=i+dx[k],y=j+dy[k];
                        if(x>=1&&x<=n&&y>=1&&y<=m)
                        {
                            if(a[x][y]>0)l++;
                        }
 
                    }
                    a[i][j]=l;
                }
                printf("%d ",a[i][j]);
            }
 
            printf("\n");
        }
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Version 13.0.2.13 (4/16/2021) TwwDataGrid - Performance with most recent OS X/BigSur was noticeably slower in painting/scrolling. We recommend adding the following line of code to your project source before the initialize which uses metal graphics and is faster to paint. ​begin FMX.Types.GlobalUseMetal := True; // add this line. Application.Initialize; Application.CreateForm(TMainDemoForm, MainDemoForm); end; ​ Note that also this build has a new method of painting the default text in the grid that is significantly faster even when metal is not used. If there are any unforeseen issues, you can return to the old behavior by adding this line of code in your main project source. ​ wwGlobalManager.InfoPowerControlsManager.TextPaintingNew:= false; Application.Initialize; ​ Note: You will need to add the units fmx.types to resolve the GlobalUseMetal reference and add fmx.wwmanager to resolve the TextPaintingNew reference. TwwDataGrid - Footer painting would incorrectly clear grid's data rows in some cases such as when the Imagecontrol property was assigned as the background image.. This problem is fixed. TwwLayoutGrid/TwwDataGrid - Retina painting on Mac OS X was not as sharp before for custom controls. TwwLayoutGrid - TwwEdit embedded in layout grid with a different font size (overridestylesettings.fontsize assigned) would paint text too large before in OSX. This problem is fixed. TwwListBox - Right Accessory icon would disappear if changing visibility of controls within the listbox during runtime. This problem is fixed.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值