| Uva-10564paths through the Hourglass |
Test instructions
request from the first floor to the bottom layer, only to the lower left or right downQ. How many paths are equal to s? If any, the path to the smallest dictionary order is output. F[i][j][k] The number of programmes from bottom to top, J and Kup and down transfers are different, separate processingno need to judge out the hourglassPrinting solutions look backwards, just go left as far as you can. Satay forgot to comment out the file wa several times
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intn= -, m=505, inf=1e9;typedefLong LongLl;inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,s,w[n<<1][n];ll f[n<<1][n][m];voiddp () {memset (F,0,sizeof(f)); for(intj=1; j<=n;j++) F[n+n-1][j][w[n+n-1][j]]=1; //Xia for(inti=n+n-2; i>=n;i--) for(intj=1; j<=i-n+1; j + +) for(intk=w[i][j];k<=s;k++) F[i][j][k]=f[i+1][j][k-w[i][j]]+f[i+1][j+1][k-W[i][j]]; //Shang for(inti=n-1; i>=1; i--) for(intj=1; j<=n-i+1; j + +) for(intk=w[i][j];k<=s;k++) F[i][j][k]=f[i+1][j][k-w[i][j]]+f[i+1][j-1][k-W[i][j]]; }voidPrintintIintJ,ll k) {//printf ("Print%d%d%d\n", i,j,k); if(i==n+n-1)return; if(i<N) { if(j>1&&f[i+1][j-1][k-W[i][j]]) {Putchar ('L'); Print (I+1, J-1, K-W[i][j]); }Else{Putchar ('R'); Print (I+1, j,k-W[i][j]); } }Else{ if(f[i+1][j][k-W[i][j]]) {Putchar ('L'); Print (I+1, j,k-W[i][j]); }Else{Putchar ('R'); Print (I+1, j+1, K-W[i][j]); } }}intMain () {//freopen ("1.in", "R", stdin); //freopen ("1.out", "w", stdout); while(SCANF ("%d%d", &n,&s)!=eof&& (n| |s)) {memset (W,0,sizeof(w)); for(intI=1; i<=n;i++) for(intj=1; j<=n-i+1; j + +) w[i][j]=read (); for(inti=n+1; i<=n+n-1; i++) for(intj=1; j<=i-n+1; j + +) w[i][j]=read (); DP (); intp=0; ll Sum=0; for(inti=n;i>=1; i--)if(f[1][i][s]) {p=i;sum+=f[1][i][s];} printf ("%lld\n", sum); if(p) printf ("%d", P-1), Print (1, P,s); Putchar ('\ n'); }}
UVA 10564 Paths through the HOURGLASS[DP print]