UVA 10564-paths through the Hourglass (DP)

Source: Internet
Author: User

The main idea: to give an hourglass, including an inverted triangle and a triangle, each square has a number 0 to 9, from the top of a lattice to go to the bottom of a lattice, to obtain a special value of the path of how many, and output the smallest dictionary order. The path is represented by a starting point and a series of characters ' L ' and ' R '.


Use A[i][j] to denote the number in column J of line I. Because the dictionary order is required to be the smallest, recursion is pushed from the bottom up. The D[i][j][u] indicates how many strips (i,j) and Paths to u, which are inverted triangles in the upper part, can be summed up by d[i+1][j][u-a[i][j] and D[i+1][j+1][u-a[i][j], and the lower half is a positive triangle, which can be d[i +1][J-1][U-A[I][J]] and D[i+1][j][u-a[i][j]] are added recursively.

Finally, find the most left-most starting point and push back the last line to get a path with the smallest dictionary order.


State transition equation:

D[I][J]=D[I+1][J][U-A[I][J]]+D[I+1][J+1][U-A[I][J]] (upper part)

D[I][J]=D[I+1][J-1][U-A[I][J]]+D[I+1][J][U-A[I][J]] (lower half)


#include <stdio.h> #include <stdlib.h>typedef long long ll;int a[45][45]; LL D[45][45][510];char ans[45];int Main (void) {int i,j,u,v,p,n,m,sit; LL sum;scanf ("%d%d", &n,&m); while ((n!=0) | | (m!=0)) {for (i=1;i<=n;i++) {for (j=1;j<=n-i+1;j++) {scanf ("%d", &a[i][j]);}} for (i=2;i<=n;i++) {for (j=1;j<=i;j++) {scanf ("%d", &a[i+n-1][j]);}} p=9* (2*n-1); if (m>p) {printf ("0\n\n");} Else{for (j=1;j<=n;j++) {d[2*n-1][j][a[2*n-1][j]]=1;} for (i=2*n-2;i>=n;i--) {for (j=1;j<=i-n+1;j++) {for (u=a[i][j];u<=p;u++) {d[i][j][u]=d[i+1][j][u-a[i][j]]+d [I+1] [J+1] [U-a[i][j];}}} for (i=n-1;i>=1;i--) {for (u=a[i][1];u<=p;u++) {d[i][1][u]=d[i+1][1][u-a[i][1]];} for (u=a[i][n-i+1];u<=p;u++) {d[i][n-i+1][u]=d[i+1][n-i][u-a[i][n-i+1]];} for (j=2;j<n-i+1;j++) {for (u=a[i][j];u<=p;u++) {d[i][j][u]=d[i+1][j-1][u-a[i][j]]+d[i+1][j][u-a[i][j]];}}} Sum=0;for (j=1;j<=n;j++) {sum=sum+d[1][j][m];} if (sum==0) {printf ("0\n\n");} else{printf ("%lld\n", sum); for (j=1;j<=n;j++) {if (d[1][j][m]!=0) {Sit=v=j;breaK;}} for (i=1;i<=n-1;i++) {m=m-a[i][v];if (v==1) {ans[i]= ' R ';} else if (v==n-i+1) {ans[i]= ' L '; v=v-1;} Else{if (d[i+1][v-1][m]!=0) {v=v-1;ans[i]= ' L ';} else{ans[i]= ' R ';}}} for (i=n;i<2*n-1;i++) {m=m-a[i][v];if (d[i+1][v][m]!=0) {ans[i]= ' L ';} else{v=v+1;ans[i]= ' R ';}} ans[2*n-1]=0;printf ("%d%s\n", sit-1,ans+1);} for (i=1;i<=2*n;i++) {for (j=1;j<=2*n;j++) {for (u=0;u<=p;u++) {d[i][j][u]=0;}}}} scanf ("%d%d", &n,&m);} return 0;}


UVA 10564-paths through the Hourglass (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.