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;
}