[暴力] Educational Codeforces Round 71 (Rated for Div. 2) B. Square Filling (1207B)

 
B. 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 A

and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0

.

You may perform some operations with matrix B

. 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 x and y such that 1x<n and 1y<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 B

equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B

.

Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B

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 (2n,m50

).

Then n

lines follow, each containing m 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 0k2500

should hold.

Examples
Input
Copy
3 3
1 1 1
1 1 1
0 1 1
Output
Copy
3
1 1
1 2
2 2
Input
Copy
3 3
1 0 1
1 0 1
0 0 0
Output
Copy
-1
Input
Copy
3 2
0 0
0 0
0 0
Output
Copy
0
Note

The sequence of operations in the first example:

000000000110110000110110110110111111

题意:

给两个n*m的01矩阵A和B,给出了A中的元素,B中元素全为0,现在有一个操作可以在B中选一个坐标,它和右边下面右下的元素都变为1,问能否通过一些操作(不要求最少)使得B等于A,若能则输出步数和坐标,否则输出-1

思路:

暴力并判断边界(当时忘记判断边界结果被Hack了 ~TAT~ )

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=55;
 4 int a[amn][amn],idx[amn][amn],ansx[2600],ansy[2600];
 5 int main(){
 6     int n,m,tp=0,valid=1;
 7     cin>>n>>m;
 8     for(int i=1;i<=n;i++){
 9         for(int j=1;j<=m;j++){
10             cin>>a[i][j];
11             idx[i][j]=0;
12         }
13     }
14     for(int i=1;i<=n;i++){
15         for(int j=1;j<=m;j++){
16             if(a[i][j]){
17                 if(!idx[i][j]&&(i+1>n||j+1>m)){ ///这里判断边界条件被hack了...当时想着i<n&&j<m,判非法时忘记判边界了
18                     valid=0;
19                     break;
20                 }
21                 if(a[i][j+1]&&a[i+1][j]&&a[i+1][j+1]){
22                     idx[i][j+1]=idx[i+1][j]=idx[i+1][j+1]=1;
23                     ansx[++tp]=i;ansy[tp]=j;
24                 }
25                 else if(!idx[i][j]&&(!idx[i][j+1]||!idx[i+1][j]||!idx[i+1][j+1])){
26                     valid=0;
27                     break;
28                 }
29             }
30         }
31         if(valid==0)break;
32     }
33     if(valid){
34         cout<<tp<<endl;
35         for(int i=1;i<=tp;i++)cout<<ansx[i]<<' '<<ansy[i]<<endl;
36     }
37     else cout<<-1<<endl;
38 }
39 /**
40 给两个n*m的01矩阵A和B,给出了A中的元素,B中元素全为0,现在有一个操作可以在B中选一个坐标,它和右边下面右下的元素都变为1,问能否通过一些操作(不要求最少)使得B等于A,若能则输出步数和坐标,否则输出-1
41 暴力并判断边界
42 **/

 

posted @ 2019-08-23 21:39  Railgun000  阅读(193)  评论(0编辑  收藏  举报