CodeForces - 2B The least round way

B. The least round way
time limit per test2 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard 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
3
1 2 3
4 5 6
7 8 9
Output
0
DDRR
题意:
给你一个n*n的方阵,求左上角到右下角路径上所有数字相乘末尾0最少的路径;
思路:
分别dp一下方阵中二和五(不是"中二")的幂最小的路径,输出较小的那条路径;
dp和输出路径都不难,就是当方阵含零的时候需要注意一下,在dp时可以把0当10来计算,然后在输入的时候把0所在行数用变量ll记录下来,并将标记flag=1(flag初始为0),在dp完成后进行比较,如果路径2和5的幂
中较小的比一大,就输出经过0的那条路径,否则正常输出;
哎,就是0这wrong了多次啊,虽然想到了这个情况,但采取的方法是错的,看了人家的博客,才知道处理方法;
#include<iostream>
using namespace std;
const int  maxn = 1005;
int two[maxn][maxn], five[maxn][ maxn];
int data1[maxn][maxn];
int tft[maxn][maxn];
int tff[maxn][maxn];
int flag1[maxn][maxn];
int flag2[maxn][maxn];
int max1(int a, int b,int i,int j)
{
 if (a < b)
 {
  flag1[i][j] = 2;
  return a;
 }
 else
  flag1[i][j] = 1;
 return b;
}
int max2(int a, int b, int i, int j)
{
 if (a < b)
 {
  flag2[i][j] = 2;
  return a;
 }
 else
  flag2[i][j] = 1;
 return b;
}
void fivemi(int i, int j, int n)
{
 int cnt = 0;
 if (n == 0)
 {
  n=10;
 }
 while (n%5==0)
 {
  if (n % 5 == 0)cnt++;
  n /= 5;
 }
 five[i][j] = cnt;
}
void twomi(int i, int j, int n)
{
 int cnt = 0;
 if (n == 0)
 {
  n = 10;
 }
 while (n % 2 == 0)
 {
  if (n % 2 == 0)cnt++;
  n /= 2;
 }
 two[i][j] = cnt;
}
int main()
{
 int flag = 0;
 int n;
 scanf("%d", &n);
 int i;
 int j;
 int ll;
 for(i=1;i<=n;i++)
  for (j = 1; j <= n; j++)
  {
   scanf("%d",&data1[i][j]);
   if (data1[i][j] == 0)
   {
    flag = 1;
    ll = i;
   }
   twomi(i, j, data1[i][j]);
   fivemi(i, j, data1[i][j]);
  }
 tft[0][1] = 0;
 tft[1][0] = 0;
 tff[0][1] = 0;
 tff[1][0] = 0;
 for (i = 1; i <= n; i++)
 {
  tft[1][i] = tft[1][i - 1] + two[1][i];
  flag1[1][i] = 1;
 }
 for (i = 2; i <= n; i++)
 {
  tft[i][1] = tft[i - 1][1] + two[i][1];
  flag1[i][1] = 2;
 }
 for (i = 1; i <= n; i++)
 {
  tff[1][i] = tff[1][i - 1] + five[1][i];
  flag2[1][i] = 1;
 }
 for (i = 2; i <= n; i++)
 {
  tff[i][1] = tff[i - 1][1] + five[i][1];
  flag2[i][1] = 2;
 }

 for(i=2;i<=n;i++)
  for (j = 2; j <=n; j++)
  {
   tft[i][j] = max1(tft[i - 1][j] + two[i][j], tft[i][j - 1]+ two[i][j],i,j);
   tff[i][j]= max2(tff[i - 1][j] + five[i][j], tff[i][j - 1] + five[i][j], i, j);
  }
 char a[2010];
 if (flag==1)
 {
  if (tft[n][n] >= 1 && tff[n][n] >= 1)
  {
   cout << 1 << endl;
   for (i = 1; i < ll; i++)
    cout << "D";
   for (i = 1; i <n; i++)
    cout << "R";
   for (i = ll; i < n; i++)
    cout << "D";
  }
  else
  {
   if (tft[n][n] < tff[n][n])
   {
    int q = 2 * (n - 1) - 1;
    int p = q;
    i = j = n;
    while (i != 1 || j != 1)
    {
     if (flag1[i][j] == 2)
     {
      a[q] = 'D';
      q--;
      i--;
     }
     else
     {
      a[q] = 'R';
      q--;
      j--;
     }
    }
    cout << tft[n][n] << endl;
    for (i = 0; i <= p; i++)
     cout << a[i];
    cout << endl;
   }
   else
   {
    int q = 2 * (n - 1) - 1;
    int p = q;
    i = j = n;
    while (i != 1 || j != 1)
    {

     if (flag2[i][j] == 2)
     {
      a[q] = 'D';
      q--;
      i--;
     }
     else
     {
      a[q] = 'R';
      q--;
      j--;
     }
    }
    cout << tff[n][n] << endl;
    for (i = 0; i <= p; i++)
     cout << a[i];
    cout << endl;
   }
  }
 }
 else
 {
  if (tft[n][n] < tff[n][n])
  {
   int q = 2 * (n - 1) - 1;
   int p = q;
   i = j = n;
   while (i != 1 || j != 1)
   {
    if (flag1[i][j] == 2)
    {
     a[q] = 'D';
     q--;
     i--;
    }
    else
    {
     a[q] = 'R';
     q--;
     j--;
    }
   }
   cout << tft[n][n] << endl;
   for (i = 0; i <= p; i++)
    cout << a[i];
   cout << endl;
  }
  else
  {
   int q = 2 * (n - 1) - 1;
   int p = q;
   i = j = n;
   while (i != 1 || j != 1)
   {

    if (flag2[i][j] == 2)
    {
     a[q] = 'D';
     q--;
     i--;
    }
    else
    {
     a[q] = 'R';
     q--;
     j--;
    }
   }
   cout << tff[n][n] << endl;
   for (i = 0; i <= p; i++)
    cout << a[i];
   cout << endl;
  }
 }
 return 0;
}

posted @ 2017-05-23 20:08  DNoSay  阅读(224)  评论(0编辑  收藏  举报