题目链接:http://codeforces.com/contest/1006/problem/F
题意是有一个n*m的地图,然后从左上角走到右下角,问最后异或的值等于k的路径有多少条。
思路就是折半搜索,可以降低很多时间复杂度,因为当地图小的时候,n+m就不满足折半的条件了,所以这里用n+m+2作为条件,首先我们先搜索步数为n+m+2的一半的值为搜索终点,记录当前的异或值,然后再从终点开始搜索,当搜索到n+m+2的一半的时候,我们判断当前点的与k的异或值是否有标记过,有的话就说明这整个一条路的异或值等于k,直接加给ans就好了。
人生第一道F题...
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define maxn 25
#define ll long long
using namespace std;
map<ll,ll> ma[maxn][maxn];
ll pre[maxn][maxn];
ll n,m,k;
ll ans;
void dfs1(ll x,ll y,ll cur){
if(x > n || y > m)return ;
cur ^= pre[x][y];
if(x + y == (n + m + 2) / 2){
ma[x][y][cur]++;
return ;
}
dfs1(x + 1, y, cur);
dfs1(x, y + 1, cur);
}
void dfs2(ll x,ll y,ll cur){
if(x < 1 || y < 1)return ;
if(x + y == (m + n + 2) / 2){
ans += ma[x][y][k ^ cur];
return ;
}
cur ^= pre[x][y];
dfs2(x - 1, y, cur);
dfs2(x, y - 1, cur);
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>pre[i][j];
}
}
ans = 0;
dfs1(1,1,0);
dfs2(n,m,0);
cout<<ans<<endl;
return 0;
}