1519. Formula 1Time limit:1.0 Second
Memory limit:64 MB
Backgroundregardless of the fact, that Vologda could don't get rights to hold the Winter Olympic games of 20**, it's well-k Nown, that's the city would conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should being built as well as hotels, restaurants, international Airpo Rt-everything for Formula 1 fans, who'll flood the city soon. But if all the hotels and a half of the restaurants were built, it appeared, which at the site for the future circuit a l OT of Gophers lived in their holes. Since We like animals very much, ecologists would never allow to build the race circuit over the holes. The mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it. Problemwho would be smart enough to draw a plan of the 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 we heroes were notLooking for easy life and set much more difficult problem: "Certainly, our mayor would be glad, if we find how many ways of Building the circuit is there! "-they said. It should be said, which the circuit in Vologda are going to being rather simple. It'll be a rectangle
N*
MCells in size with a single circuit segment built through each cell. Each segment should is parallel to one of rectangle's sides, so only right-angled bends is on the circuit. At the picture below and samples is given for
N=
M= 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There is no other ways to build the circuit here. Inputthe first line contains the integer numbers
Nand
M(2≤
N,
M≤12). Each of the next
NLines contains
MCharacters, which is the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should is built, and character "*" (asterisk)-a cell, wher E A gopher hole is located. Outputyou should output the desired number of ways. It is guaranteed, which it does not exceed 2 the-1.samples
| input |
Output |
4 4** ........ |
2 |
4 4 .......... |
6
|
Plug DP template problem.
Very good article.
My code is processed in the following order:
1.##--> ()
2.# (--># (or) # #)-->#) or) #
3. (#--> (# or # () #-->) # or #)
4. (-->##
5.))-->##
6. ()-->##
7.) (-->##
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include < cmath> #include <cstdlib> #define LL long long#define M 300000+5using namespace Std;char s[20];int tot,a[15][15], N,m,total[2],now,pre,ex,ey,bit[20];int h[m];struct edge{int Y,ne;} E[M]; ll F[2][m],state[2][m],ans=0;void Solve (ll s,ll num) {int pos=s% (M-5); for (int i=h[pos];i;i=e[i].ne) if (State[now][e[i] . y]==s) {F[now][e[i].y]+=num;return;} Total[now]++;state[now][total[now]]=s;f[now][total[now]]=num;e[++tot].y=total[now];e[tot].ne=h[pos];h[pos]=tot ;} void Plugdp () {now=0;total[now]=1;state[now][1]=0;f[now][1]=1;for (int i=1;i<=n;i++) {for (int j=1;j<=total[now ];j++) state[now][j]<<=2;for (int j=1;j<=m;j++) {Tot=0;memset (h,0,sizeof (h));p Re=now,now=pre^1;total[now] =0;for (int k=1;k<=total[pre];k++) {LL s=state[pre][k]; LL num=f[pre][k];int p= (s>>bit[j-1])%4,q= (S>>bit[j])%4;if (!a[i][j]) {if (p+q==0) Solve (s,num);} else if (p+q==0) {if (!a[i][j+1]| |! A[I+1][j]) continue;s=s+ (1<<bit[j-1]) +2* (1<<bit[j]); Solve (s,num);} else if (!p&&q) {if (a[i][j+1]) Solve (s,num), if (A[i+1][j]) s=s-q* (1<<bit[j]) +q* (1<<bit[j-1]), Solve (s,num);} else if (p&&!q) {if (A[i+1][j]) Solve (s,num), if (a[i][j+1]) s=s-p* (1<<bit[j-1]) +p* (1<<bit[j]), Solve (s,num);} else if (p+q==2) {int b=1;for (int t=j+1;t<=m;t++) {int v= (s>>bit[t])%4;if (v==1) b++;if (v==2) b--;if (!b) {s=s-(1 <<bit[t]); break;}} s=s-(1<<bit[j-1])-(1<<bit[j]); Solve (s,num);} else if (p+q==4) {int b=-1;for (int t=j-2;t>=0;t--) {int v= (s>>bit[t])%4;if (v==1) b++;if (v==2) b--;if (!b) {s=s+ ( 1<<bit[t]); break;}} s=s-2* (1<<bit[j-1]) -2* (1<<bit[j]); Solve (s,num);} else if (p==1&&q==2) {if (I==ex&&j==ey) ans+=num;} else if (p==2&&q==1) {s=s-2* (1<<bit[j-1])-(1<<bit[j]); Solve (S,num);}}}} int main () {for (Int. i=0;i<15;i++) bit[i]=i<<1;scanf ("%d%d", &n,&m); for (int i=1;i<=n;i++) {scanf("%s", s+1); for (int j=1;j<=m;j++) {a[i][j]=s[j]== '. '; if (s[j]== '. ') Ex=i,ey=j;}} PLUGDP ();p rintf ("%lld\n", ans); return 0;}
"URAL 1519" "Plug dp template" Formula 1