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:
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
3 3 11
2 1 5
7 10 0
12 6 4
3
3 4 2
1 3 3 3
0 3 3 2
3 0 1 1
5
3 4 1000000000000000000
1 3 3 3
0 3 3 2
3 0 1 1
0
Note
All the paths from the first example:
All the paths from the second example:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=25;
ll a[maxn][maxn],ans,k;
map<ll,ll>mp[maxn];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int n,m;
void dfs_pre(int x,int y,ll sum)
{
if(x+y==(n+m+2)/2) {mp[x][sum]++;return ;}//x+y==(n+m+2)/2,有个+2是因为有两种特殊情况,一个是n=1,一个是m=1
for(int i=0;i<2;i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(tx<1||ty<1||tx>n||ty>m) continue;
dfs_pre(tx,ty,sum^a[tx][ty]);
}
}
void dfs_end(int x,int y,ll sum)
{
if(x+y==(n+m+2)/2) {ans+=mp[x][sum^k^a[x][y]];return ;}
for(int i=2;i<4;i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(tx<1||ty<1||tx>n||ty>m) continue;
dfs_end(tx,ty,sum^a[tx][ty]);
}
}
int main()
{
scanf("%d%d%I64d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%I64d",&a[i][j]);
}
}
dfs_pre(1,1,a[1][1]);
dfs_end(n,m,a[n][m]);
printf("%I64d\n",ans);
return 0;
}
原文:https://www.cnblogs.com/EchoZQN/p/10351453.html