题面
题解
因为每行取的数的个数是单调不增的,感觉状态数不会很多?
怒而记搜,结果过了...
#include<bits/stdc++.h>
#define For(i,x,y) for (int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (int i=(x);i>=(y);i--)
#define cross(i,k) for (int i=first[k];i;i=last[i])
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
ll x=0;int ch=getchar(),f=1;
while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
if (ch=='-'){f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int N = 15;
int n,m,a[N][N],b[N][N];
map<ull,int>Map;
int now[N];
const ull base = 233;
inline ull Get(){
ull ans=0;
For(i,1,n) ans=ans*base+now[i];
return ans;
}
inline int dfs(int x){
ull hsh=Get();
if (Map.count(hsh)) return Map[hsh];
int tmp=(!x)?-1e9:1e9;
For(i,1,n)
if (now[i]+1<=now[i-1]){
now[i]++;
if (x>0) tmp=min(tmp,dfs(x^1)-b[i][now[i]]);
else tmp=max(tmp,dfs(x^1)+a[i][now[i]]);
now[i]--;
}
return Map[hsh]=tmp;
}
int main(){
n=read(),m=read();
For(i,1,n)
For(j,1,m) a[i][j]=read();
For(i,1,n)
For(j,1,m) b[i][j]=read();
For(i,1,n) now[i]=m;
Map[Get()]=0,memset(now,0,sizeof now),now[0]=m;
printf("%d",dfs(0));
}