题目链接:http://poj.org/problem?id=3133
题意:n*m的格子中有两个2和两个3,其余是空白或障碍。找出两条路径分别连接2和3,不经过障碍且不相交。使得两条路径长度和最短?
思路:标记每个联通块是2的还是3的还是都不是。转移时分空白格子、障碍格子和2、3格子三种情况。空白格子在合并时要注意,左侧和上侧若是2或3则必须同时是2或者同时是3;2、3格子转移时,左侧和上侧最多有一个插头且不能冲突。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
#define rush() int CC; for(scanf("%d",&CC);CC--;)
#define Rush(n) while(scanf("%d",&n)!=-1)
#define Rush(n,m) while(scanf("%d%d",&n,&m)!=-1)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(u32 x) {printf("%u\n",x);}
void PR(double x) {printf("%.4lf\n",x);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
const int INF=1000000000;
const int HASHSIZE=30007;
const int N=200005;
int n,m,code[15],cur,pre,sx,sy;
int s[100][100];
int ans;
struct node
{
int e,next[N],head[HASHSIZE];
i64 cnt[N],state[N];
void init()
{
clr(head,-1);
e=0;
}
void push(i64 s,i64 val)
{
int i,x=s%HASHSIZE;
for(i=head[x];i!=-1;i=next[i])
{
if(state[i]==s)
{
if(val<cnt[i]) cnt[i]=val;
return;
}
}
state[e]=s;
cnt[e]=val;
next[e]=head[x];
head[x]=e++;
}
};
node dp[2];
void decode(int code[],int m,i64 st)
{
int i;
FORL0(i,m) code[i]=st&7,st>>=3;
}
void trans(int x,int y)
{
int i;
FOR0(i,m+1) if(code[i]==x) code[i]=y;
}
i64 encode(int code[],int m)
{
i64 ans=0;
int hash[15],i,cnt=3;
clr(hash,-1);
hash[0]=0; hash[1]=1; hash[2]=2;
FOR0(i,m+1)
{
if(hash[code[i]]==-1) hash[code[i]]=cnt++;
code[i]=hash[code[i]];
ans=(ans<<3)|code[i];
}
return ans;
}
void update1(int i,int j)
{
int x,y,k,t,q=j==m?m-1:m;
i64 c;
FOR0(k,dp[pre].e)
{
decode(code,m,dp[pre].state[k]);
c=dp[pre].cnt[k];
x=code[j-1];
y=code[j];
if(x&&y)
{
if(x<=2&&y<=2&&x!=y) continue;
if(x!=y) trans(max(x,y),min(x,y));
code[j-1]=code[j]=0;
dp[cur].push(encode(code,q),c+1);
}
else if(x||y)
{
if(i<n&&s[i+1][j])
{
code[j-1]=x+y;code[j]=0;
dp[cur].push(encode(code,q),c+1);
}
if(j<m&&s[i][j+1])
{
code[j-1]=0;code[j]=x+y;
dp[cur].push(encode(code,q),c+1);
}
}
else if(x==0&&y==0)
{
dp[cur].push(encode(code,q),c);
if(i<n&&j<m&&s[i][j+1]&&s[i+1][j])
{
code[j-1]=code[j]=13;
dp[cur].push(encode(code,q),c+1);
}
}
}
}
void update2(int i,int j)
{
int k,q=j==m?m-1:m;
FOR0(k,dp[pre].e)
{
decode(code,m,dp[pre].state[k]);
if(code[j-1]||code[j]) continue;
dp[cur].push(encode(code,q),dp[pre].cnt[k]);
}
}
void update3(int i,int j)
{
int x,y,k,t,q=j==m?m-1:m;
i64 c;
FOR0(k,dp[pre].e)
{
decode(code,m,dp[pre].state[k]);
c=dp[pre].cnt[k];
x=code[j-1];
y=code[j];
if(x&&y) continue;
if(x)
{
if(x<=2&&x!=s[i][j]-1) continue;
if(x>2) trans(x,s[i][j]-1);
code[j-1]=0;
dp[cur].push(encode(code,q),c+1);
}
else if(y)
{
if(y<=2&&y!=s[i][j]-1) continue;
if(y>2) trans(y,s[i][j]-1);
code[j]=0;
dp[cur].push(encode(code,q),c+1);
}
else
{
if(i<n&&s[i+1][j])
{
code[j-1]=s[i][j]-1;
dp[cur].push(encode(code,q),c+1);
code[j-1]=0;
}
if(j<m&&s[i][j+1])
{
code[j]=s[i][j]-1;
dp[cur].push(encode(code,q),c+1);
}
}
}
}
int DP()
{
pre=0,cur=1;
dp[0].init(); dp[0].push(0,0);
int i,j;
FOR1(i,n) FOR1(j,m)
{
dp[cur].init();
if(s[i][j]==1) update1(i,j);
else if(s[i][j]==0) update2(i,j);
else update3(i,j);
pre^=1;
cur^=1;
}
if(dp[pre].e==0) ans=0;
else ans=dp[pre].cnt[0]-2;
return ans;
}
int main()
{
Rush(n,m)
{
if(!n&&!m) break;
int i,j;
clr(s,0);
FOR1(i,n) FOR1(j,m)
{
RD(s[i][j]);
if(s[i][j]<2) s[i][j]^=1;
}
PR(DP());
}
return 0;
}