CF2B The least round way(dp+记录路径)

B. The least round way
time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard 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.

Examples
input
Copy

3
1 2 3
4 5 6
7 8 9

output
Copy

0
DDRR

题意:给你一个n*n的矩阵 只能向下或者向右 问你从左上角走到左下角 你走过路径上数字的乘积尾部0个数最少的走法

思路:我们知道只有2和5的组合可以让尾部为0 所以我们可以预处理每个数字有多少个2和5 然后很容易得出方程dp[i][j][0/1]=min(dp[i-1][j][0/1],dp[i][j-1][0/1])+a[i][j][0/1]

值得注意的是 如果其中有出现0这个数字 那么就表示 只要经过这个点 那么尾部0的个数必然是1 那么答案就只能在0或者1中产生

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<time.h>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int dp[][][]; //0表示2的个数 1表示5的个数
int a[][]; //存矩阵
bool f=;
struct node{
int x;int y;
};
node path2[][]; //记录2的个数最少时的路径
node path1[][]; //记录5的个数最少时的路径
node ans[];
int cnt=;
void output1(node x){ //记录答案
if(x.x==&&x.y==) return ;
node t; t=path1[x.x][x.y];
output1(t);
ans[++cnt]=x;
}
void output2(node x){ //记录答案
if(x.x==&&x.y==) return ;
node t; t=path2[x.x][x.y];
output2(t);
ans[++cnt]=x;
}
int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
int posx,posy;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
cin>>a[i][j];
int t=a[i][j];
if(t==){ //一但出现零我们需要标记一下
f=;
dp[i][j][]++;
dp[i][j][]++;
posx=i;
posy=j;
continue;
}
while(){ //统计2 和 5 的个数
if(t%==){
t/=;
dp[i][j][]++;
}else if(t%==){
t/=;
dp[i][j][]++;
}else{
break;
}
}
}
for(int i=;i<=n;i++){ // 预处理边界
dp[][i][]+=dp[][i-][];
dp[][i][]+=dp[][i-][];
node t; t.x=; t.y=i-;
path1[][i]=path2[][i]=t;
dp[i][][]+=dp[i-][][];
dp[i][][]+=dp[i-][][];
t.x=i-; t.y=;
path1[i][]=path2[i][]=t;
}
path1[][].x=path2[][].x=;
path1[][].y=path2[][].y=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){ //递推
if(dp[i-][j][]<dp[i][j-][]){
dp[i][j][]=dp[i-][j][]+dp[i][j][];
node temp; temp.x=i-; temp.y=j;
path1[i][j]=temp;
}else{
dp[i][j][]=dp[i][j-][]+dp[i][j][];
node temp; temp.x=i; temp.y=j-;
path1[i][j]=temp;
}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){ //递推
if(dp[i-][j][]<dp[i][j-][]){
dp[i][j][]=dp[i-][j][]+dp[i][j][];
node temp; temp.x=i-; temp.y=j;
path2[i][j]=temp;
}else{
dp[i][j][]=dp[i][j-][]+dp[i][j][];
node temp; temp.x=i; temp.y=j-;
path2[i][j]=temp;
}
}
int minn=min(dp[n][n][],dp[n][n][]);
if(minn>&&f){ //表示尾部零最小的个数就是1 那么我们就可以直接输出
cout<<""<<endl;
for(int i=;i<posx;i++)
cout<<"D";
for(int j=;j<posy;j++)
cout<<"R";
for(int i=posx;i<n;i++)
cout<<"D";
for(int j=posy;j<n;j++)
cout<<"R";
cout<<endl;
return ;
}
cout<<minn<<endl;
node e; e.x=n; e.y=n;
if(dp[n][n][]<dp[n][n][])
output1(e);
else{
output2(e);
}
for(int i=;i<=cnt;i++){
if(ans[i].x==ans[i-].x+)
cout<<"D";
else
cout<<"R";
}
cout<<endl;
return ;
}
上一篇:POJ 2111 DP+记录路径


下一篇:java基础算法之选择排序