CF1207B

CF1207B-Square Filling

题意:

两个矩阵a,b,已知矩阵b,每次能修改b矩阵中相邻的四个格(b为空矩阵),使b变为a

解法:

枚举矩阵中的1,按题意修改,并把改过的四个点都标记一下。
注意每次枚举的点一定是未被标记过的,不然连pretest都过不去。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define LL long long
#define N 60

int a[N][N],b[N][N],m,n;

int main() {
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            scanf("%d",&a[i][j]);
        }
    }
    int ans = 0;
    for(int i = 1; i < n; i++) {
        for(int j = 1; j < m; j++) {
            if(a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1] == 4) {
                b[i][j] = b[i + 1][j] = b[i][j + 1] = b[i + 1][j + 1] = 1;
                ans++;
            }
        }
    }
    for(int i = 1; i <= n; i++)  {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] ^ b[i][j]) {
                puts("-1");
                return 0;
            }
        }

    }
    printf("%d \n",ans);
    for(int i = 1; i < n; i++) {
        for(int j = 1; j < m; j++) {
            if(a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1] == 4) {
                printf("%d %d \n",i,j);
            }
        }
    }
    //system("pause");
    return 0;
}
posted @ 2019-08-23 21:28  西窗夜雨  阅读(142)  评论(0编辑  收藏  举报