不显然,能下棋的地方对于每一行是非严格递减的
可以用一个n+1进制数来表示这个轮廓线
状态数就是n个球放m+1个盒子里的方案数,不会太大
然后就是大佬们所说的对抗搜索
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
using namespace std;
#define mmst(a, b) memset(a, b, sizeof(a))
#define mmcp(a, b) memcpy(a, b, sizeof(b))
typedef long long LL;
const int N=22,base=12,oo=1e9+7;
int n,m;
int a[N][N],b[N][N];
int h[N];
map<LL,int> mp;
LL hash()
{
LL res=0;
for(int i=1;i<=n;i++)
res=res*base+h[i];
return res;
}
void rehash(LL x)
{
for(int i=n;i>=1;i--)
h[i]=x%base,x/=base;
}
int dfs(LL x)
{
if(mp.count(x))
return mp[x];
rehash(x);
int hy=0;
for(int i=1;i<=n;i++)
hy+=h[i];
hy%=2;
int res=(hy) ? oo : -oo;
for(int i=1;i<=n;i++)
if(h[i-1]>h[i])
{
h[i]++;
LL to=hash();
if(hy)
res=min(res,dfs(to)-b[i][h[i]]);
else
res=max(res,dfs(to)+a[i][h[i]]);
h[i]--;
}
mp[x]=res;
return res;
}
int main()
{
cin>>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]);
for(int i=0;i<=n;i++)
h[i]=m;
mp[hash()]=0;
cout<<dfs(0)<<endl;
return 0;
}