题目链接:hihocoder 1241

题意:

n*n的格阵,每个方格内有一个数字.蚂蚁从左上角走到右下角,数字是零的方格不能走,只能向右向下走.蚂蚁走的路径上全部方格的的乘积为s,要使s低位0的个数尽量少.问,最少s的末尾包含几个0.

分析:

10=2*5,所以只要统计蚂蚁路径上2的个数和5的个数,二者之中的较小者即为s末尾0的个数.

假设2的个数为x,5的个数为y.

对于路径(x,y),答案是min(x,y).

"路径(p,q)比路径(x,y)好"的充要条件"min(p,q)<min(x,y)".

最优路径(x,y)中x为最小值或者y为最小值.

这个问题可以进行延伸:将路径上数字的乘积用k进制来表示,使得末尾0的个数尽量少.对k进行因子分解,当k是若干个质数的乘积时,k=p1*p2*p3*p4...,对于每一个pi进行一次动归.当上边的主键等于左边时,就应该比较剩余的值,有点不好整了,还是两个质数之积比较简单.当k=p1^m1*p2^m2......时,又该怎么做呢?

代码:

 #include<iostream>
 #include<stdio.h>
 #include<algorithm>
 #include<queue>
 #include<math.h>
 #include<string.h>
 #include<stdlib.h>
 using namespace std;
 typedef long long ll;
 typedef unsigned long long ull;
 #define re(i,n) ;i<n;i++)
 int n;
 ;
 int a[maxn][maxn];
 int two[maxn][maxn], five[maxn][maxn];
 int s[maxn][maxn][];
 void go(int c[maxn][maxn], int x){
     for (int i = 1; i <= n; i++){
         for (int j = 1; j <= n; j++){
             if (a[i][j] == 0){
                 c[i][j] = 1e6; continue;
             }
             int cnt = ;
             ; k /= x)cnt++;
             c[i][j] = cnt;
         }
     }
 }
 void work(int c[maxn][maxn], int cc[maxn][maxn]){
     re(i, n + 1)s[0][i][0] = s[0][i][1] = s[i][0][0] = s[i][0][1] = 1e6;
     s[0][1][0] = s[0][1][1] = s[1][0][0] = s[1][0][1] = 0;
     for (int i = 1; i <= n; i++){
         for (int j = 1; j <= n; j++){
             if (s[i - 1][j][0] == s[i][j - 1][0]){
                 s[i][j][0] = c[i][j] + s[i][j - 1][0];
                 s[i][j][1] = cc[i][j]+min(s[i][j - 1][1], s[i - 1][j][1]);
             }
             ][j][]< s[i][j - ][]){
                 s[i][j][0] = s[i - 1][j][0] + c[i][j];
                 s[i][j][1] = s[i - 1][j][1] + cc[i][j];
             }
             else{
                 s[i][j][0] = s[i][j-1][0] + c[i][j];
                 s[i][j][1] = s[i][j-1][1] + cc[i][j];
             }
         }
     }
 }
 int main(){
     freopen("in.txt", "r", stdin);
     cin >> n;
     re(i, n)re(j, n)scanf("%d", &a[i + 1][j + 1]);
     go(two, 2), go(five, 5);
     work(two, five); int p = min(s[n][n][0], s[n][n][1]);
     work(five, two); int q = min(s[n][n][0], s[n][n][1]);
     cout << min(p, q) << endl;
     return 0;
 }

hihocoder1241 Best Route in a Grid的更多相关文章

  1. hihocoder 1241:Best Route in a Grid

    #1241 : Best Route in a Grid 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下 ...

  2. #1241 : Best Route in a Grid

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下或向右走,且不能到达值为0的方格,求出一条到达右下角的最佳 ...

  3. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  4. [4]Telerik Grid 简单使用方法

    1.columns <% Html.Telerik().Grid(Model) .Name("Orders") .Columns(columns => { //绑定列名 ...

  5. Oracle Grid control 11g及Active DataGuard 11g安装部署

    Oracle Grid control 11g及Active DataGuard 11g安装部署(一) 原贴 http://blog.csdn.net/lichangzai/article/detai ...

  6. MatterTrack Route Of Network Traffic :: Matter

    Python 1.1 基础 while语句 字符串边缘填充 列出文件夹中的指定文件类型 All Combinations For A List Of Objects Apply Operations ...

  7. ExtJS 4.2 Grid组件的单元格合并

    ExtJS 4.2 Grid组件本身并没有提供单元格合并功能,需要自己实现这个功能. 目录 1. 原理 2. 多列合并 3. 代码与在线演示 1. 原理 1.1 HTML代码分析 首先创建一个Grid ...

  8. WPF中Grid实现网格,表格样式通用类

    /// <summary> /// 给Grid添加边框线 /// </summary> /// <param name="grid"></ ...

  9. Application Request Route实现IIS Server Farms集群负载详解

    序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一样,时不时的汹涌而至,让我们防不胜防.那么后台的高可用,以及服务器的处理能力就要做一个横向扩展的方案,以使后台业务持续的稳定 ...

随机推荐

  1. Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

    从微软官网下载jdbc驱动包sqljdbc4,运行sqljdbc_4.0.2206.100_chs.exe,将驱动包解压到了Microsoft JDBC Driver 4.0 for SQL Serv ...

  2. spring mvc 详细配置(转)

    转自: http://www.cnblogs.com/superjt/p/3309255.html 现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是 ...

  3. PHP&MySQL(二)——困也得啃书

    madan,所有事情都敢赶在一起...以后每天中午去学车啊,好开心..晚上好困,但是困也得啃书........ 二.PHP脚本编程语言 什么变量啊,数据类型啊,特别特别基本的不记录了,说点容易忽略的. ...

  4. ExecutorType 的类型

  5. Unity 5 WebGL vs Web Player

    起原 Unity5.3中看到Web Player未来将到被取消,根据Unity官方blog中称Unity5.4中将会移除web player. 本文从我知道的知识比较一下webPlayer和WebGL ...

  6. Unity3D粒子系统 合集

    http://www.cnblogs.com/qinyuanpei/p/3659513.htmlhttp://www.cnblogs.com/qinghuaideren/p/3597666.html

  7. OpenSessionInview

    Open Session In View模式的主要思想是:在用户的每一次请求过程始终保持一个Session对象打开着 实现步骤: 步骤一.创建一个Web项目,创建包cn.happy.util,创建Hi ...

  8. CSS语法

    在CSS中,通过id选择元素和JavaScript一样:#idname.也可以在最前面加上元素名,防止id相同的冲突.

  9. js checkbox 选中判断

    var isSelect = ""; isSelect = $("#tblImgList" + " input[type='checkbox']&qu ...

  10. ASP.NET中处理自定义错误的最佳方式

    要在ASP.NET中处理好自定义错误(Custom Errors)首先要抛弃使用web.config\customErrors. <customErrors mode="RemoteO ...