Educational Codeforces Round 71 (Rated for Div. 2) B

You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0.

You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2×2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1≤x

Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B.

Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don’t have to minimize the number of operations.

Input
The first line contains two integers n and m (2≤n,m≤50).

Then n lines follow, each containing m integers. The j-th integer in the i-th line is Ai,j. Each integer is either 0 or 1.

Output
If it is impossible to make B equal to A, print one integer −1.

Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k — the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set Bx,y, Bx,y+1, Bx+1,y and Bx+1,y+1 to 1). The condition 0≤k≤2500 should hold.

Examples
inputCopy
3 3
1 1 1
1 1 1
0 1 1
outputCopy
3
1 1
1 2
2 2
inputCopy
3 3
1 0 1
1 0 1
0 0 0
outputCopy
-1
inputCopy
3 2
0 0
0 0
0 0
outputCopy
0

题意:有两个n*m的矩阵A,B,矩阵B里面的数字全是0,矩阵A是输入的矩阵,问是否能通过变换把矩阵B变成矩阵A,变换规则是 选择矩形B的一个数字,假设坐标是x,y,那么你可以把(x,y)(x+1,y)(x,y+1)(x+1,y+1)这四个位置的0变成1
思路:直接判断矩阵A的某个位置的四个变换中是否都是1,如果都是1的话直接把矩阵B相对应的四个位置变成1就可以

#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const int maxn=10005;
int v[55][55],s[55][55],mapp[55][55];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int a=1;a<=n;a++)
    {
        for(int b=1;b<=m;b++)
        {
            scanf("%d",&v[a][b]);
        }
    }
    int ans=0;
    for(int a=1;a<n;a++)
    {
        for(int b=1;b<m;b++)
        {
            if(v[a][b]&&v[a+1][b]&&v[a+1][b+1]&&v[a][b+1])
            {
                mapp[a][b]=1;
                s[a][b]=1;
                s[a+1][b]=1;
                s[a+1][b+1]=1;
                s[a][b+1]=1;
                ans++;
            }
        }
    }
    int flog=0;
    for(int a=1;a<=n;a++)
    {
        for(int b=1;b<=m;b++)
        {
            if(v[a][b]!=s[a][b])
            {
                printf("-1\n");
              flog=1;
              break;
            }
        }
        if(flog)
            break;
    }
    if(!flog)
    {
        printf("%d\n",ans);
     for(int a=1;a<=n;a++)
    {
        for(int b=1;b<=m;b++)
        {
            if(mapp[a][b])
                printf("%d %d\n",a,b);
        }
    }
    }
}

你可能感兴趣的:(#,【模拟】,#,【构造】)