1. 程式人生 > >Codeforces Round #498 (Div. 3)F

Codeforces Round #498 (Div. 3)F

直接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;
}