将格子中的兩個2和兩個3連起來,求經過總的格子數的總和減2.
Hint:兩條路徑不能交叉,有障礙格子。非障礙格子隻能經過一次。
SOL:
插頭記錄三種狀态: 0----沒有插頭
2----數字2
3----數字3
用四進制優于三進制。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXD=15;
const int HASH=10007;
const int STATE=1000010;
int N,M;
int maze[MAXD][MAXD];//0表示障礙,1是非障礙,2和3
int code[MAXD];
//0表示沒有插頭,2表示和插頭2相連,3表示和插頭3相連
struct HASHMAP
{
int head[HASH],next[STATE],size;
int state[STATE];
int dp[STATE];
void init()
{
size=0;
memset(head,-1,sizeof(head));
}
void push(int st,int ans)
{
int i,h=st%HASH;
for(i=head[h];i!=-1;i=next[i])
if(state[i]==st)
{
if(dp[i]>ans)dp[i]=ans;
return;
}
state[size]=st;
dp[size]=ans;
next[size]=head[h];
head[h]=size++;
}
}hm[2];
void decode(int *code,int m,int st)//四進制
{
int i;
for(int i=m;i>=0;i--)
{
code[i]=(st&3);
st>>=2;
}
}
int encode(int *code,int m)
{
int i;
int st=0;
for(int i=0;i<=m;i++)
{
st<<=2;
st|=code[i];
}
return st;
}
void shift(int *code,int m)//換行
{
for(int i=m;i>0;i--)code[i]=code[i-1];
code[0]=0;
}
void dpblank(int i,int j,int cur)
{
int k,left,up;
for(k=0;k<hm[cur].size;k++)
{
decode(code,M,hm[cur].state[k]);
left=code[j-1];
up=code[j];
if(left&&up)
{
if(left==up)//隻能是相同的插頭
{
code[j-1]=code[j]=0;
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
}
else if((left&&(!up))||((!left)&&up))//隻有一個插頭
{
int t;
if(left)t=left;
else t=up;//這裡少寫個else ,查了好久的錯誤
if(maze[i][j+1]==1||maze[i][j+1]==t)//插頭從右邊出來
{
code[j-1]=0;
code[j]=t;
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
if(maze[i+1][j]==1||maze[i+1][j]==t)//插頭從下邊出來
{
code[j]=0;
code[j-1]=t;
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
}
else if(left==0&&up==0)//沒有插頭
{
code[j-1]=code[j]=0;//不加插頭
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]);
if(maze[i][j+1]&&maze[i+1][j])
{
if(maze[i][j+1]==1&&maze[i+1][j]==1)
{
decode(code,M,hm[cur].state[k]);
code[j-1]=code[j]=2;//加2号插頭
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
//decode(code,M,hm[cur].state[k]);
code[j-1]=code[j]=3;//加3号插頭
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
else if((maze[i][j+1]==2&&maze[i+1][j]==1)||(maze[i+1][j]==2&&maze[i][j+1]==1)||(maze[i][j+1]==2&&maze[i+1][j]==2))
{
decode(code,M,hm[cur].state[k]);
code[j-1]=code[j]=2;
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
else if((maze[i][j+1]==3&&maze[i+1][j]==1)||(maze[i+1][j]==3&&maze[i][j+1]==1)||(maze[i][j+1]==3&&maze[i+1][j]==3))
{
decode(code,M,hm[cur].state[k]);
code[j-1]=code[j]=3;
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
}
}
}
}
void dpblock(int i,int j,int cur)
{
int k;
for(k=0;k<hm[cur].size;k++)
{
decode(code,M,hm[cur].state[k]);
if(code[j-1]!=0||code[j]!=0)continue;
code[j-1]=code[j]=0;//不加插頭
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]);
}
}
void dp_2(int i,int j,int cur)
{
int left,up,k;
for(k=0;k<hm[cur].size;k++)
{
decode(code,M,hm[cur].state[k]);
left=code[j-1];
up=code[j];
if((left==2&&up==0)||(left==0&&up==2))
{
code[j-1]=code[j]=0;
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
else if(left==0&&up==0)
{
if(maze[i][j+1]==1||maze[i][j+1]==2)
{
code[j-1]=0;
code[j]=2;
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
if(maze[i+1][j]==1||maze[i+1][j]==2)
{
code[j-1]=2;
code[j]=0;
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
}
}
}
void dp_3(int i,int j,int cur)
{
int left,up,k;
for(k=0;k<hm[cur].size;k++)
{
decode(code,M,hm[cur].state[k]);
left=code[j-1];
up=code[j];
if((left==3&&up==0)||(left==0&&up==3))
{
code[j-1]=code[j]=0;
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
else if(left==0&&up==0)
{
if(maze[i][j+1]==1||maze[i][j+1]==3)
{
code[j-1]=0;
code[j]=3;
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
if(maze[i+1][j]==1||maze[i+1][j]==3)
{
code[j-1]=3;
code[j]=0;
if(j==M)shift(code,M);
hm[cur^1].push(encode(code,M),hm[cur].dp[k]+1);
}
}
}
}
void init()
{
memset(maze,0,sizeof(maze));
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
{
scanf("%d",&maze[i][j]);
if(maze[i][j]==0)maze[i][j]=1;
else if(maze[i][j]==1)maze[i][j]=0;
//兩種表達方式
//if(maze[i][j]==1||maze[i][j]==0)maze[i][j]^=1;//0變1,1變0
}
}
void solve()
{
int i,j,cur=0;
hm[cur].init();
hm[cur].push(0,0);
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
{
hm[cur^1].init();
if(maze[i][j]==0)dpblock(i,j,cur);
else if(maze[i][j]==1)dpblank(i,j,cur);
else if(maze[i][j]==2)dp_2(i,j,cur);
else if(maze[i][j]==3)dp_3(i,j,cur);
cur^=1;
}
int ans=0;
for(int i=0;i<hm[cur].size;i++)
ans+=hm[cur].dp[i];
if(ans>0)ans-=2;
printf("%d\n",ans);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&N,&M))
{
if(N==0&&M==0)break;
init();
solve();
}
return 0;
}