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;
}