Codeforces 1006 F - Xor-Paths

F - Xor-Path

思路:

双向搜索dfs

如果普通的搜索复杂度是n

那么双向搜索复杂度是√n

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 25;
LL a[N][N], k, ans = 0;
int n, m;
map<LL, LL>mp[N][N];
void dfs_pre(int x, int y, LL sum) {
    if(x + y == (n+m+2)/2) {
        mp[x][y][sum]++;
        return ;
    }
    if(x < n) dfs_pre(x+1, y, sum^a[x+1][y]);
    if(y < m) dfs_pre(x, y+1, sum^a[x][y+1]);
}
void dfs_suf(int x, int y, LL sum) {
    if(x + y == (n+m+2)/2) {
        ans += mp[x][y][sum^a[x][y]^k];
        return ;
    }
    if(x > 1) dfs_suf(x-1, y, sum^a[x-1][y]);
    if(y > 1) dfs_suf(x, y-1, sum^a[x][y-1]);
}
int main() {
    scanf("%d %d %lld", &n, &m, &k);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++)
            scanf("%lld", &a[i][j]);
    }
    dfs_pre(1, 1, a[1][1]);
    dfs_suf(n, m, a[n][m]);
    printf("%lld\n", ans);
    return 0;
}

 

posted @ 2018-07-17 09:46  Wisdom+.+  阅读(248)  评论(0编辑  收藏  举报