B. Square Filling(贪心,矩阵)

此生再无相见时 提交于 2019-11-28 08:32:15

B. Square Filling
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two matrices 𝐴 and 𝐵. Each matrix contains exactly 𝑛 rows and 𝑚 columns. Each element of 𝐴 is either 0 or 1; each element of 𝐵 is initially 0.

You may perform some operations with matrix 𝐵. During each operation, you choose any submatrix of 𝐵 having size 2×2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers 𝑥 and 𝑦 such that 1≤𝑥<𝑛 and 1≤𝑦<𝑚, and then set 𝐵𝑥,𝑦, 𝐵𝑥,𝑦+1, 𝐵𝑥+1,𝑦 and 𝐵𝑥+1,𝑦+1 to 1.

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

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

Input
The first line contains two integers 𝑛 and 𝑚 (2≤𝑛,𝑚≤50).

Then 𝑛 lines follow, each containing 𝑚 integers. The 𝑗-th integer in the 𝑖-th line is 𝐴𝑖,𝑗. Each integer is either 0 or 1.

Output
If it is impossible to make 𝐵 equal to 𝐴, print one integer −1.

Otherwise, print any sequence of operations that transforms 𝐵 into 𝐴 in the following format: the first line should contain one integer 𝑘 — the number of operations, and then 𝑘 lines should follow, each line containing two integers 𝑥 and 𝑦 for the corresponding operation (set 𝐵𝑥,𝑦, 𝐵𝑥,𝑦+1, 𝐵𝑥+1,𝑦 and 𝐵𝑥+1,𝑦+1 to 1). The condition 0≤𝑘≤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
Note
The sequence of operations in the first example:

000000000→110110000→110110110→110111111

题意: 二维矩阵a,b。a矩阵全是0,你每次可以在a里面指定一个2x2的矩阵全部赋为1。问能否使得a矩阵等于b矩阵

思路: 这题出了好几次了。。。
只需要贪心的选择b里面的2x2矩阵,记录下位置,在a里面相同位置染色为1。最后比对一下a和b矩阵即可。

#include <cstdio>
#include <algorithm>
#include <vector>
 
using namespace std;
const int maxn = 1e6 + 7;
 
int a[55][55],b[55][55];
int vis[55][55];
vector<pair<int,int> >ans;
 
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    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 + 1][j + 1] == 1 && a[i][j] == 1 && a[i + 1][j] == 1 && a[i][j + 1] == 1)
            {
                ans.push_back(make_pair(i,j));
            }
        }
    }
    
    for(int i = 0;i < ans.size();i++)
    {
        int x = ans[i].first;int y = ans[i].second;
        b[x][y] = 1;b[x + 1][y + 1] = 1;b[x + 1][y] = 1;b[x][y + 1] = 1;
    }
    
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            if(b[i][j] != a[i][j])
            {
                printf("-1\n");
                return 0;
            }
        }
    }
    
    printf("%d\n",ans.size());
    for(int i = 0;i < ans.size();i++)
    {
        printf("%d %d\n",ans[i].first,ans[i].second);
    }
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!