【题目描述】
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.
【输入】
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).
【输出】
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
【样例输入】
3
1 2 3
4 5 6
7 8 9
【样例输出】
0
DDRR
题目链接:https://codeforces.com/contest/2/problem/B
代码如下:
#include <bits/stdc++.h>
using namespace std;
static const int MAXN=1000;
int m[MAXN+10][MAXN+10][2],f[MAXN+10][MAXN+10];
bool vis[MAXN+10][MAXN+10];
int main()
{
int n,x;
cin>>n;
int ii=0,jj=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>x;
if(x==0)
{
ii=i; jj=j; continue;
}
while(x%2==0)
{
x/=2;
m[i][j][0]++;
}
while(x%5==0)
{
x/=5;
m[i][j][1]++;
}
}
stack<bool> st;
int ans=0x3f3f3f3f;
for(int k=0;k<=1;k++)
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==1 && j==1) f[i][j]=m[i][j][k];
if(i==1) f[i][j]=f[i][j-1]+m[i][j][k];
else if(j==1)
{
f[i][j]=f[i-1][j]+m[i][j][k];
vis[i][j]=true;
}
else if(f[i-1][j]<f[i][j-1])
{
f[i][j]=f[i-1][j]+m[i][j][k];
vis[i][j]=true;
}
else f[i][j]=f[i][j-1]+m[i][j][k];
}
if(f[n][n]<ans)
{
while(!st.empty()) st.pop();
ans=f[n][n];
int cnt=0,i=n,j=n;
while(i+j>2)
{
cnt++;
st.push(vis[i][j]);
if(vis[i][j]) i--;
else j--;
}
}
}
if(ans && ii)
{
cout<<1<<endl;
for(int i=1;i<ii;i++) cout<<"D";
for(int i=1;i<jj;i++) cout<<"R";
for(int i=ii;i<n;i++) cout<<"D";
for(int i=jj;i<n;i++) cout<<"R";
cout<<endl;
return 0;
}
cout<<ans<<endl;
while(!st.empty())
{
bool temp=st.top();
st.pop();
if(temp) cout<<"D";
else cout<<"R";
}
cout<<endl;
return 0;
}