Square Filling

题目链接:http://codeforces.com/contest/1207/problem/B

                                                                              Square Filling

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
input
3 3
1 1 1
1 1 1
0 1 1
output
3
1 1
1 2
2 2
input
3 3
1 0 1
1 0 1
0 0 0
output
-1
input
3 2
0 0
0 0
0 0
output
0

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

思路:只需要贪心的选择b里面的2x2矩阵,记录下位置,在b里面相同位置变为1。最后比对一下a和b矩阵即可。

#include<bits/stdc++.h>
using namespace std;
int a[55][55], b[55][55];
vector<pair<int, int>> ans;
int main()
{
	ios::sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	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]) {
				ans.push_back(make_pair(i, j));
				b[i][j] = 1, b[i + 1][j] = 1, b[i][j + 1] = 1, b[i + 1][j + 1] = 1;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (a[i][j] != b[i][j]) {
				cout << -1 << endl;
				return 0;
			}
		}
	}
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
		cout << ans[i].first << ' ' << ans[i].second << endl;
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值