题目链接
题意:
每个格子都有权值, 从(1,1)走到(n,m),每个格子不能重复访问,问最大得分。
解:
不能用2进制,用最小表示法加以判断防止出现多个回路。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 9 ;
const int mod=19993;
const int N=300000;
int cur,n,m,a[maxn+3][maxn+3];
struct pii
{
ll first;int second;
};
void update(int &x ,int val)
{
if(val>x) x=val;
}
struct Hashmap
{
pii e[N];
int fir[mod+3],nex[N],nedge;
void init()
{
nedge=0;
mem(fir,-1);
}
void insert(ll state,int val)
{
int p=state%mod;
for(int i=fir[p];~i;i=nex[i])
{
if(state==e[i].first)
{
update(e[i].second,val);
return;
}
}
e[nedge].first=state,e[nedge].second=val;
nex[nedge]=fir[p];
fir[p]=nedge++;
}
}hashmap[2];
struct Code
{
int tot,bit[10],ch[11];
ll s;
void decode(int x)
{
tot=0;
for(int i=0;i<=m;i++)
{
bit[i]=x&7;
tot=max(tot,bit[i]);
x>>=3;
}
tot++;
}
ll normalize()
{
mem(ch,-1);ch[0]=0;tot=1;
s=0;
for(int i=m;i>=0;i--)
{
if(ch[bit[i]]==-1) ch[bit[i]]=tot++;
bit[i]=ch[bit[i]];
s=(s<<3)|bit[i];
}
return s;
}
void shift()
{
for(int i=m;i>0;i--) bit[i]=bit[i-1];
bit[0]=0;
}
void color(int a,int b)//a to b
{
for0(i,m+1) if(bit[i]==a)
{
bit[i]=b;
}
}
}code;
void dpSE(int x,int y,ll state,int val)
{
const int p=code.bit[y-1],q=code.bit[y];
if(!p&&!q)
{
if(y+1<=m)
{
code.bit[y-1]=0,code.bit[y]=code.tot;
hashmap[cur].insert(code.normalize(),val+a[x][y]);
}
if(x+1<=n)
{
code.bit[y-1]=code.tot,code.bit[y]=0;
if(y==m) code.shift();
hashmap[cur].insert(code.normalize(),val+a[x][y]);
}
return;
}
if(p&&q) return;
code.bit[y-1]=code.bit[y]=0;
if(y==m) code.shift();
hashmap[cur].insert(code.normalize(),val+a[x][y]);
}
void dpBlank(int x,int y,ll state,int val)
{
const int p=code.bit[y-1],q=code.bit[y];
if(p&&q)
{
if(p==q) return;
code.color(p,q);
code.bit[y-1]=code.bit[y]=0;
if(y==m) code.shift();
hashmap[cur].insert(code.normalize(),val+a[x][y]);
return;
}
if(!p&&!q)
{
if(y+1<=m&&x+1<=n)
{
code.bit[y-1]=code.bit[y]=code.tot++;
hashmap[cur].insert(code.normalize(),val+a[x][y]);
}
code.bit[y-1]=code.bit[y]=0;
if(y==m) code.shift();
hashmap[cur].insert(code.normalize(),val);
return;
}
int t=p+q;
if(y+1<=m)
{
code.bit[y-1]=0,code.bit[y]=t;
hashmap[cur].insert(code.normalize(),val+a[x][y]);
}
if(x+1<=n)
{
code.bit[y-1]=t,code.bit[y]=0;
if(y==m) code.shift();
hashmap[cur].insert(code.normalize(),val+a[x][y]);
}
}
void solve()
{
cur=0;
hashmap[cur].init();
hashmap[cur].insert(0,0);
for1(i,n) for1(j,m)
{
cur^=1;
hashmap[cur].init();
for0(k,mod) for(int l=hashmap[1^cur].fir[k];~l;l=hashmap[1^cur].nex[l])
{
pii now=hashmap[cur^1].e[l];
ll state=now.first;int val=now.second;
code.decode(state);
// printf("i=%d,j=%d,state=%lld,vall=%d\n",i,j,state,val);
if(i==1&&j==1||i==n&&j==m) dpSE(i,j,state,val);
else dpBlank(i,j,state,val);
}
}
int ans=-INF;
for0(k,mod) for(int l=hashmap[cur].fir[k];~l;l=hashmap[cur].nex[l])
{
ans=max(ans,hashmap[cur].e[l].second);
}
cout<<ans<<endl;
}
int main()
{
std::ios::sync_with_stdio(false);
int kase=0;
while(cin>>n>>m)
{
for1(i,n) for1(j,m) cin>>a[i][j];
cout<<"Case "<<++kase<<": ";
if(n==1&&m==1) {cout<<a[1][1]<<endl;continue;}
solve();
}
return 0;
}