Educational Codeforces Round 71 (Rated for Div. 2) B - Square Filling

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 71 (Rated for Div. 2) B - Square Filling相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/https' title='https'>https   nta   ati   space   sync   www   air   check   during   

鍘熸枃閾炬帴锛?a href="https://www.cnblogs.com/xwl3109377858/p/11404261.html">https://www.cnblogs.com/xwl3109377858/p/11404261.html

Educational Codeforces Round 71 (Rated for Div. 2)

B - Square Filling

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 1≤x<n and 1≤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 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鈥榯 have to minimize the number of operations.

Input

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

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 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

Note

The sequence of operations in the first example:

000       110        111        111

000  →  110  →    111  →  111

000       000        000       011

 

棰樻剰锛氶鐩剰鎬濇槸缁欎綘涓€涓敱0锛?缁勬垚鐨勭煩闃靛拰涓€涓┖鐧界煩闃碉紝

浣犲彲浠ュ绌虹櫧鐭╅樀杩涜鎿嶄綔锛岄€夋嫨涓€涓偣锛屼负宸︿笂鐐癸紝浣垮叾2*2鐭╅樀琚?瑕嗙洊锛?/span>

闂綘鑳戒笉鑳芥妸绌虹櫧鐭╅樀鍙樻垚鎵€缁欑煩闃点€?/span>

鎬濊矾锛氬厛鏍规嵁鍘熺煩闃典腑鐨?锛屾爣璁扮煩闃典腑涓嶈兘鏀瑰彉鐨勫嚑涓偣锛堜负2*2鐭╅樀锛岃0鐐逛负鍙充笅鐐癸級锛?/span>

鏈€鍚庣敤1鐭╅樀鎶婃病鏈夋爣璁扮殑鐐硅鐩栵紝鐪嬩袱涓煩闃垫槸鍚︿竴鏍枫€?/span>

 

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<map>
  7 #include<set>
  8 #include<vector>
  9 #include<queue>
 10 #include<stack>
 11 #include<list>
 12 //#include<unordered_map>
 13 using namespace std;
 14 #define ll long long 
 15 const int mod=1e9+7;
 16 const long long int inf=1e18+7;
 17 
 18 const int maxn=105;
 19 
 20 int a[maxn][maxn];
 21 
 22 int b[maxn][maxn];
 23 
 24 int book[maxn][maxn];
 25 
 26 int n,m;
 27 
 28 inline bool check(int x,int y)
 29 
 30     if(x<1||x>n||y<1||y>m)
 31         return false;
 32     return true;
 33 
 34 
 35 int main()
 36 
 37     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 38     
 39     while(cin>>n>>m)
 40     
 41         memset(a,0,sizeof(a));
 42         memset(b,0,sizeof(b));
 43         memset(book,0,sizeof(book));
 44         
 45         for(int i=1;i<=n;i++)
 46         
 47             for(int j=1;j<=m;j++)
 48             
 49                 cin>>a[i][j];
 50             
 51         
 52         
 53         for(int i=1;i<=n;i++)
 54         
 55             for(int j=1;j<=m;j++)
 56             
 57                 if(a[i][j]==0)
 58                 
 59                     book[i][j]=1;//杩欎釜鐐逛笉鑳藉姩
 60                     
 61                     if(check(i-1,j-1))
 62                         book[i-1][j-1]=1;
 63                     if(check(i-1,j))
 64                         book[i-1][j]=1;
 65                     if(check(i,j-1))
 66                         book[i][j-1]=1;    
 67                 
 68             
 69         
 70         vector<pair<int,int> >v;
 71         
 72         for(int i=1;i<n;i++)//鍙?*2鐭╅樀锛岃竟鐣屾棤娉曞彇 
 73         
 74             for(int j=1;j<m;j++)
 75             
 76                 if(book[i][j]==0)//鍙互鍔?/span>
 77                 
 78                     v.push_back(make_pair(i,j));
 79                     b[i][j]=1;
 80                     b[i+1][j]=1;
 81                     b[i][j+1]=1;
 82                     b[i+1][j+1]=1;
 83                   
 84             
 85         
 86         
 87         int flag=0;
 88         
 89         for(int i=1;i<=n;i++)
 90         
 91             for(int j=1;j<=m;j++)
 92             
 93                 if(a[i][j]!=b[i][j])
 94                 
 95                     flag=1;
 96                     break;
 97                 
 98             
 99         
100         
101         if(flag)
102             cout<<-1<<endl;
103         else
104         
105             cout<<v.size()<<endl;
106             for(int i=0;i<v.size();i++)
107                 cout<<v[i].first<<" "<<v[i].second<<endl;
108         
109         
110     
111     
112     return 0;
113 

 

以上是关于Educational Codeforces Round 71 (Rated for Div. 2) B - Square Filling的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27