Note: The XOR-sum of set {s1,s2,…,sm}{s1,s2,…,sm} is defined as s1⊕s2⊕…⊕sms1⊕s2⊕…⊕sm, where ⊕⊕ denotes the bitwise XOR operation.
After almost winning IOI, Victor bought himself an n×nn×n grid containing integers in each cell. nn is an even integer. The integer in the cell in the ii-th row and jj-th column is ai,jai,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≤n1≤i,j,k,l≤n, the cell in the ii-th row and jj-th column is a neighbor of the cell in the kk-th row and ll-th column if |i−k|=1|i−k|=1 and j=lj=l, or if i=ki=k and |j−l|=1|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.
Input
The first line of the input contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single even integer nn (2≤n≤10002≤n≤1000) — the size of the grid.
Then follows nn lines, each containing nn integers. The jj-th integer in the ii-th of these lines represents the XOR-sum of the integers in all the neighbors of the cell in the ii-th row and jj-th column.
It is guaranteed that the sum of nn over all test cases doesn't exceed 10001000 and in the original grid 0≤ai,j≤230−10≤ai,j≤230−1.
Hack Format
To hack a solution, use the following format:
The first line should contain a single integer t (1≤t≤1001≤t≤100) — the number of test cases.
The first line of each test case should contain a single even integer nn (2≤n≤10002≤n≤1000) — the size of the grid.
Then nn lines should follow, each containing nn integers. The jj-th integer in the ii-th of these lines is ai,jai,j in Victor's original grid. The values in the grid should be integers in the range [0,230−1][0,230−1]
The sum of nn over all test cases must not exceed 10001000.
Output
For each test case, output a single integer — the XOR-sum of the whole grid.
Example
input
Copy
3 2 1 5 5 1 4 1 14 8 9 3 1 5 9 4 13 11 1 1 15 4 11 4 2 4 1 6 3 7 3 10 15 9 4 2 12 7 15 1
output
Copy
4 9 5
Note
For the first test case, one possibility for Victor's original grid is:
11 | 33 |
22 | 44 |
For the second test case, one possibility for Victor's original grid is:
33 | 88 | 88 | 55 |
99 | 55 | 55 | 11 |
55 | 55 | 99 | 99 |
88 | 44 | 22 | 99 |
For the third test case, one possibility for Victor's original grid is:
44 | 33 | 22 | 11 |
11 | 22 | 33 | 44 |
55 | 66 | 77 | 88 |
88 | 99 | 99 | 11 |
思路:题中给出我们每个数相邻的异或和,让我们求全部的异或和。我们是不是只要将数组的每一个值都异或一遍不就是答案了。题中给出相邻异或和,是不是已经替我们遍历过了周围的数了,那么将他给的相邻异或和记录就行了,我们只需要记录哪个数用过哪个数没用过,用过了就不用管了,已经将他的和记录了,(用过的是当前这个数周围的数),没用过将他的值加入答案即可标记周围的数用过了。但可能出现的情况是这个数被用了两次(因为是通过周围影响的),这个数异或两次相当于又去掉了,所以这个点应当是没用过的,之后再加上即可。
周围点标记:这个题像《费解的开关》一样,都是标记周围的数(只不过费解的开关是将当前点也标记,这个题不用)所以将这个题也从第二行开始遍历,看当前点上面的那个点是否用过,没有的话,记录当前点的(周围的点)即可,“加入”该异或和即可。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1010;
int a[N][N];
int use[N][N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(use,0,sizeof use);
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(use[i-1][j]==0)
{
ans^=a[i][j];
use[i-1][j]^=1;
use[i+1][j]^=1;
use[i][j-1]^=1;
use[i][j+1]^=1;
}
}
}
cout<<ans<<endl;
}
return 0;
}