codeforce 2B

B. The least round way
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard 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
 
        
题解:对2的个数,5的个数Dp,取最小的,有0特判
#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
int a[1005][1005],dp1[1005][1005],dp2[1005][1005],n;
void dfs(int dp[1005][1005],int x,int y)//回溯记路径 ,从最后一个点开始,要么左要么上 
{
	if(x==1&&y==1)
	return;
	int x1=x-1,y1=y-1;
	if(x1>=1&&x1<=n&&y1>=1&&y1<=n)
	{
		if(dp[x1][y]>dp[x][y1])
		{
			dfs(dp,x,y1);
			printf("R");
		}
		else
		{
			dfs(dp,x1,y);
			printf("D");
		}
	}
	else if(x1<1||x1>n)
	{
		dfs(dp,x,y1);
		printf("R");
	}
    else if(y1<1||y1>n)
    {
    	dfs(dp,x1,y);
    	printf("D");
	}
} 
int main()
{
	int i,j,x,y,biao;
	while(~scanf("%d",&n))
	{
		biao=0;
		memset(dp1,0,sizeof(dp1));
		memset(dp2,0,sizeof(dp2));
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			scanf("%d",&a[i][j]);
			if(a[i][j]==0)
			{
				x=i;
				y=j;
				biao=1;
				dp1[i][j]=1;
				dp2[i][j]=1;
			}
			else
			{
			int t=a[i][j];
			while(a[i][j]%5==0)
			{
				dp1[i][j]++;
				a[i][j]/=5;
			}
			while(t%2==0)
			{
				dp2[i][j]++;
				t/=2;
			}
			}
		}
	}
	for(i=2;i<=n;i++)
	{
		dp1[1][i]=dp1[1][i-1]+dp1[1][i];
		dp1[i][1]=dp1[i-1][1]+dp1[i][1];
		dp2[1][i]=dp2[1][i-1]+dp2[1][i];
		dp2[i][1]=dp2[i-1][1]+dp2[i][1];
	}
	for(i=2;i<=n;i++)
	{
		for(j=2;j<=n;j++)                                                                                                                                 
		dp1[i][j]+=min(dp1[i-1][j],dp1[i][j-1]);
	}
	for(i=2;i<=n;i++)
	{
		for(j=2;j<=n;j++)                                                                                                                                
		dp2[i][j]+=min(dp2[i-1][j],dp2[i][j-1]);	
	}
	if(biao==1)
	{
		if(min(dp1[n][n],dp2[n][n])>1)
		{
			printf("1\n");
			for(i=1;i<y;i++)
			printf("R");
			for(i=2;i<=n;i++)
			printf("D");
			for(i=y+1;i<=n;i++)
			printf("R");
			printf("\n");
		}
		else
		{
			if(dp1[n][n]<=1)
			{
	        printf("%d\n",dp1[n][n]);
	       dfs(dp1,n,n);
			}
			else
			{
	     printf("%d\n",dp2[n][n]);
	      dfs(dp2,n,n);
			}
		}
	}
	else
	{
    if(dp1[n][n]<dp2[n][n])
    {
    printf("%d\n",dp1[n][n]);
	dfs(dp1,n,n);
	}
	else
	{
	printf("%d\n",dp2[n][n]);
	dfs(dp2,n,n);
	}
	printf("\n");
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值