B. Square Filling

You are given two matrices AA and BB. Each matrix contains exactly nn rows and mm columns. Each element of AA is either 00 or 11; each element of BB is initially 00.

You may perform some operations with matrix BB. During each operation, you choose any submatrix of BB having size 2×22×2, and replace every element in the chosen submatrix with 11. In other words, you choose two integers xx and yy such that 1≤x<n1≤x<n and 1≤y<m1≤y<m, and then set Bx,yBx,y, Bx,y+1Bx,y+1, Bx+1,yBx+1,y and Bx+1,y+1Bx+1,y+1 to 11.

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

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

The first line contains two integers nn and mm (2≤n,m≤502≤n,m≤50).

Then nn lines follow, each containing mm integers. The jj-th integer in the ii-th line is Ai,jAi,j. Each integer is either 00 or 11.Output

If it is impossible to make BB equal to AA, print one integer −1−1.

Otherwise, print any sequence of operations that transforms BB into AA in the following format: the first line should contain one integer kk — the number of operations, and then kk lines should follow, each line containing two integers xx and yy for the corresponding operation (set Bx,yBx,y, Bx,y+1Bx,y+1, Bx+1,yBx+1,y and Bx+1,y+1Bx+1,y+1 to 11). The condition 0≤k≤25000≤k≤2500 should hold.ExamplesinputCopy

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


思路:类似封墙的思路,先一个个点进行操作,最后看是否能要的图对应。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100;
typedef long long LL;
LL a[maxn][maxn];
LL b[maxn][maxn];
LL vis[maxn][maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++)
  	for(LL j=1;j<=m;j++)
  		cin>>a[i][j];
  for(LL i=1;i<=n;i++)
  	for(LL j=1;j<=m;j++)
	  {
	 	  if(a[i][j]&&a[i][j+1]&&a[i+1][j]&&a[i+1][j+1])
		   {
		   		vis[i][j]=1;	
		   } 	
	  }		
   LL sum=0;
   for(LL i=1;i<=n;i++)
   	for(LL j=1;j<=m;j++)
	   	{
	   		if(vis[i][j])
			   {
			   		b[i][j]=1;b[i+1][j]=1;b[i][j+1]=1;b[i+1][j+1]=1;	
					sum++;
			   }	
		}  
	for(LL i=1;i<=n;i++)
		for(LL j=1;j<=m;j++)
		{
			if(a[i][j]!=b[i][j])
			{
				cout<<"-1"<<endl;
				return 0;	
			}	
		}	
	cout<<sum<<endl;
	for(LL i=1;i<=n;i++)
		for(LL j=1;j<=m;j++)
			if(vis[i][j]) cout<<i<<" "<<j<<endl;
				
return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值