A - The least round way

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),从左上角开始,只能往下或往右走,走到右下角,所经过的所有数相乘,使得末尾0个数最小,并输出相应的走法。

思路:

这是一个非常经典的dp问题改编而来的。要求末尾0最少,10=2*5,即0是由5跟2得到的。从而转化为找一条包含最少的2跟最少的5,答案就是min(2,5);

路径输出:确定答案是那一条路(2或5),回溯回去,看当前坐标正上方跟左边哪个方向的(2或5)最小就走哪,直到走到左上角为止。

坑点:因为数据包括0,假如路径0的个数>0,那么无疑走经过0的路是最佳的。

#include <iostream>

using namespace std;


int dp[1005][1005][2];

int solve(int x, int m){//求出x包含几个m
    int cnt=0;
    while(x%m==0){
        cnt++;
        x/=m;
    }
    return cnt;
}
int main()
{
    int n,x,pos=-1,m=0;
    string s="";
    cin>>n;
    for(int i=1; i<=n; i++){
       for(int j=1; j<=n; j++){
          cin>>x;
          if(x==0){//特别考虑有0的情况
            pos=i;
            continue;
          }
          dp[i][j][0]=solve(x,2);
          dp[i][j][1]=solve(x,5);
       }
    }
    for(int i=2; i<=n; i++){
          dp[1][i][0]+=dp[1][i-1][0];
          dp[1][i][1]+=dp[1][i-1][1];
          dp[i][1][0]+=dp[i-1][1][0];
          dp[i][1][1]+=dp[i-1][1][1];
    }
    for(int i=2; i<=n; i++){
       for(int j=2; j<=n; j++){
            dp[i][j][0]+=min(dp[i-1][j][0],dp[i][j-1][0]);
            dp[i][j][1]+=min(dp[i-1][j][1],dp[i][j-1][1]);
       }
    }
    if(pos!=-1&&min(dp[n][n][0],dp[n][n][1])>0)//路径有0但是2或5的最小值>0,即有意义的。
    {
       cout<<1<<endl;
       for(int i=1; i<pos; i++){
          cout<<"D";
       }
       for(int i=1; i<n; i++){
          cout<<"R";
       }
       for(int i=pos; i<n; i++){
          cout<<"D";
       }
       return 0;
    }
    if(dp[n][n][0]>dp[n][n][1]){//确定最终选最少2还是5
       m=1;
    }
   cout<<min(dp[n][n][0],dp[n][n][1])<<endl;
   int i=n,j=n;
   while(true){//回溯输出
       if(dp[i-1][j][m]<dp[i][j-1][m])//正上方跟左边谁比较少走就走哪
       {
           i--;
           s="D"+s;//路径构造
       }
       else{
          j--;
          s="R"+s;
       }
       if(i==1){//边界处理,走到第一行只能往右走
         for(int x=1; x<j; x++){
            s="R"+s;
         }
         break;
       }
       if(j==1){//边界处理,走到第一列只能往下走
          for(int x=1; x<i; x++){
            s="D"+s;
         }
         break;
       }
    }
    cout<<s<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值