C. Madoka and Childish Pranks

42 篇文章 0 订阅

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 nn rows and mm columns, consisting only of zeroes and ones. The coordinate of the cell in the ii-th row and the jj-th column (1≤i≤n1≤i≤n, 1≤j≤m1≤j≤m) is (i,j)(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×mn×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 00). 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 00, black means 11. 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⋅mn⋅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 tt (1≤t≤101≤t≤10) — the number of test cases. Description of the test cases follows.

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

Output

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

Otherwise, print in the first line a single integer qq (0≤q≤n⋅m0≤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.

题意:让你形如拿棋盘格的01字符来将全是0的矩阵变为目标矩阵

思路:看棋盘格最小的单元:

因为可以覆盖,竖着的01可以将除了第一行下面的所有的0变成1

横着的01可以将除了第一列的所有的0变成1

所以只有当g[1][1]是1的时候才输出-1

然后我我们从第n排开始到第二排结束,每次从第一列开始遍历

当 g[i][j]==1的时候我们执行第一个操作,把i-1,j和i,j加入答案序列

然后遍历第一排,从第m列开始遍历

如果g[1][i]==1的话,进行第二个操作,把1,j-1和i,j加入答案序列

最后输出即可

/*

 .----------------.  .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. |
| |  ________    | || |  _________   | || | ____    ____ | || |     ____     | |
| | |_   ___ `.  | || | |_   ___  |  | || ||_   \  /   _|| || |   .'    `.   | |
| |   | |   `. \ | || |   | |_  \_|  | || |  |   \/   |  | || |  /  .--.  \  | |
| |   | |    | | | || |   |  _|  _   | || |  | |\  /| |  | || |  | |    | |  | |
| |  _| |___.' / | || |  _| |___/ |  | || | _| |_\/_| |_ | || |  \  `--'  /  | |
| | |________.'  | || | |_________|  | || ||_____||_____|| || |   `.____.'   | |
| |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'

*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<unordered_map>
#include<unordered_set>
#include<stack>
//#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b){
	return b>0 ? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
/*void init(){
	int con=0;
	for(int i=1;i<=15;i++)con+=i;
}
*/
/*
void sjdhfb(){
	int djbhff=0;
	for(int dd=1;dd<=4;dd++){
		djbhff+=dd;
	}
}
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
	e[idx]=b;
	w[idx]=c;
	ne[idx]=h[a];
	h[a]=idx++;
}
*/
const int N=100+10;
int n,m;
int g[N][N];
void sove(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		for(int j=0;j<m;j++){
			g[i][j+1]=s[j]-'0';
		}
	}
	if(g[1][1]){
		cout<<-1<<endl;
		return ;
	}
	vector<pii> ans;
	for(int i=n;i>=2;i--){
		for(int j=1;j<=m;j++){
			if(g[i][j]){
				ans.push_back({i-1,j});
				ans.push_back({i,j});  
			}
		}
	}
	for(int j=m;j>=2;j--){
		if(g[1][j]){
			ans.push_back({1,j-1});
			ans.push_back({1,j});  
		}
	}
	cout<<ans.size() /2<<endl;
	for(int i=0;i<ans.size() ;i+=2){
		cout<<ans[i].first<<" "<<ans[i].second<<" ";
		cout<<ans[i+1].first<<" "<<ans[i+1].second<<endl;
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	int t=1;
	
	cin>>t;
//	init();
//	sjdhfb();
//	init();
	while(t--){
		sove();
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值