codeforce 2B

来源:互联网 发布:千牛淘宝卖家助手 编辑:程序博客网 时间:2024/06/06 20:37
B. The least round way
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that

  • starts in the upper left cell of the matrix;
  • each following cell is to the right or down from the current cell;
  • the way ends in the bottom right cell.

Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.

Input

The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).

Output

In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.

Examples
input
31 2 34 5 67 8 9
output
0DDRR
题解:对2的个数,5的个数Dp,取最小的,有0特判
#include<stdio.h>#include<iostream>#include<string>#include<algorithm>#include<string.h>using namespace std;int a[1005][1005],dp1[1005][1005],dp2[1005][1005],n;void dfs(int dp[1005][1005],int x,int y)//回溯记路径 ,从最后一个点开始,要么左要么上 {if(x==1&&y==1)return;int x1=x-1,y1=y-1;if(x1>=1&&x1<=n&&y1>=1&&y1<=n){if(dp[x1][y]>dp[x][y1]){dfs(dp,x,y1);printf("R");}else{dfs(dp,x1,y);printf("D");}}else if(x1<1||x1>n){dfs(dp,x,y1);printf("R");}    else if(y1<1||y1>n)    {    dfs(dp,x1,y);    printf("D");}} int main(){int i,j,x,y,biao;while(~scanf("%d",&n)){biao=0;memset(dp1,0,sizeof(dp1));memset(dp2,0,sizeof(dp2));for(i=1;i<=n;i++){for(j=1;j<=n;j++){scanf("%d",&a[i][j]);if(a[i][j]==0){x=i;y=j;biao=1;dp1[i][j]=1;dp2[i][j]=1;}else{int t=a[i][j];while(a[i][j]%5==0){dp1[i][j]++;a[i][j]/=5;}while(t%2==0){dp2[i][j]++;t/=2;}}}}for(i=2;i<=n;i++){dp1[1][i]=dp1[1][i-1]+dp1[1][i];dp1[i][1]=dp1[i-1][1]+dp1[i][1];dp2[1][i]=dp2[1][i-1]+dp2[1][i];dp2[i][1]=dp2[i-1][1]+dp2[i][1];}for(i=2;i<=n;i++){for(j=2;j<=n;j++)                                                                                                                                 dp1[i][j]+=min(dp1[i-1][j],dp1[i][j-1]);}for(i=2;i<=n;i++){for(j=2;j<=n;j++)                                                                                                                                dp2[i][j]+=min(dp2[i-1][j],dp2[i][j-1]);}if(biao==1){if(min(dp1[n][n],dp2[n][n])>1){printf("1\n");for(i=1;i<y;i++)printf("R");for(i=2;i<=n;i++)printf("D");for(i=y+1;i<=n;i++)printf("R");printf("\n");}else{if(dp1[n][n]<=1){        printf("%d\n",dp1[n][n]);       dfs(dp1,n,n);}else{     printf("%d\n",dp2[n][n]);      dfs(dp2,n,n);}}}else{    if(dp1[n][n]<dp2[n][n])    {    printf("%d\n",dp1[n][n]);dfs(dp1,n,n);}else{printf("%d\n",dp2[n][n]);dfs(dp2,n,n);}printf("\n");}}}

原创粉丝点击