CF 2B The least round way

B. The least round way
time limit per test
5 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.

Sample test(s)
input
3
1 2 3
4 5 6
7 8 9
output
0
DDRR

题意是给你一个n * n 的矩阵,他的每一步只能向下或向右走,问从左上角到右下角的路径中乘积中零最少的路径,输出路径和零的个数。
思路:由于每个位置只能有其上的位置或其右的位置得到,两者取min即可;
dp[i][j][k]表示从(0 , 0)到(i , j)位置的路径中乘积零最小的路径所包含的2数(k = 0)5数(k = 1)
特例是矩阵中有零且有2和5,则必走有零的那条路径。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1

#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 1000 + 50;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-4
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
int dp[MAXN][MAXN][2] , a[MAXN][MAXN][2] , n ;
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    //freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#endif // Online_Judge
    int x ,  r;
    bool ok;
    while(~scanf("%d" , &n))
    {
        ok = false;
        FOR(i ,0 , n)FOR(j ,0 , n)
        {
            scanf("%d" , &x);
            if(x == 0)
            {
                ok = true;
                r = i ; x = 10;
            }
            while(x % 2 == 0)
            {
                x /= 2;
                dp[i][j][0] ++;
            }
            while(x % 5 == 0)
            {
                x /= 5;
                dp[i][j][1]++;
            }
            FOR(k , 0 , 2)a[i][j][k] = dp[i][j][k];
        }
        FOR(i , 1 , n)FOR(j , 0 , 2)
        {
            dp[0][i][j] += dp[0][i - 1][j];
            dp[i][0][j] += dp[i - 1][0][j];
        }
        FOR(i , 1 , n)FOR(j , 1 , n)FOR(k , 0 ,2)
        {
            dp[i][j][k] += min(dp[i - 1][j][k] , dp[i][j - 1][k]);
        }
        int k = dp[n - 1][n - 1][1] < dp[n - 1][n - 1][0] ;
        if(ok)
        {
            if(dp[n - 1][n - 1][k] > 1)
            {
                printf("1\n");
                FORR(i , 1 , r)printf("D");
                FOR(i , 0 , n - 1)printf("R");
                FOR(i , r + 1 , n)printf("D");
                continue;
            }
        }
        printf("%d\n" , dp[n - 1][n - 1][k]);
        int i = n - 1 , j = n - 1;
        char s[MAXN * 3];
        int len = 0;
        while(i != 0||j != 0)
        {
            if(i > 0&&dp[i - 1][j][k] + a[i][j][k] == dp[i][j][k])
            {
                s[len++] = 'D';
                i--;
            }
            else
            {
                s[len++] = 'R';
                j--;
            }
        }
        REPP(i ,len - 1 , 0)printf("%c" , s[i]);
        printf("\n");
    }

    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值