zoukankan      html  css  js  c++  java
  • #1241 : Best Route in a Grid

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下或向右走,且不能到达值为0的方格,求出一条到达右下角的最佳路径。所谓最佳路径是指途经的数的乘积的末尾连续的0最少。

    输入

    输入文件的第一行包含一个整数N,其中1≤N≤1000。

    接下来的N行每行包含N个非负整数,其中每个数小于等于1,000,000。

    数据保证至少存在一条不全为0的路径。

    输出

    输出文件仅一行,包含一个整数,表示要求的最佳路径上所有数字乘积的末尾连续零的个数。

    样例输入
    4
    1 3 0 0
    0 8 2 25
    6 5 0 3
    0 15 7 4
    样例输出
    2  
    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/3/25 19:56:57
    File Name     :hiho15a.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int b[1010][1010],c[1010][1010],a;
    int dp[1010][1010];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n;
        while(cin>>n){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    scanf("%d",&a);
                    if(a==0){
                        b[i][j]=-1;
                        c[i][j]=-1;
                        continue;
                    }
                    b[i][j]=c[i][j]=0;
                    while(a%5==0)b[i][j]++,a/=5;
                    while(a%2==0)c[i][j]++,a/=2;
                }
            }
            memset(dp,INF,sizeof dp);
            dp[1][1]=b[1][1];
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(b[i][j]==-1)continue;
                    if(i>1)dp[i][j]=min(dp[i-1][j]+b[i][j],dp[i][j]);
                    if(j>1)dp[i][j]=min(dp[i][j-1]+b[i][j],dp[i][j]);
                }
            }
            //cout<<dp[2][2]<<endl;
            int ans=dp[n][n];
            memset(dp,INF,sizeof dp);
            dp[1][1]=c[1][1];
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(c[i][j]==-1)continue;
                    if(i>1)dp[i][j]=min(dp[i-1][j]+c[i][j],dp[i][j]);
                    if(j>1)dp[i][j]=min(dp[i][j-1]+c[i][j],dp[i][j]);
                }
            }
            cout<<min(dp[n][n],ans)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Ajax实现表单验证
    JDK配置环境变量
    Java判断指定日期是星期几
    坚持不懈,直到成功
    Struts Action返回xml
    springMVC获得HttpServletRequest对象
    Ubuntu添加eclipse快捷方式
    如何给tomcat 7.0.32添加用户
    使用FusionCharts Free显示图表(JSP)
    Simulate a Windows Service using ASP.NET to run scheduled jobs
  • 原文地址:https://www.cnblogs.com/pk28/p/5320849.html
Copyright © 2011-2022 走看看