There is a rectangular grid of size n×mn×m . Each cell has a number written on it; the number on the cell (i,ji,j ) is ai,jai,j . Your task is to calculate the number of paths from the upper-left cell (1,11,1 ) to the bottom-right cell (n,mn,m ) meeting the following constraints:
- You can move to the right or to the bottom only. Formally, from the cell (i,ji,j ) you may move to the cell (i,j+1i,j+1 ) or to the cell (i+1,ji+1,j ). The target cell can't be outside of the grid.
- The xor of all the numbers on the path from the cell (1,11,1 ) to the cell (n,mn,m ) must be equal to kk (xor operation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and "xor" in Pascal).
Find the number of such paths in the given grid.
Input
The first line of the input contains three integers nn , mm and kk (1≤n,m≤201≤n,m≤20 , 0≤k≤10180≤k≤1018 ) — the height and the width of the grid, and the number kk .
The next nn lines contain mm integers each, the jj -th element in the ii -th line is ai,jai,j (0≤ai,j≤10180≤ai,j≤1018 ).
Output
Print one integer — the number of paths from (1,11,1 ) to (n,mn,m ) with xor sum equal to kk .
Examples
Input
3 3 11 2 1 5 7 10 0 12 6 4
Output
3
Input
3 4 2 1 3 3 3 0 3 3 2 3 0 1 1
Output
5
Input
3 4 1000000000000000000 1 3 3 3 0 3 3 2 3 0 1 1
Output
0
Note
All the paths from the first example:
- (1,1)→(2,1)→(3,1)→(3,2)→(3,3)(1,1)→(2,1)→(3,1)→(3,2)→(3,3) ;
- (1,1)→(2,1)→(2,2)→(2,3)→(3,3)(1,1)→(2,1)→(2,2)→(2,3)→(3,3) ;
- (1,1)→(1,2)→(2,2)→(3,2)→(3,3)(1,1)→(1,2)→(2,2)→(3,2)→(3,3) .
All the paths from the second example:
- (1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4) ;
- (1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4) ;
- (1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4)(1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4) ;
- (1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4) ;
- (1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4) .
思路:双向广搜
一边从左上角往右下角扩展,一边从右下角往左上角扩展。
利用map保存结点。
从左上角往右下角扩展之后,记得将与下次bfs重复的地方置0(否则会改变异或的结果)
存入数组最好从1开始(避免出现n=1,m=1的情况)
#include<cstdio>
#include<map>
#include<queue>
using namespace std;
static const int MAX = 25;
long long n, m;
long long k;
long long a[MAX][MAX];
int dir1_x[2] = {1, 0};
int dir1_y[2] = {0, 1};
int dir2_x[2] = {-1, 0};
int dir2_y[2] = {0, -1};
long long res;
struct point{
int x, y;
long long state;
point(int x1, int y1) : x(x1), y(y1) {}
point(int x1, int y1, long long s) : x(x1), y(y1), state(s){}
bool operator < (const point& p) const
{
if (x != p.x)
return x < p.x;
else if (y != p.y)
return y < p.y;
else
return state < p.state;
}
int sum(){return x + y;}
};
map<point, long long> mp;
//从左上角到右下角的bfs
void bfs1(point s)
{
int bound = (n + m) / 2 + 1;
s.state = a[s.x][s.y];
queue<point> que;
que.push(s);
while (!que.empty())
{
point tmp = que.front();
que.pop();
if (tmp.sum() == bound)
{
mp[tmp]++;
continue;
}
for (int i = 0; i < 2; i++)
{
int tx = tmp.x + dir1_x[i];
int ty = tmp.y + dir1_y[i];
if (tx > n || ty > m)
continue;
long long state = tmp.state ^ a[tx][ty];
que.push(point(tx, ty, state));
}
}
}
//从右下角到左上角的bfs
void bfs2(point s)
{
int bound = (n + m) / 2 + 1;
s.state = k ^ a[s.x][s.y];
queue<point> que;
que.push(s);
while (!que.empty())
{
point tmp = que.front();
que.pop();
if (tmp.sum() == bound)
{
res += mp[tmp];
continue;
}
for (int i = 0; i < 2; i++)
{
int tx = tmp.x + dir2_x[i];
int ty = tmp.y + dir2_y[i];
if (tx < 1 || ty < 1)
continue;
long long state = tmp.state ^ a[tx][ty];
que.push(point(tx, ty, state));
}
}
}
int main()
{
while (scanf("%lld %lld %lld", &n, &m, &k) != EOF)
{
res = 0;
mp.clear();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%lld", &a[i][j]);
bfs1(point(1, 1));
int bound = (n + m) / 2 + 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (i + j == bound)
a[i][j] = 0;
bfs2(point(n, m));
printf("%lld\n", res);
}
return 0;
}