topic link
bzoj5248: [2018 multi-provincial and provincial team joint test] a pair of wooden chess
answer
I didn't think of it at the scene... It's really a brain-damage that limits the imagination
. The placement of the pieces is increasing, and the number of states is not many, more than 300,000
. Against search, map and memorize the state. It's over
. I didn't dare to think about
it at the time... ahh, when I hashed it, I got stuck on the following base. . . . .
code
#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxn = 13;
#define LL long long
int n,m;
int a[maxn][maxn],b[maxn][maxn];
int sta[maxn];
int base = 13;
std::map<LL,int>mp;
inline LL hash() {
LL ret = 0;
for(int i = 1;i <= n;++ i) ret = ret * base + sta[i];
return ret;
}
void get_s(LL tmp) {
for(int i = n;i;i --) sta[i] = tmp % base,tmp /= base;
//puts("") ;
}
inline bool get_NS () {
int ret = 0;
for(int i = 1;i <= n;++ i) ret += sta[i];
return ret & 1;
}
inline int dfs(LL state) {
if(mp.find(state) != mp.end()) return mp[state];
get_s(state);
bool type = get_NS() ;
int ret = type ? 0x3f3f3f3f : -0x3f3f3f3f;
for(int i = 1;i <= n;++ i)
if(sta[i - 1] > sta[i]) {
++ sta[i];
LL Next = hash();
if(!type) ret = std::max(ret,dfs(Next) + a[i][sta[i]]);
else ret = std::min(ret,dfs(Next) - b[i][sta[i]]);
-- sta[i];
}
mp[state] = ret;
//printf("%d\n",ret);
return ret;
}
int main() {
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= m;++ j)
scanf("%d",&a[i][j]);
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= m;++ j)
scanf("%d",&b[i][j]);
sta[0] = m;
for(int i = 1;i <= n;++ i) sta[i] = m;
mp[hash()] = 0;
dfs(0);
//for(int i = 1;i <= 10000*n;++ i) {
//printf("%d\n",mp[i]);
//}
printf("%d\n",mp[0]);
return 0;
}