Codeforces-1207B Square Filling

Square Filling

time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

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 B having size 2×2, and replace every element in the chosen submatrix with 1. 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, y, Bx,y+1 , Bx+1,y and Bx+1,y+1 to 1.

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 A 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 A. Note that you don’t have to minimize the number of operations.

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

Then nn lines follow, each containing mm integers. The j-th integer in the i-th line is Ai,j. Each integer is either 0 or 1.

Output
If it is impossible to make B equal to A, print one integer −1.

Otherwise, print any sequence of operations that transforms B into A in the following format: the first line should contain one integer k — the number of operations, and then k lines should follow, each line containing two integers x and y for the corresponding operation (set Bx, y, Bx,y+1 , Bx+1,y and Bx+1,y+1 to 1). The condition 0≤k≤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

题意:

给定一个50乘50以内的矩阵,该矩阵内只能填入0或1,填好了之后再在一个相同的且全为零的矩阵内填入1,每次填入1是将当前位置的四周填为1(Bx, y, Bx,y+1 , Bx+1,y and Bx+1,y+1),如果填入最后能满足则输出有多少个满足的位置及每次对应的x,y位置。如果不满足则输出-1,如果两矩阵一样则输出0

思路:

直接暴力,先找到在原矩阵中满足条件的1,然后再给为零的相同矩阵的相应位置赋值为一,从每一行遍历直接暴力出位置,因为没有对有多少序列的要求,直接挨着遍历暴搜即可。

AC代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 55;
int a1[maxn][maxn];
int b1[maxn][maxn];
int ans[maxn][maxn];
int main()
{
	int flag = 0;
	int a, b;
	cin >> a >> b;
	memset(a1, 0, sizeof(a1));
	memset(b1, 0, sizeof(b1));
	memset(ans, 0, sizeof(ans));
	int cnt = 0;
	for (int i = 1; i <= a; i++)
	{
		for (int j = 1; j <= b; j++)
		{
			cin >> a1[i][j];

			/*if (a1[i][j] == 1)flag = 1;*/
		}
	}
	/*if (flag == 0)cout << 0 << endl;*/
	
	for (int i = 1; i <= a-1; i++)
	{
		for (int j = 1; j <= b-1; j++)
		{

			if (a1[i][j] == 1 && a1[i + 1][j + 1] == 1 && a1[i + 1][j] == 1 && a1[i][j + 1] == 1)//遍历a1中能满足条件的
			{
				ans[i][j] = 1;//记录当前更改的位置
				cnt++;//记录次数
				b1[i][j] = b1[i + 1][j + 1] = b1[i + 1][j] = b1[i][j + 1] = 1;
				//满足的话则将b1数组中相等的位置更改为1
			}
		}
	}

	for (int i = 1; i <= a; i++)
	{
		for (int j = 1; j <= b; j++)
		{

			if (a1[i][j] != b1[i][j])
			{//遍历查看是否相等
				cout << -1 << endl;
				return 0;
			}
			}
	}

	cout << cnt << endl;

	for (int i = 1; i <= a;i++)
	for (int j = 1; j <= b;j++)
	if (ans[i][j])
		cout << i << " " << j << endl;


	return 0;
}

发布了40 篇原创文章 · 获赞 10 · 访问量 2576

猜你喜欢

转载自blog.csdn.net/lsdstone/article/details/100610466