Codeforces 1006 F - Xor-Paths

Posted widsom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
", ans);
    return 0;
}

 

以上是关于Codeforces 1006 F - Xor-Paths的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1006E

Codeforces Round #498 (Div. 3)完结

Codeforces 1006F

Luogu 1006 传纸条

Codeforces Round #498 (Div. 3) 简要题解

HOJ 1006