当前位置: 首页 > news >正文

【动态规划】The least round way

B. The least round way
time limit per test5 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard 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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int a[1003][1003];
char path[2003];

void judge(int &x2, int &x5, int n)
{
    x2 = x5 = 0;
    while(n % 5 == 0 && n)
    {
        n /= 5;
        x5++;
    }
    while(n % 2 == 0 && n)
    {
        n /= 2;
        x2++;
    }
}

struct Node
{
    int Susake2;
    int Susake5;
    int px, py;
};

Node Susake[1003][1003], dp1[1003][1003], dp2[1003][1003];

int main(int argc, char *argv[])
{
    int n, x2, x5, Min1, Min2, k1, k2, flag, ex, ey, tx, ty, si, sj, God;
    memset(a, 0, sizeof(a));
    memset(Susake, 0, sizeof(Susake));
    memset(path, 0, sizeof(path));
    scanf("%d", &n);
    flag = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            scanf("%d", &a[i][j]);
            if(a[i][j] == 0 && flag == 0)
            {
                flag = 1;
                si = i;
                sj = j;
            }
            judge(x2, x5, a[i][j]);
            Susake[i][j].Susake2 = x2;
            Susake[i][j].Susake5 = x5;
        }
    //bround
    dp1[0][0].Susake2 = 0;
    dp1[1][0].Susake2 = 0;
    dp1[0][1].Susake2 = 0;
    dp2[0][0].Susake5 = 0;
    dp2[1][0].Susake5 = 0;
    dp2[0][1].Susake5 = 0;
    for(int i = 2; i <= n; i++)
    {
        dp1[i][0].Susake2 = 1000000009;
        dp1[0][i].Susake2 = 1000000009;
        dp2[i][0].Susake5 = 1000000009;
        dp2[0][i].Susake5 = 1000000009;
    }
    //according to 2 dp
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            dp1[i][j].Susake2 = min(dp1[i - 1][j].Susake2, dp1[i][j - 1].Susake2) + Susake[i][j].Susake2;
            //Save path
            if(dp1[i - 1][j].Susake2 < dp1[i][j - 1].Susake2)
            {
                dp1[i][j].px = i - 1;
                dp1[i][j].py = j;
            }
            if(dp1[i - 1][j].Susake2 >= dp1[i][j - 1].Susake2)
            {
                dp1[i][j].px = i;
                dp1[i][j].py = j - 1;
            }
        }
    Min1 = dp1[n][n].Susake2;
    //according to 5 dp
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            dp2[i][j].Susake5 = min(dp2[i - 1][j].Susake5, dp2[i][j - 1].Susake5) + Susake[i][j].Susake5;
            //Save path
            if(dp2[i - 1][j].Susake5 < dp2[i][j - 1].Susake5)
            {
                dp2[i][j].px = i - 1;
                dp2[i][j].py = j;
            }
            if(dp2[i - 1][j].Susake5 >= dp2[i][j - 1].Susake5)
            {
                dp2[i][j].px = i;
                dp2[i][j].py = j - 1;
            }
        }
    Min2 = dp2[n][n].Susake5;
    //print the path
    dp1[1][1].px = 1;
    dp1[1][1].py = 1;
    dp2[1][1].px = 1;
    dp2[1][1].py = 1;
    God = 0;
    if(min(Min1, Min2) > 1 && flag)
    {
        printf("%d\n", 1);
        for(int i = 2; i <= si; i++)
            printf("D");
        for(int i = 2; i <= sj; i++)
            printf("R");
        for(int i = si + 1; i <= n; i++)
            printf("D");
        for(int i = sj + 1; i <= n; i++)
            printf("R");
        printf("\n");
    }
    else
    {
        if(Min1 <= Min2)
        {
            k1 = 0;
            tx = ty = n;
            ex = tx; ey = ty;
            tx = dp1[tx][ty].px;
            ty = dp1[tx][ty].py;
            if(a[ex][ey] == 0)
                God = 1;
            if(a[tx][ty] == 0)
                God = 1;
            if(tx < ex)
            {
                k1++;
                path[k1] = 'D';
            }
            if(ty < ey)
            {
                k1++;
                path[k1] = 'R';
            }
            ex = tx; ey = ty;
            while(tx != 1 || ty != 1)
            {
                tx = dp1[ex][ey].px;
                ty = dp1[ex][ey].py;
                if(a[tx][ty] == 0)
                God = 1;
                if(tx < ex)
                {
                    k1++;
                    path[k1] = 'D';
                }
                if(ty < ey)
                {
                    k1++;
                    path[k1] = 'R';
                }
                ex = tx; ey = ty;
            }
            if(God == 1)
                printf("1\n");
            else
                printf("%d\n", min(Min1, Min2));
            for(int i = k1; i >= 1; i--)
                printf("%c", path[i]);
            printf("\n");
        }
        else
        {
            k2 = 0;
            tx = ty = n;
            ex = tx; ey = ty;
            tx = dp2[tx][ty].px;
            ty = dp2[tx][ty].py;
            if(a[ex][ey] == 0)
                God = 1;
            if(a[tx][ty] == 0)
                God = 1;
            if(tx < ex)
            {
                k2++;
                path[k2] = 'D';
            }
            if(ty < ey)
            {
                k2++;
                path[k2] = 'R';
            }
            ex = tx; ey = ty;
            while(tx != 1 || ty != 1)
            {
                tx = dp2[ex][ey].px;
                ty = dp2[ex][ey].py;
                if(a[tx][ty] == 0)
                God = 1;
                if(tx < ex)
                {
                    k2++;
                    path[k2] = 'D';
                }
                if(ty < ey)
                {
                    k2++;
                    path[k2] = 'R';
                }
                ex = tx; ey = ty;
            }
            if(God == 1)
                printf("1\n");
            else
                printf("%d\n", min(Min1, Min2));
            for(int i = k2; i >= 1; i--)
                printf("%c", path[i]);
            printf("\n");
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Susake/p/4583500.html

相关文章:

  • HTTP数据包头解析---之温故而知新!
  • hdu 1874
  • EditPlus保存时不生成bak文件(转)
  • 调试iOS App的WebView
  • java war 打包、解压命令
  • DESIR队列研究: 早期SpA患者骶髂关节放射学结构损伤的不同定义对结构损伤变化的敏感性...
  • github for windows
  • 清理DBA_DATAPUMP_JOBS中的孤立数据泵作业
  • sphinx/coreseek 精确搜索记录
  • 创建一个最简单的Linux随机启动服务
  • c++随机数
  • 交换机使用之级联和堆叠配置---学习中
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • 集成支付宝SDK时错误的解决办法
  • vim配置
  • php的引用
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • ES6语法详解(一)
  • Git 使用集
  • interface和setter,getter
  • javascript面向对象之创建对象
  • mysql 5.6 原生Online DDL解析
  • Otto开发初探——微服务依赖管理新利器
  • SwizzleMethod 黑魔法
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • webpack入门学习手记(二)
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 区块链技术特点之去中心化特性
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C++17) optional的使用
  • (二)hibernate配置管理
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (规划)24届春招和25届暑假实习路线准备规划
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (四)linux文件内容查看
  • (四)图像的%2线性拉伸
  • (转载)Linux 多线程条件变量同步
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .NET Core 通过 Ef Core 操作 Mysql
  • .net core使用ef 6
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .net和php怎么连接,php和apache之间如何连接
  • /deep/和 >>>以及 ::v-deep 三者的区别
  • @FeignClient注解,fallback和fallbackFactory
  • @Service注解让spring找到你的Service bean
  • @staticmethod和@classmethod的作用与区别
  • []error LNK2001: unresolved external symbol _m