Matrix Problem

6 篇文章 0 订阅
4 篇文章 0 订阅

Matrix Problem

题目描述

A teacher gives his students a problem to test his students’ construction skills.
The teacher has two 0/1 matrices A and B with n rows and m columns, where all the 1 of both matrices separately form a 4-connected block. 4-connected means that each cell is connected with adjacent cells in four directions (up, down, leftward and rightward).
Now the teacher gives a third matrix C with n rows and m columns, and a specific position of this matrix is 1 if and only if the corresponding positions of the two matrices are 1.
You need to find two matrices A and B such that one of the positions of C is 1 if and only if the corresponding positions of A,B are both 1.

输入描述

The first line contains two integers n and m (3≤n,m≤500) – the size of the matrices.
The next n lines describe the matrix C. Each of the following n lines contains m characters 0 or 1.
It is guaranteed that the boundaries of the matrix C are 0, i.e., the 1-st row, the 1-st column, the n-th row and the m-th column of the matrix C are 0.

输出描述

Print 2n lines and each line has m characters 0 or 1.
The first n lines describe the matrix A.
The next n lines describe the matrix B.
If there are multiple answers, print any of them.

样例输入

5 5
00000
00100
01010
01100
00000

样例输出

00000
00100
01110
01100
00000
00000
01110
01010
01110
00000

题目大意

给定一个目标矩阵C,这个矩阵保证四边都是0。现在你需要用两个矩阵AB去构造C。AB矩阵相与之后得到的矩阵要构成C矩阵。AB矩阵中的所有1都是联通的上下左右有相连。SPJ构造AB矩阵并输出。

题目思路

向鱼骨一样错开,比如
111111 000001
10000 1111111
111111 000001
10000 1111111
111111 000001

对于每一行的0中可以选择是否搭建成为一个1。思路成立,代码如下

通过代码

//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
	return a * b / gcd(a, b);
}
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a) {
	if (a < 0)putchar('-'), a = -a;
	if (a > 9)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
#define read read()
char save[505][505];
char A[505][505];
char B[505][505];
int main()
{
	//freopen("C:\\Users\\wyb2001\\Desktop\\temp.txt", "w", stdout);
	int n, m;
	cin >> n >> m;
	string temp;
	for (int i = 0; i < n; i++)
		cin >> save[i];
	for (int i = 0; i < n; i++)
	{
		A[i][0] = B[i][m - 1] = '1';
		A[i][m - 1] = B[i][0] = '0';
	}
	for (int i = 0; i < n; i++)
	{
		if ((i & 1) == 0)
			for (int j = 1; j < m - 1; j++)
				A[i][j] = '1';
		else
			for (int j = 1; j < m - 1; j++)
				if (save[i][j] == '1')A[i][j] = '1';
				else A[i][j] = '0';
	}
	for (int i = 0; i < n; i++)
	{
		if (i & 1)
			for (int j = 1; j < m - 1; j++)
				B[i][j] = '1';
		else
			for (int j = 1; j < m - 1; j++)
				if (save[i][j] == '1')B[i][j] = '1';
				else B[i][j] = '0';
	}
	for (int i = 0; i < n; i++)
		cout << A[i] << endl;
	for (int i = 0; i < n; i++)
		cout << B[i] << endl;
}

By-轮月

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值