M - Matrix Problem

A teacher gives his students a problem to test his students' construction skills.

The teacher has two 0 / 10/1 matrices AA and BB with nn rows and mm columns, where all the 11 of both matrices separately form a 44-connected block. "Form one 4-connected block" means that starting at any 11, you can reach all the 11 by moving up, down, right or left, and you can not move into 00.

Now the teacher gives a third matrix CC with nn rows and mm columns, and a specific position of this matrix is 11 if and only if the corresponding positions of the two matrices are 11.

You need to find two matrices AA and BB such that one of the positions of CC is 11 if and only if the corresponding positions of A, BA,B are both 11.

Input

The first line contains two integers nn and mm (3 \le n, m \le 500)(3≤n,m≤500) – the size of the matrices.

The next nn lines describe the matrix CC. Each of the following nn lines contains mm characters 00 or 11.

It is guaranteed that the boundaries of the matrix CC are 00, i.e., the 11-st row, the 11-st column, the nn-th row and the mm-th column of the matrix CC are 00.

Output

Print 2n2n lines and each line has mm characters 00 or 11.

The first nn lines describe the matrix AA.

The next nn lines describe the matrix BB.

If there are multiple answers, print any of them.

Sample 1

InputcopyOutputcopy
5 5
00000
00100
01010
01100
00000
11110
10100
11110
11100
11110
00001
01111
01011
01111
00001

今天训练赛做到的题目。感谢张大佬和黄姐拖着我往前跑!

这道题目的意思就是给你一个矩阵q,这个矩阵的周围一圈都是'0', 然后让你写两个对应的矩阵a和b要求矩阵 q中为1的地方在a和b中都是1,然后剩下为零的地方a可以是1也可以是0, b中也可以是1也可以是0, 但是要求a和b如果在某处都为1,那么c中也为一, 换而言之就是q中是零的地方,a中可以是1, b可以是0, 或者a中为0, b中为1.要求最后a或者b中的1都是连通的。

根据题目给的样例,首先将a中的最左列处理为1, 最右列处理为0,将b中的最右边处理为1, 最左边处理为0, 因为矩阵q的最外围都是0.然后再去处理中间的部分, 然后为了方便他们联通, 让p为0在a和b中对应的点是相反的(1或0), 然后多次尝试就考虑到让他根据行的奇偶性去进行变换, 比如如果q对应的是零的话, 让a中的奇数行变成1, 让b的偶数行变为1,就可以了。

#include <iostream>
#include <string>

#define endl "\n"
using namespace std;
const int N = 510;

string q[N];

int main() {
//    ios_base::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);

    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= n; i++) cin >> q[i];

    for (int i = 1; i <= m - 1; i++) {
        putchar('1');
    }
    putchar('0');
    cout << endl;

    for (int i = 2; i <= n - 1; i++) {
        putchar('1');
        for (int j = 1; j < m - 1; j ++) {
            if (q[i][j] == '1') {
                putchar('1');
            } else {
                if (i & 1 == 1) {
                	putchar('1');
				} else {
					putchar('0');
				}
            }
        }
        putchar('0');
        cout << endl;
    }

    for (int i = 1; i <= m - 1; i++) {
        putchar('1');
    }
    putchar('0');
    cout << endl;

    for (int i = 1; i <= m - 1; i++) {
        putchar('0');
    }
    putchar('1');
    cout << endl;

    for (int i = 2; i <= n - 1; i++) {
        putchar('0');
        for (int j = 1; j < m - 1; j ++) {
            if (q[i][j] == '1') {
                putchar('1');
            } else {
                if (i & 1 == 1) {
                	putchar('0');
				} else {
					putchar('1');
				}
            }
        }
        putchar('1');
        cout << endl;
    }

    for (int i = 1; i <= m - 1; i++) {
        putchar('0');
    }
    putchar('1');
    cout << endl;
}

感谢大佬带我飞~~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值