Codeforces Round #498 (Div. 3) F. Xor-Paths
题目链接:F. Xor-Paths
题解:从起点和终点双向搜索在中间相遇时更新答案
1 #include<bits/stdc++.h> 2 #include<set> 3 #include<cstdio> 4 #include<iomanip> 5 #include<iostream> 6 #include<string> 7 #include<cstring> 8 #include<algorithm> 9 #define pb push_back 10 #define ll long long 11 #define fi first 12 #define se second 13 #define PI 3.14159265 14 #define ls l,m,rt<<1 15 #define rs m+1,r,rt<<1|1 16 #define eps 1e-7 17 #define pii pair<int,int> 18 typedef unsigned long long ull; 19 const int mod=998244353; 20 const ll inf=0x3f3f3f3f3f3f3f; 21 const int maxn=25; 22 using namespace std; 23 ll n,m,cn,ans,k; 24 ll a[25][25]; 25 map<ll,ll>mp[maxn][maxn]; 26 void dfs1(int x,int y,ll s) 27 { 28 //cout<<x<<" "<<y<<" "<<s<<endl; 29 if(x+y-2==(n+m-2)/2) 30 { 31 //cout<<x<<" "<<y<<" "<<s<<endl; 32 mp[x][y][s]++;return ; 33 } 34 if(x+1<=n)dfs1(x+1,y,s^a[x+1][y]); 35 if(y+1<=m)dfs1(x,y+1,s^a[x][y+1]); 36 } 37 void dfs2(int x,int y,ll s) 38 { 39 if(mp[x][y].size()>0) 40 { 41 //cout<<x<<" "<<y<<" "<<(s^a[x][y])<<" "<<(s^a[x][y]^k)<<endl; 42 ans+=mp[x][y][s^k^a[x][y]]; 43 return ; 44 } 45 if(x-1>=1)dfs2(x-1,y,s^a[x-1][y]); 46 if(y-1>=1)dfs2(x,y-1,s^a[x][y-1]); 47 } 48 int main() 49 { 50 ios::sync_with_stdio(false); 51 cin.tie(0);cout.tie(0); 52 cin>>n>>m>>k; 53 for(int i=1;i<=n;i++) 54 { 55 for(int j=1;j<=m;j++) 56 cin>>a[i][j]; 57 } 58 dfs1(1,1,a[1][1]); 59 dfs2(n,m,a[n][m]); 60 cout<<ans<<endl; 61 return 0; 62 }