CF Round 777--C. Madoka and Childish Pranks

这篇文章描述了一个编程问题,给定一个n*m的黑白方格图,目标是通过不超过n*m次操作将全0的方格图转换成目标图案。每次操作可以选择一个矩形并以左上角为0的棋盘样式填充。如果第一格无法变为1,则情况无解。提供的解决方案包括从最后一列开始填充,以及处理第一列的特殊情况。
摘要由CSDN通过智能技术生成

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≤i≤n, 1≤j≤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×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.

White 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 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) — 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) — 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 (0≤q≤n⋅m) — 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.

input

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

output

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

The description of the first test case is below.

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黑白方格图,1表示黑,0表示白,你初始有一个全0的方格图,你可以选择一个矩形进行填充,填充效果是左上角是0,然后依次0101黑白相间,问最多操作n*m次,输出一个可行方案或者无解输出-1。

解析:因为每次每次操作左上角都是0,因此无论怎么选择,都无法使得(1,1)变成1,因此无解情况就是当且仅当a[1][1]是1,对于其他的,我们可以每次选择1*2的矩形,从最后一列开始填充,如果a[i][j]是1,那么我们可以选择(i,j-1,i,j)矩形,因为我们是从最后一列开始,因此不用考虑覆盖的问题,除去第一列,我们都已经填充完毕,最后第一列其实同理,选择2*1的矩形,从(n,1)->(2,1),如此就完成填充。

#include <bits/stdc++.h>
using namespace std;
const int N=105;
char a[N][N];
struct node
{
    int x1,y1,x2,y2;
};
void solve()
{
    int n,m;
    vector<node> ans;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) scanf(" %c",&a[i][j]);
    if(a[1][1]=='1')//无解情况
    {
        printf("-1\n");
        return;
    }
    for(int i=1;i<=n;i++)//除了第一列
    {
        for(int j=m;j>=2;j--)
        {
            if(a[i][j]=='1') ans.push_back({i,j-1,i,j});
        }
    }
    for(int i=n;i>=2;i--)//第一列情况
    {
        if(a[i][1]=='1') ans.push_back({i-1,1,i,1});
    }
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
    {
        int x1=ans[i].x1;
        int y1=ans[i].y1;
        int x2=ans[i].x2;
        int y2=ans[i].y2;
        printf("%d %d %d %d\n",x1,y1,x2,y2);
    }
}
int main()
{
    int t=1;
    scanf("%d",&t);
    while(t--) solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值