Codeforces Round #684 (Div. 2) C1. Binary Table (Easy Version)(构造算法)

题目链接:https://codeforc.es/contest/1440/problem/C1

This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.

You are given a binary table of size n×m. This table consists of symbols 0 and 1.

You can make such operation: select 3 different cells that belong to one 2×2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).

Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don’t need to minimize the number of operations.

It can be proved that it is always possible.

Input
The first line contains a single integer t (1≤t≤5000) — the number of test cases. The next lines contain descriptions of test cases.

The first line of the description of each test case contains two integers n, m (2≤n,m≤100).

Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.

It is guaranteed that the sum of nm for all test cases does not exceed 20000.

Output
For each test case print the integer k (0≤k≤3nm) — the number of operations.

In the each of the next k lines print 6 integers x1,y1,x2,y2,x3,y3 (1≤x1,x2,x3≤n,1≤y1,y2,y3≤m) describing the next operation. This operation will be made with three cells (x1,y1), (x2,y2), (x3,y3). These three cells should be different. These three cells should belong into some 2×2 square.

Example

input

5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101

output

1
1 1 2 1 2 2
2 
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2 
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2 
1 4 1 5 2 5 
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2

题意

给出一个 n * m 矩阵,每次可以选择一个 2 * 2 的子矩阵,选择子矩阵中的三个元素将其取反,输出将 n * m 全部都变为 0 的步骤(不能超过 3 * n * m 次)。

分析

对于一个 2 * 2 的子矩阵,我们可以用如下方法使得最后只改变其中一个点,其他位置的点同理,最多用 3 * n * m 步。
在这里插入图片描述

代码
#include<bits/stdc++.h>
using namespace std;

int t;
int n,m;
int ans[30007][6],cnt;
char g[107][107];

void add(int x1,int y1,int x2,int y2,int x3,int y3)
{
	ans[++cnt][0] = x1;
	ans[cnt][1] = y1;
	ans[cnt][2] = x2;
	ans[cnt][3] = y2;
	ans[cnt][4] = x3;
	ans[cnt][5] = y3;
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		cnt = 0;
		scanf("%d%d",&n,&m);getchar();
		for(int i=1;i<=n;i++) scanf("%s",g[i] + 1);
		for(int i=1;i<n;i++)
			for(int j=1;j<m;j++)
				if(g[i][j] == '1')
				{
					add(i, j, i, j + 1, i + 1, j + 1);
					add(i, j, i, j + 1, i + 1, j);
					add(i, j, i + 1, j, i + 1, j + 1);
				}
		for(int j=1;j<m;j++)
			if(g[n][j] == '1')
			{
				int i = n;
				add(i, j, i - 1, j, i, j + 1);
				add(i, j, i, j + 1, i - 1, j + 1);
				add(i, j, i - 1, j, i - 1, j + 1);
			}
		for(int i=1;i<n;i++)
			if(g[i][m] == '1')
			{
				int j = m;
				add(i, j, i, j - 1, i + 1, j);
				add(i, j, i, j - 1, i + 1, j - 1);
				add(i, j, i + 1, j, i + 1, j - 1);
			}
		if(g[n][m] == '1')
		{
			int i = n, j = m;
			add(i, j, i, j - 1, i - 1, j - 1);
			add(i, j, i, j - 1, i - 1, j);
			add(i, j, i - 1, j, i - 1, j - 1);
		}
		printf("%d\n",cnt);
		for(int i=1;i<=cnt;i++)
		{
			for(int j=0;j<6;j++)
				printf("%d ",ans[i][j]);
			printf("\n");
		}
	}
	return 0;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值