hihocoder1241 : Best Route in a Grid
来源:互联网 发布:淘宝丫头正品商城正吗 编辑:程序博客网 时间:2024/05/16 12:54
题目链接:
传送门
题意:
给定一个n*n的矩阵,每次只能往下或者往左走,而且0的位置不能走,求最后走的格子的数的乘积末尾连续0的最小个数。
分析:
很明显最后末尾连续0的个数和素因子2和素因子5的数量有关。那么我们就做两次dp,设
Code:
#include <bits/stdc++.h>using namespace std;const int maxn = 1e3+10;int a[maxn][maxn];int dp[maxn][maxn];int cnt1[maxn][maxn];int cnt2[maxn][maxn];const int inf = 1000000;int fen(int x,int f) { int num = 0; while(x%f==0) { num++; x=x/f; } return num;}void debug() { cout<<"======================"<<endl; for(int i=0; i<=n; i++) { for(int j=0; j<=n; j++) { cout<<dp[i][j]<<" "; } cout<<endl; } cout<<"======================"<<endl;}int main() { int n; while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) scanf("%d",&a[i][j]); } memset(cnt1,0,sizeof(cnt1)); memset(cnt2,0,sizeof(cnt2)); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(!a[i][j]) { cnt1[i][j]=inf; cnt2[i][j]=inf; } else { cnt1[i][j]=fen(a[i][j],2); cnt2[i][j]=fen(a[i][j],5); } } } for(int i=0; i<=n; i++) dp[i][0]=dp[0][i]=inf; dp[1][0]=0,dp[0][1]=0; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { dp[i][j]=min(dp[i][j-1],dp[i-1][j])+cnt1[i][j]; } } int tmp = dp[n][n]; for(int i=0; i<=n; i++) dp[i][0]=dp[0][i]=inf; dp[1][0]=dp[0][1]=0; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { dp[i][j]=min(dp[i][j-1],dp[i-1][j])+cnt2[i][j]; } } printf("%d\n",min(tmp,dp[n][n])); } return 0;}/***32 5 22 5 22 5 255 5 5 2 25 5 5 2 25 5 5 2 25 5 5 2 25 5 5 2 2****/
1 0
- hihocoder1241 : Best Route in a Grid
- Best Route in a Grid
- hihocoder 1241:Best Route in a Grid
- HIHOCODE #1241:Best Route in a Grid
- hihoCoder 1241 Best Route in a Grid
- hihoCoder挑战赛15 Best Route in a Grid
- hihocoder#1241 : Best Route in a Grid(DP)
- hiho #1241 : Best Route in a Grid 【dp 求解质因子2和5的最少匹配个数】
- 8.2 Robot in a Grid
- Rails add a search function controller and route best practice
- Add a static route item in OSX
- hdoj_Choose the best route
- hdu2680Choose the best route
- HDU2680Choose the best route
- Choose the best route
- Choose the best route
- Choose the best route
- Choose the best route
- ARM Linux BenchMark
- Linux 技巧:让进程在后台可靠运行的几种方法
- http://ryewhisky.itpub.net/post/28330/298289
- jstat
- vector和string优先于动态数组的分配
- hihocoder1241 : Best Route in a Grid
- Hibernate hql 查询指定字段并获取结果集
- 黑马程序员——构造函数,构造代码块,静态代码块的区别
- 美团和大众点评合并:抱团取暖 1+1能否大于2?---OFweek
- js提交表单
- Mahout 算法解析与案例实战 笔记目录
- Centos 6.5 DNS配置
- 求第n个数的值
- 17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment