cf1628 C. Grid Xor

Note: The XOR-sum of set {s1,s2,…,sm} is defined as s1⊕s2⊕…⊕sm, where ⊕ denotes the bitwise XOR operation.
After almost winning IOI, Victor bought himself an n×n grid containing integers in each cell. n is an even integer. The integer in the cell in the i-th row and j-th column is ai,j.
Sadly, Mihai stole the grid from Victor and told him he would return it with only one condition: Victor has to tell Mihai the XOR-sum of all the integers in the whole grid.
Victor doesn’t remember all the elements of the grid, but he remembers some information about it: For each cell, Victor remembers the XOR-sum of all its neighboring cells.
Two cells are considered neighbors if they share an edge — in other words, for some integers 1≤i,j,k,l≤n, the cell in the i-th row and j-th column is a neighbor of the cell in the k-th row and l-th column if |i−k|=1 and j=l, or if i=k and |j−l|=1.
To get his grid back, Victor is asking you for your help. Can you use the information Victor remembers to find the XOR-sum of the whole grid?
It can be proven that the answer is unique.

把取每个格子的贡献看做给它四面八方的格子染色(0,1染色,0染成1,1染成0),目标是把所有格子染成1。
从第二行开始,按顺序遍历。
对于每一个方格(i,j),如果它的上一个方格没有被染色,那么只能通过计算这个方格的贡献来对其染色。

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

#define maxn ((int)1e3)

int a[maxn+5][maxn+5],g[maxn+5][maxn+5];

int main() {
	
	int T;
	cin>>T;
	while(T--) {
		int n;
		cin>>n;
		for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>a[i][j];
		for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) g[i][j]=0;
		
		int ans=0;
		
		for(int i=2;i<=n;i++) {
			for(int j=1;j<=n;j++) {
				if(g[i-1][j]==0) {
					g[i-1][j]^=1,g[i+1][j]^=1,g[i][j-1]^=1,g[i][j+1]^=1;
					ans=ans^a[i][j];
				}
			}
		}
		
		cout<<ans<<'\n';
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值