Foj 2296 Alice and Bob
阿新 • • 發佈:2018-07-01
all ice pair define code IT int pac end
Foj 2296 Alice and Bob
題意
題解
代碼
擴展
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 "\n"
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;
}
Foj 2296 Alice and Bob