CodeForces 1006F Xor-Paths

题目:http://codeforces.com/problemset/problem/1006/F

题意:从左上走到右下,每次只能向右或下走,问走到右下角时路径异或和为k的路径数

直接暴力dfs的话复杂度为240

所以我们用双向dfs 复杂度为2*220=221

用map记录路径异或和

从左上跑一半,右下跑一半

跑到相同点时将答案传回

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
int n,m;
ll k;
ll a[25][25];
map<ll,ll> dp[25];
void dfs(int x,int y,ll s)
{
    if (x>n||y>m) return;
    if ((x+y)==(n+m)/2+1) {dp[x][s^a[x][y]]++;return;}
    dfs(x+1,y,s^a[x][y]);dfs(x,y+1,s^a[x][y]);
}
ll solve(int x,int y,ll s)
{
    if (x<1||y<1) return 0;
    if ((x+y)==(n+m)/2+1) return dp[x][s];
    return solve(x-1,y,s^a[x][y])+solve(x,y-1,s^a[x][y]);
}
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(1,1,0);
    printf("%I64d
",solve(n,m,k));
    return 0;
}

  

原文地址:https://www.cnblogs.com/bk-201/p/9325315.html