zoukankan      html  css  js  c++  java
  • Foj 2296 Alice and Bob(博弈、搜索)

    Foj 2296 Alice and Bob

    题意

    两个人博弈,规则如下:轮流取09中的数字,最后Alice所得的数字个数为1n中,数位在Alice所取集合中出现奇数次的。
    双方想获得尽量多,问Alice能获得几个。

    题解

    观察一下,如果n是十的倍数,最后肯定是一人一半,这样一来,状态数应该会减少很多,我们就可以直接去搜。
    半个月前我补这道题,一直觉得是个dp问题,现在想想,其实就是暴力去做极大极小过程,只是需要把状态数表示出来。
    因为有了一开始的观察,所以可以把数字分成四类,再记一下前几位数一共出现了奇数次还是偶数次,如果奇数次最后答案是多少,偶数次是多少,这题就做完了。其实不难嘛!

    扩展

    game dfs 写法需考虑的状态表示问题需要多练习。
    dp 需练习。

    代码

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    #define rep(i, a, b) for(int i=(a); i<(b); i++)
    #define sz(a) (int)a.size()
    #define de(a) cout << #a << " = " << a << endl
    #define dd(a) cout << #a << " = " << a << " "
    #define all(a) a.begin(), a.end()
    #define endl "
    "
    typedef long long ll;
    typedef pair<int, int> pii;
    //---
    
    int n;
    int dp[11][11][11], cnt[10];
    
    int dfs(int a, int b, int c, int d, int pre, int cnt1, int cnt2, int ty) {
    	if(a+b+c+d == 0) return pre==0 ? cnt1 : cnt2;
    	if(ty == 0) {
    		int ans = 0;
    		if(a) ans = max(ans, dfs(a-1, b, c, d, pre, cnt1+1, cnt2, 1));
    		if(b) ans = max(ans, dfs(a, b-1, c, d, pre^1, cnt1+1, cnt2, 1));
    		if(c) ans = max(ans, dfs(a, b, c-1, d, pre, cnt1, cnt2, 1));
    		if(d) ans = max(ans, dfs(a, b, c, d-1, pre^1, cnt1, cnt2, 1));
    		return ans;
    	} else {
    		int ans = 10;
    		if(a) ans = min(ans, dfs(a-1, b, c, d, pre, cnt1, cnt2+1, 0));
    		if(b) ans = min(ans, dfs(a, b-1, c, d, pre, cnt1, cnt2+1, 0));
    		if(c) ans = min(ans, dfs(a, b, c-1, d, pre, cnt1, cnt2, 0));
    		if(d) ans = min(ans, dfs(a, b, c, d-1, pre, cnt1, cnt2, 0));
    		return ans;
    	}
    }
    
    void init() {
    	rep(i, 0, 11) rep(j, 0, 11-i) rep(k, 0, 11-i-j) dp[i][j][k] = dfs(i, j, k, 10-i-j-k, 0, 0, 0, 0);
    }
    
    int main() {
    	std::ios::sync_with_stdio(false);
    	std::cin.tie(0);
    	init();
    	int T;
    	cin >> T;
    	while(T--) {
    		///
    		cin >> n;
    		///init
    		memset(cnt, 0, sizeof(cnt));
    		///solve
    		int k = n%10;
    		int ans = n = n/10;
    		while(n) {
    			++cnt[n%10];
    			n/=10;
    		}
    		int a = 0, b = 0, c = 0;
    		rep(i, 0, k) {
    			if(cnt[i]%2==0) ++a;
    			else ++b;
    		}
    		rep(i, k, 10) c += cnt[i]%2==0;
    		cout << ans*5 + dp[a][b][c] << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    gradle3.0新命令
    Android Studio项目生成Jar包
    《图解HTTP》读书笔记
    Java实现多继承
    Unity3d和Android之间互相调用
    《Gradle权威指南》--Android Gradle NDK支持
    《Gradle权威指南》--Android Gradle测试
    《Gradle权威指南》--Android Gradle多渠道构建
    adb命令大全
    SparseArray源码解析
  • 原文地址:https://www.cnblogs.com/wuyuanyuan/p/9220740.html
Copyright © 2011-2022 走看看