CF 2B.The least round way

题目链接

很久以前就见过此题,以前看了题解,然后今天写了写,写的真搓。

  1 #include <cstdio>

  2 #include <cstring>

  3 #include <string>

  4 #include <cmath>

  5 #include <ctime>

  6 #include <cstdlib>

  7 #include <iostream>

  8 using namespace std;

  9 int dp2[1001][1001];

 10 int dp5[1001][1001];

 11 int p2[1001][1001];

 12 int p5[1001][1001];

 13 int p[1001][1001];

 14 int que[5001];

 15 int x,y,n,num = 0;

 16 void f1()

 17 {

 18     int a,b;

 19     a = b = n;

 20     while(1)

 21     {

 22         if(a > 1&&dp2[a][b] == dp2[a-1][b] + p2[a][b])

 23         {

 24             que[num++] = 1;

 25             a --;

 26         }

 27         else if(dp2[a][b] == dp2[a][b-1] + p2[a][b])

 28         {

 29             que[num++] = 2;

 30             b --;

 31         }

 32         if(a == 1&&b == 1) break;

 33     }

 34 

 35 }

 36 void f2()

 37 {

 38     int a,b;

 39     a = b = n;

 40     while(1)

 41     {

 42         if(a > 1&&dp5[a][b] == dp5[a-1][b] + p5[a][b])

 43         {

 44             que[num++] = 1;

 45             a --;

 46         }

 47         else if(dp5[a][b] == dp5[a][b-1] + p5[a][b])

 48         {

 49             que[num++] = 2;

 50             b --;

 51         }

 52         if(a == 1&&b == 1) break;

 53     }

 54 }

 55 void f3()

 56 {

 57     int a,b;

 58     a = b = n;

 59     while(1)

 60     {

 61         if(a > x)

 62         {

 63             que[num++] = 1;

 64             a --;

 65         }

 66         else if(b > y)

 67         {

 68             que[num++] = 2;

 69             b --;

 70         }

 71         if(a == x&&b == y) break;

 72     }

 73     while(1)

 74     {

 75         if(a > 1)

 76         {

 77             que[num++] = 1;

 78             a --;

 79         }

 80         else if(b > 1)

 81         {

 82             que[num++] = 2;

 83             b --;

 84         }

 85         if(a == 1&&b == 1) break;

 86     }

 87 }

 88 int main()

 89 {

 90     int i,j,temp,flag;

 91     scanf("%d",&n);

 92     flag = 0;

 93     for(i = 1;i <= n;i ++)

 94     {

 95         for(j = 1;j <= n;j ++)

 96         {

 97             scanf("%d",&p[i][j]);

 98             if(p[i][j] == 0)

 99             {

100                 flag = 1;

101                 x = i;

102                 y = j;

103                 continue;

104             }

105             temp = p[i][j];

106             while(temp%2 == 0)

107             {

108                 p2[i][j] ++;

109                 temp /= 2;

110             }

111             while(temp%5 == 0)

112             {

113                 p5[i][j] ++;

114                 temp /= 5;

115             }

116         }

117     }

118     for(j = 1;j <= n;j ++)

119     {

120         dp2[1][j] = dp2[1][j-1] + p2[1][j];

121         dp5[1][j] = dp5[1][j-1] + p5[1][j];

122     }

123     for(i = 1;i <= n;i ++)

124     {

125         dp2[i][1] = dp2[i-1][1] + p2[i][1];

126         dp5[i][1] = dp5[i-1][1] + p5[i][1];

127     }

128     for(i = 2;i <= n;i ++)

129     {

130         for(j = 2;j <= n;j ++)

131         {

132             dp2[i][j] = min(dp2[i-1][j],dp2[i][j-1]) + p2[i][j];

133             dp5[i][j] = min(dp5[i-1][j],dp5[i][j-1]) + p5[i][j];

134         }

135     }

136     if(flag == 0)

137     {

138         printf("%d\n",min(dp2[n][n],dp5[n][n]));

139         if(dp2[n][n] < dp5[n][n])

140         f1();

141         else

142         f2();

143     }

144     else

145     {

146         printf("%d\n",min(1,min(dp2[n][n],dp5[n][n])));

147         if(min(dp2[n][n],dp5[n][n]) >= 1)

148         f3();

149         else if(dp2[n][n] < dp5[n][n])

150         f1();

151         else

152         f2();

153     }

154     for(i = num-1;i >= 0;i --)

155     {

156         if(que[i] == 1)

157         printf("D");

158         else

159         printf("R");

160     }

161     printf("\n");

162     return 0;

163 }

你可能感兴趣的:(round)