当前位置: 代码迷 >> 综合 >> Codeforces Round #498 (Div. 3)F - Xor-Paths(dfs+中间相遇算法)
  详细解决方案

Codeforces Round #498 (Div. 3)F - Xor-Paths(dfs+中间相遇算法)

热度:78   发布时间:2023-11-06 17:32:50.0

直接dfs会超时,优化的办法是将区间分割成两个部分,用map记录到达每个分割点的路径数。我在递归的时候不小心在DFS中写了dfs,ans一直是0。这个故事告诉我们:取两个功能一样的函数的名字的时候,不要用大小写区分,很容易写错。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ff(n,a) for(int i = 0 ; i < n; i++) scanf("%d",a+i)
#define tp(x) printf("x = %d\n",x)
#define ansp(x) printf("%d\n",x)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
const ll inf = 0x3f3f3f3f;const int mx = 25;
int n,m;
int vis2[mx][mx],vis1[mx][mx];ll ma[mx][mx],k,ans=0;
map<ll,ll>p[mx][mx];
void dfs(int x,int y, ll sum){if(x+y == (n+m+2)/2){p[x][y][sum]++;return;}if(x+1 <= n&&!vis1[x+1][y]){vis1[x+1][y] = 1;dfs(x+1,y,sum^ma[x+1][y]);vis1[x+1][y] = 0;}if(y+1 <= m&&!vis1[x][y+1]){vis1[x][y+1] = 1;dfs(x,y+1,sum^ma[x][y+1]);vis1[x][y+1] = 0;}
}
void DFS(int x, int y, ll sum){if(vis1[x][y]){ans += p[x][y][sum^ma[x][y]^k];return;}if(x-1 >= 1&&!vis2[x-1][y]){vis2[x-1][y] = 1;DFS(x-1,y,sum^ma[x-1][y]);vis2[x-1][y] = 0;}if(y-1 >= 1&&!vis2[x][y-1]){vis2[x][y-1] = 1;DFS(x,y-1,sum^ma[x][y-1]);vis2[x][y-1] = 0;}}int main(){//int T=10;	scanf("%d",&T);cin>>n>>m>>k;for(int i = 1; i <= n; i++)for(int j = 1; j <= m;j++)cin>>ma[i][j];dfs(1,1,ma[1][1]);for(int i = 1; i <= n; i++)for(int j = 1; j <= m;j++){if(i+j == (n+m+2)/2)vis1[i][j] = 1;			}DFS(n,m,ma[n][m]);cout<<ans<<endl;return 0;
}

 

  相关解决方案