思路:
双向搜索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; }