1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include<bits/stdc++.h>
using namespace std; inline int read(){ int res=0,f=1;char ch=getchar(); while(ch<'0'ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar(); return res*f; } int n,a[1010][1010],f[2][1010][1010],dp[2][1010][1010]; int ans,qx,qy; bool ff; inline int get2(register int x,register int y){ if(a[x][y]==0){return 0;} register int pt=0; while(a[x][y]%2==0) ++pt,a[x][y]/=2; return pt; } inline int get5(register int x,register int y){ if(a[x][y]==0){return 0;} register int pt=0; while(a[x][y]%5==0) ++pt,a[x][y]/=5; return pt; } inline void print(register int k,register int x,register int y,register int first){ if(x==1&&y==1) ; else if(x==1) print(k,x,y-1,0); else if(y==1) print(k,x-1,y,1); else if(dp[k][x][y]==dp[k][x-1][y]+f[k][x][y]) print(k,x-1,y,1); else print(k,x,y-1,0); if(first==6666) return ; putchar(first==0?'R':'D'); return ; } signed main(){ n=read(); ff=0;qx=0;qy=0; for(register int i=1;i<=n;i++){ for(register int j=1;j<=n;j++){ a[i][j]=read(); if(a[i][j]==0){ qx=i;qy=j; ff=1; } } } if(a[1][1]==0a[n][n]==0){ puts("1"); for(int i=1;i<=n-1;i++) putchar('D'); for(int i=1;i<=n-1;i++) putchar('R'); return 0; } for(register int i=1;i<=n;i++){ for(register int j=1;j<=n;j++){ f[0][i][j]=get2(i,j); f[1][i][j]=get5(i,j); } } memset(dp,63,sizeof(dp)); for(register int i=1;i<=n;i++) for(register int j=1;j<=n;j++){ dp[0][i][j]=min(dp[0][i][j],dp[0][i-1][j]); dp[0][i][j]=min(dp[0][i][j],dp[0][i][j-1]); if(i==1&&j==1) dp[0][i][j]=0; dp[0][i][j]+=f[0][i][j]; } for(register int i=1;i<=n;i++) for(register int j=1;j<=n;j++){ dp[1][i][j]=min(dp[1][i][j],dp[1][i-1][j]); dp[1][i][j]=min(dp[1][i][j],dp[1][i][j-1]); if(i==1&&j==1) dp[1][i][j]=0; dp[1][i][j]+=f[1][i][j]; } ans=min(dp[0][n][n],dp[1][n][n]); if(ans>1&&ff==1){ putchar('1'); putchar('\n'); for(register int i=1;i<qx;i++) putchar('D'); for(register int i=1;i<qy;i++) putchar('R'); for(register int i=qx;i<n;i++) putchar('D'); for(register int i=qy;i<n;i++) putchar('R'); putchar('\n'); }else{ cout<<ans; putchar('\n'); if(dp[0][n][n]<dp[1][n][n]) print(0,n,n,6666); else print(1,n,n,6666); putchar('\n'); } return 0; }
|