URAL1519 Formula 1
Background
Regardless 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 circuit should 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.
Problem
Who 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 our heroes 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 rectangle N* 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.
Input
The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are 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. There are at least 4 cells without gopher holes.
Output
You should output the desired number of ways. It is guaranteed, that it does not exceed 2 63-1.
Example
input | output |
---|---|
4 4 **.. .... .... .... |
2 |
4 4 .... .... .... .... |
6 |
这算完全体的插头DP吧?
果然超麻烦
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #define LL long long 6 using namespace std; 7 const int mod=100007; 8 const int mxn=650010; 9 struct edge{ 10 int v,nxt,id; 11 }e[2][mxn]; 12 int hd[2][mod+2],mct[2]; 13 LL f[2][mxn]; 14 int now,pre; 15 void add(int v,LL w){//hash 16 int z=v%mod; 17 for(int i=hd[now][z];i;i=e[now][i].nxt){ 18 if(v==e[now][i].v){f[now][i]+=w;return;} 19 } 20 e[now][++mct[now]]=(edge){v,hd[now][z],z};hd[now][z]=mct[now]; 21 f[now][mct[now]]=w; 22 return; 23 } 24 int b[20]; 25 int n,m; 26 char mp[20][20]; 27 int main(){ 28 int i,j; 29 scanf("%d%d",&n,&m); 30 for(i=1;i<=n;i++)scanf("%s",mp[i]+1); 31 int ex,ey; 32 for(i=n;i;i--){ 33 for(j=m;j;j--)if(mp[i][j]=='.'){ex=i;ey=j;break;} 34 if(j)break; 35 } 36 for(i=1;i<=m;i++)b[i]=i<<1; 37 now=0;pre=1; 38 add(0,1); 39 LL ans=0; 40 for(i=1;i<=n;i++){ 41 for(int k=1;k<=mct[now];k++)e[now][k].v<<=2; 42 for(j=1;j<=m;j++){ 43 swap(now,pre); 44 for(int k=1;k<=mct[now];k++)hd[now][e[now][k].id]=0; 45 mct[now]=0; 46 //init 47 // printf("i:%d j:%d mp:%c\n",i,j,mp[i][j]); 48 for(int k=1;k<=mct[pre];k++){ 49 int v=e[pre][k].v; 50 int x=(v>>b[j-1])&3; 51 int y=(v>>b[j])&3; 52 LL w=f[pre][k]; 53 // printf("%d %d k:%d v:%d w:%I64d\n",i,j,k,v,w); 54 // printf("x:%d y:%d\n",x,y); 55 if(mp[i][j]=='.'){ 56 if(!(x+y)){ 57 if(mp[i+1][j]=='.' && mp[i][j+1]=='.') 58 add(v^(1<<b[j-1])^(2<<b[j]),w); 59 } 60 else if(!x && y==1){ 61 if(mp[i+1][j]=='.')add(v^(1<<b[j-1])^(1<<b[j]),w); 62 if(mp[i][j+1]=='.')add(v,w); 63 } 64 else if(!x && y==2){ 65 if(mp[i+1][j]=='.')add(v^(2<<b[j-1])^(2<<b[j]),w); 66 if(mp[i][j+1]=='.')add(v,w); 67 } 68 else if(x==1 && !y){ 69 if(mp[i][j+1]=='.')add(v^(1<<b[j-1])^(1<<b[j]),w); 70 if(mp[i+1][j]=='.')add(v,w); 71 } 72 else if(x==1 && y==1){// (( 如果右边的左右括号能匹配,这两个可以接起来 73 int cnt=1; 74 for(int c=j+1;c<=m;c++){ 75 int tmp=(v>>b[c])&3; 76 if(tmp==1)cnt++; 77 if(tmp==2)cnt--;//不能else 78 if(!cnt){add((v^(1<<b[j-1])^(1<<b[j]))-(1<<b[c]),w);break;} 79 } 80 81 } 82 else if(x==1 && y==2){//只有在终点才能这么合并 83 if(i==ex && j==ey)ans+=w; 84 } 85 else if(x==2 && !y){ 86 if(mp[i][j+1]=='.')add(v^(2<<b[j-1])^(2<<b[j]),w); 87 if(mp[i+1][j]=='.')add(v,w); 88 } 89 else if(x==2 && y==1){ 90 add(v^(2<<b[j-1])^(1<<b[j]),w); 91 } 92 else if(x==2 && y==2){ 93 int cnt=-1; 94 for(int c=j-2;c>=0;c--){ 95 int tmp=(v>>b[c])&3; 96 if(tmp==1)cnt++; 97 if(tmp==2)cnt--; 98 if(!cnt){add((v^(2<<b[j-1])^(2<<b[j]))+(1<<b[c]),w);break;} 99 } 100 } 101 } 102 else{if(!(x+y))add(v,w);} 103 } 104 } 105 } 106 printf("%I64d\n",ans); 107 return 0; 108 }
本文为博主原创文章,转载请注明出处。