hihoCoder挑战赛15 Best Route in a Grid
来源:互联网 发布:电脑软件不在桌面上 编辑:程序博客网 时间:2024/05/20 15:36
描述
给定一个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
使0最少即为乘积得到的10尽量少,那么10又是从哪来的呢?显然路径上只有2和5的数量会影响10的数量,所以只需要记录方阵中每个数有几个2和几个5即可,然后再在上方和左边取较小值即可(对于2和5分别进行2次动归)。
#include<cstdio>#include<iostream>using namespace std;#define min(a,b) (((a)<(b))?(a):(b))int map[1100][1100];int a[1100][1100],b[1100][1100];long long f[1100][1100];int n;long long ans;void read(int &x){ x=0;int f=1;char t=getchar(); while((t<48)or(t>57)){if(t=='-')f=-1;t=getchar();} while((t>=48)and(t<=57)){x=(x*10)+t-48;t=getchar();} x*=f;}int main(){ read(n); for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) { read(map[i][j]); if (map[i][j]==0) { a[i][j]=5000000; b[i][j]=5000000; continue; } while (map[i][j]%2==0) { ++a[i][j]; map[i][j]/=2; } while (map[i][j]%5==0) { ++b[i][j]; map[i][j]/=5; } } f[1][1]=a[1][1]; for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) if ((i>1)or(j>1)) { if (i==1) f[i][j]=f[i][j-1]; else if (j==1) f[i][j]=f[i-1][j]; else f[i][j]=min(f[i-1][j],f[i][j-1]); f[i][j]+=a[i][j]; } ans=f[n][n]; f[1][1]=b[1][1]; for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) if ((i>1)or(j>1)) { if (i==1) f[i][j]=f[i][j-1]; else if (j==1) f[i][j]=f[i-1][j]; else f[i][j]=min(f[i-1][j],f[i][j-1]); f[i][j]+=b[i][j]; } cout<<min(ans,f[n][n]); return 0;}
0 0
- hihoCoder挑战赛15 Best Route in a Grid
- hihocoder 1241:Best Route in a Grid
- hihoCoder 1241 Best Route in a Grid
- hihocoder#1241 : Best Route in a Grid(DP)
- Best Route in a Grid
- hihocoder1241 : Best Route in a Grid
- HIHOCODE #1241:Best Route in a Grid
- hiho #1241 : Best Route in a Grid 【dp 求解质因子2和5的最少匹配个数】
- hihoCoder挑战赛14 A 不等式
- hihoCoder挑战赛A题——顺子
- hihoCoder挑战赛19 A Rikka with Sequence
- hihoCoder挑战赛14A题 1223
- Hihocoder 挑战赛
- hihocoder 挑战赛13A 组合数学中的帕斯卡公式
- hihoCoder挑战赛16 A—— 王胖浩与三角形
- hihoCoder挑战赛17 A String Problem I 题库 1260
- hiho-hihoCoder挑战赛29-A-序列的值
- hihoCoder挑战赛14-1223
- Rails读书笔记第四章
- 数据库框架FMDB的使用
- Android之图片缓存管理
- Javah Error android.app.Activity not found的问题
- java中反射的用法
- hihoCoder挑战赛15 Best Route in a Grid
- J2EE Web项目Tomcat跑不起来的一些问题解决过程汇总
- Vmware中安装ubuntu无法通过NAT上网
- Smarty模板学习入门教程
- react要点解析
- 设计模式--迪米特法则
- 内存管理
- Best Time to Buy and Sell Stock
- 关于图片拉伸的技巧