ural 1519(插头DP入门题)
2011-06-08 16:56
465 查看
1519. Formula 1Time Limit: 1.0 secondMemory Limit: 16 MB BackgroundRegardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuitshould be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it. ProblemWho will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But ourheroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said. It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangleN*M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for N = M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here. InputThe first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, whichare the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located. OutputYou should output the desired number of ways. It is guaranteed, that it does not exceed 263-1.Samples
Problem Source: Timus Top Coders: Third Challenge |
分析:这题算是插头DP的真正入门题了,以前做过的插头DP都是多条回路或者多米诺骨牌一类的,其实算不上插头。。。汗,现在才明白,插头DP要用hash存储状态,尽管用二进制直接表示很方便,但是出现太多无用状态,hash表正好解决这个问题,自己写个hash函数,用于存储和状态转移,最关键的还是如何用二进制表示状态,其实是用两个二进制位表示三种插头状态,01->左插头(左括号) 10->右插头(右括号) 00->空插头(无括号),这题插头数量小,适合用括号表示法,具体做法十分复杂,多种状态的讨论,直接看代码吧,实在是没时间写了。。。
代码:
#include<cstdio> #include<cstring> using namespace std; const int mm=15511; struct data { int s[mm],h[mm],p[mm],t; long long d[mm]; inline int hash(int x,long long dd) { int i,c=x%mm; for(i=h[c];i>=0;i=p[i]) if(s[i]==x) { d[i]+=dd; return i; } s[t]=x,p[t]=h[c],h[c]=t,d[t]=dd; return t++; } void clear() { t=0,memset(h,-1,sizeof(h)); } }f[2]; int i,j,g1,g2,u,k,n,m,en,em,a,b,a1,a2,b1,b2; bool g[13][13]; inline int eat(int s,int a,int b,int c,bool f) { int n=1,x; while(n) { if(f)a<<=2,b<<=2,c<<=2; else a>>=2,b>>=2,c>>=2; x=s&c; if(x==a)++n; if(x==b)--n; } return (s^b)|a; } inline bool ok(int c) { if(c==1)return g[i+1][j]; if(c==2)return g[i][j+1]; if(c==3)return g[i+1][j]&&g[i][j+1]; return 1; } inline void work(int S,long long d) { int x,y; x=a&S,y=b&S; if(x==0&&y==0) { if(ok(3))f[g2].hash(S|a1|b2,d); } else if(x==0&&y==b1) { if(ok(2))f[g2].hash(S,d); if(ok(1))f[g2].hash((S^b1)|a1,d); } else if(x==a1&&y==0) { if(ok(1))f[g2].hash(S,d); if(ok(2))f[g2].hash((S^a1)|b1,d); } else if(x==a1&&y==b1)f[g2].hash(eat(S^a1^b1,b1,b2,b,1),d); else if(x==0&&y==b2) { if(ok(2))f[g2].hash(S,d); if(ok(1))f[g2].hash((S^b2)|a2,d); } else if(x==a2&&y==0) { if(ok(1))f[g2].hash(S,d); if(ok(2))f[g2].hash((S^a2)|b2,d); } else if(x==a2&&y==b2)f[g2].hash(eat(S^a2^b2,a2,a1,a,0),d); else if(x==a2&&y==b1)f[g2].hash(S^a2^b1,d); } inline void end() { for(en=n-1;en>=0;--en) for(em=m-1;em>=0;--em)if(g[en][em])return; } inline long long DP() { end(); f[0].clear(); f[0].hash(0,1); for(g1=1,g2=i=0;i<=en;++i) { for(k=0;k<f[g2].t;++k)f[g2].s[k]<<=2; a=3,b=3<<2,a1=1,a2=2,b1=1<<2,b2=2<<2; for(j=0;j<m;a<<=2,b<<=2,a1<<=2,a2<<=2,b1<<=2,b2<<=2,++j) if(g[i][j])for(g1=!g1,g2=!g2,f[g2].clear(),k=0;k<f[g1].t;++k) { if(i==en&&j==em) { if((f[g1].s[k]&(a1|b2))==(a1|b2))return f[g1].d[k];else continue; } work(f[g1].s[k],f[g1].d[k]); } } return 0; } int main() { char c; memset(g,0,sizeof(g)); scanf("%d%d",&n,&m); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf(" %c",&c),g[i][j]=(c=='.'); printf("%I64d/n",DP()); return 0; }
一开始先用DFS搜索出所有状态转移的情况,结果因为无用状态太多,不仅爆内存,而且降速啊。。。
![](http://img.baidu.com/hi/jx2/j_0004.gif)
贴下来留念吧:
#include<cstdio> #include<cstring> using namespace std; const int mm=50007; struct data { int s[mm],h[mm],p[mm],t; long long d[mm]; int hash(int x,long long dd) { int i,c=x%mm; for(i=h[c];i>=0;i=p[i]) if(s[i]==x) { d[i]+=dd; return i; } s[t]=x,p[t]=h[c],h[c]=t,d[t]=dd; return t++; } void clear() { t=0,memset(h,-1,sizeof(h)); } }f[2],o; struct gra { int s[11],n[11],t; void insert(int ss,int nn) { s[t]=ss,n[t]=nn,++t; } }S[mm][13]; int i,j,g1,g2,v,u,k,n,m,en,em; bool g[13][13]; int dfs(int s,int r,int t) { if(t>m)return r?0:o.hash(s,0); dfs(s<<2,r,t+1); dfs((s<<2)|2,r+1,t+1); if(r)dfs((s<<2)|1,r-1,t+1); return 0; } int eat(int s,int a,int b,int c,bool f) { int n=1,x; while(n) { if(f)a<<=2,b<<=2,c<<=2; else a>>=2,b>>=2,c>>=2; x=s&c; if(x==a)++n; if(x==b)--n; } return (s^b)|a; } void pre() { o.clear(); dfs(0,0,0); int i,j,a=3,b=3<<2,x,y,a1=1,a2=2,b1=1<<2,b2=2<<2; for(j=0;j<m;a<<=2,b<<=2,a1<<=2,a2<<=2,b1<<=2,b2<<=2,++j) for(i=0;i<o.t;++i) { S[i][j].t=0; x=a&o.s[i],y=b&o.s[i]; if(x==0&&y==0)S[i][j].insert(o.s[i]|a1|b2,3); else if(x==0&&y==b1)S[i][j].insert(o.s[i],2),S[i][j].insert((o.s[i]^b1)|a1,1); else if(x==a1&&y==0)S[i][j].insert(o.s[i],1),S[i][j].insert((o.s[i]^a1)|b1,2); else if(x==a1&&y==b1)S[i][j].insert(eat(o.s[i]^a1^b1,b1,b2,b,1),0); else if(x==0&&y==b2)S[i][j].insert(o.s[i],2),S[i][j].insert((o.s[i]^b2)|a2,1); else if(x==a2&&y==0)S[i][j].insert(o.s[i],1),S[i][j].insert((o.s[i]^a2)|b2,2); else if(x==a2&&y==b2)S[i][j].insert(eat(o.s[i]^a2^b2,a2,a1,a,0),0); else if(x==a2&&y==b1)S[i][j].insert(o.s[i]^a2^b1,0); } } inline bool ok(int c) { if(c==1)return g[i+1][j]; if(c==2)return g[i][j+1]; if(c==3)return g[i+1][j]&&g[i][j+1]; return 1; } void end() { for(en=n-1;en>=0;--en) for(em=m-1;em>=0;--em)if(g[en][em])return; } long long DP() { end(); f[0].clear(); f[0].hash(0,1); for(g1=1,g2=i=0;i<=en;++i) { for(k=0;k<f[g2].t;++k)f[g2].s[k]<<=2; for(j=0;j<m;++j) if(g[i][j])for(g1=!g1,g2=!g2,f[g2].clear(),k=0;k<f[g1].t;++k) { v=o.hash(f[g1].s[k],0); if(i==en&&j==em) { if((o.s[v]&(9<<(j<<1)))==(9<<(j<<1)))return f[g1].d[k];else continue; } for(u=0;u<S[v][j].t;++u) if(ok(S[v][j].n[u]))f[g2].hash(S[v][j].s[u],f[g1].d[k]); } } return 0; } int main() { char c; memset(g,0,sizeof(g)); scanf("%d%d",&n,&m); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf(" %c",&c),g[i][j]=(c=='.'); pre(); printf("%I64d/n",DP()); return 0; }
2012-9-27
重新写了下:
#include<cstdio> #include<cstring> using namespace std; const int mm=15511; typedef long long LL; struct hashTable { int h[mm],s[mm],p[mm],t; LL v[mm]; void insert(int w,LL val) { int i,id=w%mm; for(i=h[id];i>=0;i=p[i]) if(s[i]==w) { v[i]+=val; return; } v[t]=val,s[t]=w,p[t]=h[id],h[id]=t++; } void clear() { t=0,memset(h,-1,sizeof(h)); } }f[2]; bool g[22][22]; int i,j,k,n,m,em,g1,g2; bool ok(int s) { if(s==1)return g[i+1][j]; if(s==2)return g[i][j+1]; if(s==3)return g[i+1][j]&&g[i][j+1]; } int Link(int s,bool flag) { int n=1,w,x=3<<(j<<1),a=(flag?1:2)<<(j<<1); while(n) { if(flag)a<<=2,x<<=2; else a>>=2,x>>=2; w=s&x; if(w)n+=(w==a)?1:-1; } return s^x; } void Work(int s,LL val) { int e,w=j<<1,ss=(s>>w)&15; if(ss==9)return; if(!ss) { if(ok(3))f[g2].insert(s|(9<<w),val); } else if(!(ss&3)||!(ss&12)) { if(ss&3)e=1,ss|=ss<<2; else e=0,ss|=ss>>2; if(ok(1+!e))f[g2].insert(s,val); if(ok(1+e))f[g2].insert(s^(ss<<w),val); } else if(ss==6)f[g2].insert(s^(ss<<w),val); else f[g2].insert(Link(s^(ss<<w),ss==5),val); } void end() { while(n--) for(em=m-1;em>=0;--em) if(g [em])return; } LL PlugDP() { end(); f[0].clear(); f[0].insert(0,1); for(g2=i=0;i<=n;++i) { for(k=0;k<f[g2].t;++k)f[g2].s[k]<<=2; for(j=0;j<m;++j) if(g[i][j])for(g1=g2,g2=!g2,f[g2].clear(),k=0;k<f[g1].t;++k) { if(i==n&&j==em) if(((f[g1].s[k]>>(j<<1))&15)==9)return f[g1].v[k]; else continue; Work(f[g1].s[k],f[g1].v[k]); } } return 0; } char c; int main() { scanf("%d%d",&n,&m); memset(g,0,sizeof(g)); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf(" %c",&c),g[i][j]=(c=='.'); printf("%I64d\n",PlugDP()); return 0; }
相关文章推荐
- [URAL1519] Formula 1 [插头dp入门]
- 插头DP——从不会到入门(POJ 2411,HDU 1565,HDU 2167,HDU 1693,Ural 1519)
- ural 1519 插头DP 入门
- Ural1519 Formula 1 插头dp入门
- URAL 1519 Formula 1(插头DP,入门题)
- URAL - 1519 Formula 1 (插头DP)
- Ural 1519 Formula 1( 插头dp )
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
- URAL 1519 基础插头DP
- Ural 1519 Formula 1 插头DP
- [URAL1519]-Formula 1-插头DP
- [ural1519]Formula 1 && 插头DP(括号表示法)
- 【Ural1519】Formula 1 插头DP模板
- [ural1519]Formula 1 && 插头DP
- URAL1519 Formula 1 【插头dp】
- URAL 1519 Formula 1(插头DP)
- [BZOJ]1814 Ural 1519 Formula 1 插头DP
- Ural1519 Formula 1 插头dp
- 【BZOJ1814】Ural 1519 Formula 1 插头DP