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
3
1 2 3
4 5 6
7 8 9
output
0
DDRR
思路:利用dp思想,寻找2最少与5最少,最后也需要考虑特殊情况当数字为0时, 输入时记得用scanf输入。
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
const int MAX = 1010;
int n;
struct data{
int value;
int num_2;
int num_5;
char last_path[2]; //0 : "2_path" 1: "5_path"
}data[MAX][MAX];
int buffer_dp[MAX][MAX]={
0
};
int count(int number,int base){
int count = 0;
if(number==0)
return count;
while(number%base==0){
count++;
number=number/base;
}
return count;
}
void path_2(){
buffer_dp[0][0] = data[0][0].num_2;
for(int j = 1;j<n;++j){
buffer_dp[0][j] = buffer_dp[0][j-1] + data[0][j].num_2;
data[0][j].last_path[0] = 'R';
}
for(int i = 1;i<n;++i){
buffer_dp[i][0] = buffer_dp[i-1][0] + data[i][0].num_2;
data[i][0].last_path[0] = 'D';
}
for(int i=1;i<n;++i){
for(int j = 1;j<n;++j){
if(buffer_dp[i-1][j]<buffer_dp[i][j-1]){
buffer_dp[i][j] = buffer_dp[i-1][j] + data[i][j].num_2;
data[i][j].last_path[0] = 'D';
}
else{
buffer_dp[i][j] = buffer_dp[i][j-1] + data[i][j].num_2;
data[i][j].last_path[0] = 'R';
}
}
}
}
void path_5(){
buffer_dp[0][0] = data[0][0].num_5;
for(int j = 1;j<n;++j){
buffer_dp[0][j] = buffer_dp[0][j-1] + data[0][j].num_5;
data[0][j].last_path[1] = 'R';
}
for(int i = 1;i<n;++i){
buffer_dp[i][0] = buffer_dp[i-1][0] + data[i][0].num_5;
data[i][0].last_path[1] = 'D';
}
for(int i=1;i<n;++i){
for(int j = 1;j<n;++j){
if(buffer_dp[i-1][j]<buffer_dp[i][j-1]){
buffer_dp[i][j] = buffer_dp[i-1][j] + data[i][j].num_5;
data[i][j].last_path[1] = 'D';
}
else{
buffer_dp[i][j] = buffer_dp[i][j-1] + data[i][j].num_5;
data[i][j].last_path[1] = 'R';
}
}
}
}
void Print(int i,int j,int index){
if( i==0 && j==0 ){
return ;
}
char direction = data[i][j].last_path[index];
if( direction == 'D')
Print(i-1,j,index);
else
Print(i,j-1,index);
cout<<direction;
}
void Print_zero(int zero_x,int zero_y){
for(int i = 0;i<zero_x;++i)
cout<<"D";
for(int j = 0;j<zero_y;++j)
cout<<"R";
for(int i = zero_x+1 ;i<n;++i)
cout<<"D";
for(int j = zero_y+1 ;j<n;++j)
cout<<"R";
}
int main(){
bool zero = false;
int zero_x ,zero_y;
int value;
cin>>n;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
scanf("%d",&value);
data[i][j].value = value;
if( value == 0){
zero = true;
zero_x = i;
zero_y = j;
}
data[i][j].value = value;
data[i][j].num_2 = count(value,2);
data[i][j].num_5 = count(value,5);
}
}
path_2();
int min_2 = buffer_dp[n-1][n-1];
if(min_2==0){
cout<<min_2<<endl,Print(n-1,n-1,0);
cout<<endl;
return 0;
}
path_5();
int min_5 = buffer_dp[n-1][n-1];
if(min_5==0){
cout<<min_5<<endl,Print(n-1,n-1,1);
cout<<endl;
return 0;
}
if(!zero)
min_2 < min_5?(cout<<min_2<<endl,Print(n-1,n-1,0)):(cout<<min_5<<endl,Print(n-1,n-1,1));
else{
if(1<=min(min_2,min_5))
cout<<1<<endl,Print_zero(zero_x,zero_y);
else
min_2 < min_5?(cout<<min_2<<endl,Print(n-1,n-1,0)):(cout<<min_5<<endl,Print(n-1,n-1,1));
}
cout<<endl;
return 0;
}