Codeforces Round #777 C. Madoka and Childish Pranks

C. Madoka and Childish Pranks

time limit per test

1 second

memory limit per test

256 megabytes

Madoka as a child was an extremely capricious girl, and one of her favorite pranks was drawing on her wall. According to Madoka’s memories, the wall was a table of n rows and m columns, consisting only of zeroes and ones. The coordinate of the cell in the$ i-th$ row and the j-th column $(1 \le i \le n, 1 \le j \le m) $is (i, j).

One day she saw a picture “Mahou Shoujo Madoka Magica” and decided to draw it on her wall. Initially, the Madoka’s table is a table of size n \times m filled with zeroes. Then she applies the following operation any number of times:

Madoka selects any rectangular subtable of the table and paints it in a chess coloring (the upper left corner of the subtable always has the color 0). Note that some cells may be colored several times. In this case, the final color of the cell is equal to the color obtained during the last repainting.

imgWhite color means 0, black means 1. So, for example, the table in the first picture is painted in a chess coloring, and the others are not.

For better understanding of the statement, we recommend you to read the explanation of the first test.

Help Madoka and find some sequence of no more than n ⋅ m n \cdot m nm operations that allows you to obtain the picture she wants, or determine that this is impossible.

Input

Each test contains multiple test cases. The first line contains a single integer t ( 1 ≤ t ≤ 10 1 \le t \le 10 1t10) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n and m ( 1 ≤ n , m ≤ 100 ) (1 \leq n, m \leq 100) (1n,m100) — the size of the table. Each of the following n lines contains a string of length m consisting only of 1 and 0 — description of the picture that Madoka wants to obtain.

Output

If it is impossible to obtain the given picture, print -1.

Otherwise, print in the first line a single integer q q q ( 0 ≤ q ≤ n ⋅ m 0 \leq q \leq n \cdot m 0qnm) — the number of operations you need to obtain the picture. Note that you do not need to minimize the number of operations.

Then for each operation (in the order of execution) print a single line containing four numbers — the coordinates of the upper-left corner and the lower-right corner of the rectangle.

Example

input

Copy

4
4 5
01000
10100
01010
00110
2 3
001
010
3 3
110
101
000
1 1
0

output

Copy

4
1 1 3 3
3 3 4 4
4 3 4 4
4 2 4 3
1
1 2 2 3
-1
0

Note

The description of the first test case is below.

img

In the third test case, it is impossible to paint the desired picture.

In the fourth test case, the initial table is already the desired picture.

这题的意思有点难懂,大概题意是,原本有 n ∗ m n*m nm的网格,然后每次操作只能按棋盘图案操作,就是只能画一个类似棋盘的图案,比如样例1,第一步,画了一个 3 ∗ 3 3*3 33的正方形,按照棋盘的规则(黑白相间,第一个为白),第二步也是画了个棋盘不过是$2*2的。最后她给出画的样子,要你用这个方法画出来。

分析,分析这个需要从小着手,比如我们涂一个1*2的长方形,那么就只有三种情况:

1.在这里插入图片描述2.在这里插入图片描述3.在这里插入图片描述

显然第三种不能画,第一种可以画,第二种不用动。所以我们看到第一行第一个为黑就可以NO了。而且,很关键的是,他不需要你求最小步数,只要画出来就成功。所以我们试着每次只涂两个,从后面涂到前面,其实发现,只要第一个不是黑色,全部都能涂,只不过是用最多次数来涂,遍历所需数组,遇到1就涂(前一格和这一格(两格)),如果在第一列,就涂这一格和上面一格(第一行第一列除外)。

#include<iostream>
#include<cstring>
#include<vector>
#include<array>
using namespace std;
int a[101][101];
vector<array<int ,4> >ans;//二维答案数组
int main()
{
    int n;
    cin>>n;
    int x,y;
    while(n--){
        cin>>x>>y;
        char c;
        for(int i=1;i<=x;i++){
            for(int j = 1; j<= y; j++){
               cin>>c;
               a[i][j]=c-'0'; 
            }
        }
        ans.clear();
        if(a[1][1]==1){cout<<-1<<endl;
        continue;}
        for(int i=x; i >=1; i--){
            for(int j=y; j>=1 ;j--){
                if(a[i][j]==1){
                    if(j>1){
                        ans.push_back({i,j-1,i,j});
                        //cout<<i<<' '<<j-1<<' '<<i<<' '<<j<<endl;
                    }
                    else {
                        ans.push_back({i-1,j,i,j});
                        //cout<<i-1<<' '<<j<<' '<<i<<' '<<j<<endl;
                    }
                }
            }
        }
        cout<<ans.size()<<endl;//因为要先输入步数才输出坐标,所以要存
        for(auto i: ans){
            cout<< i[0]<<' '<< i[1] << ' '<< i[2]<< ' '<< i[3]<< endl;
        }
    }
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值