Codeforces_The least round way

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
3
1 2 3
4 5 6
7 8 9
output
0
DDRR

题意:从左上到右下,选择一条路,使得路上每个格子中的数相乘末尾的0最少。

思路:一般情况下,只有2和5的组合才会在末尾形成0。之前一直想在dp过程中同时推2和5,然后就wa了,其实分别推2和5并记录两个路径就行,因为一般情况下,末尾0等于min(dp2[n][n],dp5[n][n]),但是还有特殊情况,就是路径中遇到0,经过0的路径最终只有一个0,但还有可能一个0都没有,要讨论全面。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
#define N 1005
#define INF 1000000
int num2[N][N],num5[N][N];
int dp2[N][N],dp5[N][N];
char dir2[N][N],dir5[N][N];

int getNum(int num,int a)
{
    if(num==0)
        return 1;
    int cnt=0;
    while(num%a==0)
    {
        cnt++;
        num/=a;
    }
    return cnt;
}

void dfs(char dir[][N], int x,int y)
{
    if(x==1&&y==1)
        return;
    if(dir[x][y]=='R')
        dfs(dir,x,y-1);
    else if(dir[x][y]=='D')
        dfs(dir,x-1,y);
    printf("%c",dir[x][y]);
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int z_flag=0,z_x,z_y;
        memset(num2,0,sizeof(num2));
        memset(num5,0,sizeof(num5));
        memset(dp2,0,sizeof(dp2));
        memset(dp5,0,sizeof(dp5));
        //memset(dp,0,sizeof(dp));
        /*for(int i=0; i<=n; i++)
        {
            dp2[0][i]=dp2[i][0]=INF;
            dp5[0][i]=dp5[i][0]=INF;
        }
        dp2[0][1]=dp2[1][0]=0;
        dp5[0][1]=dp5[1][0]=0;*/
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                int num;
                scanf("%d",&num);
                if(num)
                {
                    num2[i][j]=getNum(num,2);
                    num5[i][j]=getNum(num,5);
                }
                else
                {
                    z_flag=1;
                    z_x=i;
                    z_y=j;
                }
            }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                if(i==1&&j==1)
                {
                    dir2[i][j]=' ';
                    dp2[i][j]=num2[i][j];
                }
                else if(i==1)
                {
                    dir2[i][j]='R';
                    dp2[i][j]=dp2[i][j-1]+num2[i][j];
                }
                else if(j==1)
                {
                    dir2[i][j]='D';
                    dp2[i][j]=dp2[i-1][j]+num2[i][j];
                }
                else
                {
                    if(dp2[i-1][j]<dp2[i][j-1])
                    {
                        dir2[i][j]='D';
                        dp2[i][j]=dp2[i-1][j]+num2[i][j];
                    }
                    else
                    {
                        dir2[i][j]='R';
                        dp2[i][j]=dp2[i][j-1]+num2[i][j];
                    }
                }
            }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                if(i==1&&j==1)
                {
                    dir5[i][j]=' ';
                    dp5[i][j]=num5[i][j];
                }
                else if(i==1)
                {
                    dir5[i][j]='R';
                    dp5[i][j]=dp5[i][j-1]+num5[i][j];
                }
                else if(j==1)
                {
                    dir5[i][j]='D';
                    dp5[i][j]=dp5[i-1][j]+num5[i][j];
                }
                else
                {
                    if(dp5[i-1][j]<dp5[i][j-1])
                    {
                        dir5[i][j]='D';
                        dp5[i][j]=dp5[i-1][j]+num5[i][j];
                    }
                    else
                    {
                        dir5[i][j]='R';
                        dp5[i][j]=dp5[i][j-1]+num5[i][j];
                    }
                }
            }
            if(z_flag)
            {
                if(dp2[n][n]==0)
                {
                    printf("0
");
                    dfs(dir2,n,n);
                    printf("
");
                }
                else if(dp5[n][n]==0)
                {
                    printf("0
");
                    dfs(dir5,n,n);
                    printf("
");
                }
                else
                {
                    printf("1
");
                    for(int i=1;i<z_x;i++)
                        printf("D");
                    for(int i=1;i<n;i++)
                        printf("R");
                    for(int i=z_x;i<n;i++)
                        printf("D");
                    printf("
");
                }
            }
            else
            {
                if(dp2[n][n]<dp5[n][n])
                {
                    printf("%d
",dp2[n][n]);
                    dfs(dir2,n,n);
                    printf("
");
                }
                else
                {
                    printf("%d
",dp5[n][n]);
                    dfs(dir5,n,n);
                    printf("
");
                }
            }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jasonlixuetao/p/6031524.html