Binary Table (Easy Version)-(思维)

博客围绕大小为n×m的二进制表展开,表由0和1组成,可对2×2正方形中的3个不同单元格符号进行更改。介绍了两种使表中所有符号变为0的思路,思路1通过找2*2矩阵规律扫图操作,思路2发现3次操作可改变2*2矩阵中一个点状态,还提及需特判边界。
题目描述:点击进入
题意

给你一个大小为n×m的二进制表,这个表由符号 0 和符号 1 组成。
你可以进行这样的操作:
选择属于一个 2 × 2 正方形的 3 个不同的单元格,并更改这些单元格中的符号(将 0 更改为1, 1 更改为 0 )。
你的任务是使表中的所有符号都等于0。您最多可以进行 3nm次 操作。
你不需要最小化操作的数量。

思路1

通过找找规律,我们可以知道一个 2*2 的矩阵
不包含0的可以通过一次操作变为包含3个0的,
包含3个0的通过一次操作可以变为包含2个0的,
包含2个0的通过一次操作可以变为包含1个0的,
包含1个0的通过一次操作可以变为包含4个0的,
包含4个0的为符合条件的不需要操作。
在这里插入图片描述
根据这个规律我们就可以直接扫图然后操作、记录了
(当 n 或者 m 是奇数的时候需要特判一下边界)

代码
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define INF 0x3f3f3f3f
#define pii pair<int, int>
//#define pdi pair<double,int>
//#define int long long 
using namespace std;
typedef long long ll;
typedef unsigned long long llu; 
const int maxn=1e5+10;
const int mod=1000000007;
int t,n,k,m,tot;
vector<int>ax;
vector<int>ay;
char a[110][110];
int check(int x,int y)//查询 2 * 2 矩阵中 0 的个数
{
	int sum=0;
	for(int i=x;i<=x+1;i++)
	{
		for(int j=y;j<=y+1;j++)
		{
			if(a[i][j]=='0') 
				sum++; 
		}
	}
	return sum;
}
void check1(int x,int y)// 1 个 0
{
	for(int i=x;i<=x+1;i++)
	{
		for(int j=y;j<=y+1;j++)
		{
			if(a[i][j]=='1')
			{
				ax.push_back(i);
				ay.push_back(j);
				a[i][j]='0';
			}
		}
	}
}
void check2(int x,int y)// 2 个 0 
{
	int flag=1;
	for(int i=x;i<=x+1;i++)
	{
		for(int j=y;j<=y+1;j++)
		{
			if(a[i][j]=='0'||a[i][j]=='1'&&flag)
			{
				if(a[i][j]=='1') flag--;
				ax.push_back(i);
				ay.push_back(j);
				if(a[i][j]=='0') a[i][j]='1';
				else a[i][j]='0';
			}
		}
	}
	check1(x,y);
}
void check3(int x,int y)// 3 个 0 
{
	int flag=2;
	for(int i=x;i<=x+1;i++)
	{
		for(int j=y;j<=y+1;j++)
		{
			if(flag&&a[i][j]=='0'||a[i][j]=='1')
			{
				if(a[i][j]=='0') flag--;
				ax.push_back(i);
				ay.push_back(j);
				if(a[i][j]=='0') a[i][j]='1';
				else a[i][j]='0';
			}
		}
	}
	check2(x,y);
}
void check0(int x,int y)//没有 0 
{
	int flag=3;
	for(int i=x;i<=x+1;i++)
	{
		for(int j=y;j<=y+1;j++)
		{
			if(flag)
			{
				flag--;
				ax.push_back(i);
				ay.push_back(j);
				a[i][j]='0';
			}
		}
	}
	check3(x,y);
}
int main( )
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		ax.clear();
		ay.clear();
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) 
		    cin>>a[i][j];
		if(n&1)
		{
			for(int j=1;j<=m-1;j+=2)
			{
				int sum=check(n-1,j);
				if(sum==4) continue; 
				else if(sum==3)
			    	check3(n-1,j);
			    else if(sum==2)
			       check2(n-1,j);
		    	else if(sum==1)
		    		check1(n-1,j);
		    	else if(sum==0)
		    		check0(n-1,j);
			}
		}
		if(m&1)
		{
			for(int i=1;i<=n-1;i+=2)
			{
				int sum=check(i,m-1);
				if(sum==4) continue; 
				else if(sum==3)
			    	check3(i,m-1);
			    else if(sum==2)
			       check2(i,m-1);
		    	else if(sum==1)
		    		check1(i,m-1);
		    	else if(sum==0)
		    		check0(i,m-1);
			}
		}
		for(int i=1;i<=n-1;i+=2)
		{
			for(int j=1;j<=m-1;j+=2)
			{
				int sum=check(i,j);
				if(sum==4) continue; 
				else if(sum==3)
			    	check3(i,j);
			    else if(sum==2)
			    	check2(i,j);
		    	else if(sum==1)
		    		check1(i,j);
		    	else if(sum==0)
		    		check0(i,j);
			}
		}
		for(int i=1;i<=n-1;i++)//这里需要重复检查一次避免遗漏
		{
			for(int j=1;j<=m-1;j++)
			{
				int sum=check(i,j);
				if(sum==4) continue; 
				else if(sum==3)
			    	check3(i,j);
			    else if(sum==2)
			    	check2(i,j);
		    	else if(sum==1)
		    		check1(i,j);
		    	else if(sum==0)
		    		check0(i,j);
			}
		}
       int len=ax.size();
		cout<<len/3<<endl; 
		for(int i=0;i<len;i+=3)
		{
			cout<<ax[i]<<' '<<ay[i]<<' '<<ax[i+1]<<' '<<ay[i+1]<<' '<<ax[i+2]<<' '<<ay[i+2]<<' '<<endl;		
		}
	}
    return 0;
}
思路2

这里再介绍一下 ybl 大佬的思路(绝了),看题目可以知道最多 3nm 次操作,为什么是 3nm ,为什么有3,,, n * m 可以理解为每一个位置的元素,那 3 呢,,于是找找规律,,,惊奇的发现 3 次操作可以只改变 2 * 2 矩阵中一个点的状态,而其他点保持不变,例如:
在这里插入图片描述
这三次操作可以只改变左上角点的状态,那么,,这道题不就有了嘛
,剩下的就是边界特判了,Game over!

代码:
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define INF 0x3f3f3f3f
#define pii pair<int, int>
//#define pdi pair<double,int>
//#define int long long 
using namespace std;
typedef long long ll;
typedef unsigned long long llu; 
const int maxn=1e6+10;
const int mod=1000000007;
ll t,n,k,m,tot; 
struct node
{
	int x1,y1;
	int x2,y2;
	int x3,y3;
	node(){}
	node(int a,int b,int c,int d,int e,int f)
	{
		x1=a,y1=b;
    	x2=c,y2=d;
    	x3=e,y3=f;
	}
};
char a[110][110];
int main( )
{
//	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		vector<node>v;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		    scanf("%s",a[i]+1);
		for(int i=1;i<=n-1;i++)
		{
			for(int j=1;j<=m;j++) 
			{
				if(a[i][j]=='1')
				{
					if(j==1)
					{
						v.push_back(node(i,j,i,j+1,i+1,j));
						v.push_back(node(i,j,i,j+1,i+1,j+1));
						v.push_back(node(i,j,i+1,j,i+1,j+1));
					}
					else if(j==m)
					{
						v.push_back(node(i,j,i,j-1,i+1,j));
						v.push_back(node(i,j,i,j-1,i+1,j-1));
						v.push_back(node(i,j,i+1,j,i+1,j-1));
					}
					else
					{
						v.push_back(node(i,j,i,j+1,i+1,j));
						v.push_back(node(i,j,i,j+1,i+1,j+1));
						v.push_back(node(i,j,i+1,j,i+1,j+1));
					}
				}
			}
		}
		for(int i=1;i<=m;i++) 
		{
			if(a[n][i]=='1')
			{
				if(i<m)
				{
					v.push_back(node(n-1,i,n,i,n,i+1));
					v.push_back(node(n-1,i+1,n,i,n,i+1));
					v.push_back(node(n-1,i,n-1,i+1,n,i));
				}
				else if(i==m)
				{
					v.push_back(node(n-1,i,n,i,n,i-1));
					v.push_back(node(n-1,i-1,n,i,n,i-1));
					v.push_back(node(n-1,i-1,n-1,i,n,i));
				}
			}
		}
		cout<<v.size()<<endl;
		for(int i=0;i<v.size();i++)
		{
			cout<<v[i].x1<<' '<<v[i].y1<<' '<<v[i].x2<<' '<<v[i].y2<<' '<<v[i].x3<<' '<<v[i].y3<<endl;
		}
	}
    return 0;
}
The Network Simulator, Version 3 -------------------------------- Table of Contents: ------------------ 1) An overview 2) Building ns-3 3) Running ns-3 4) Getting access to the ns-3 documentation 5) Working with the development version of ns-3 Note: Much more substantial information about ns-3 can be found at http://www.nsnam.org 1) An Open Source project ------------------------- ns-3 is a free open source project aiming to build a discrete-event network simulator targeted for simulation research and education. This is a collaborative project; we hope that the missing pieces of the models we have not yet implemented will be contributed by the community in an open collaboration process. The process of contributing to the ns-3 project varies with the people involved, the amount of time they can invest and the type of model they want to work on, but the current process that the project tries to follow is described here: http://www.nsnam.org/developers/contributing-code/ This README excerpts some details from a more extensive tutorial that is maintained at: http://www.nsnam.org/documentation/latest/ 2) Building ns-3 ---------------- The code for the framework and the default models provided by ns-3 is built as a set of libraries. User simulations are expected to be written as simple programs that make use of these ns-3 libraries. To build the set of default libraries and the example programs included in this package, you need to use the tool 'waf'. Detailed information on how use waf is included in the file doc/build.txt However, the real quick and dirty way to get started is to type the command ./waf configure --enable-examples followed by ./waf in the the directory which contains this README file. The files built will be copied in the build/ directory. The current codebase is expected to build and run on the set of platforms listed in the RELEASE_NOTES file. Other platforms may or may not work: we welcome patches to improve the portability of the code to these other platforms. 3) Running ns-3 --------------- On recent Linux systems, once you have built ns-3 (with examples enabled), it should be easy to run the sample programs with the following command, such as: ./waf --run simple-global-routing That program should generate a simple-global-routing.tr text trace file and a set of simple-global-routing-xx-xx.pcap binary pcap trace files, which can be read by tcpdump -tt -r filename.pcap The program source can be found in the examples/routing directory. 4) Getting access to the ns-3 documentation ------------------------------------------- Once you have verified that your build of ns-3 works by running the simple-point-to-point example as outlined in 4) above, it is quite likely that you will want to get started on reading some ns-3 documentation. All of that documentation should always be available from the ns-3 website: http:://www.nsnam.org/documentation/. This documentation includes: - a tutorial - a reference manual - models in the ns-3 model library - a wiki for user-contributed tips: http://www.nsnam.org/wiki/ - API documentation generated using doxygen: this is a reference manual, most likely not very well suited as introductory text: http://www.nsnam.org/doxygen/index.html 5) Working with the development version of ns-3 ----------------------------------------------- If you want to download and use the development version of ns-3, you need to use the tool 'mercurial'. A quick and dirty cheat sheet is included in doc/mercurial.txt but reading through the mercurial tutorials included on the mercurial website is usually a good idea if you are not familiar with it. If you have successfully installed mercurial, you can get a copy of the development version with the following command: "hg clone http://code.nsnam.org/ns-3-dev"
最新发布
03-08
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值