蒟蒻求hack错在#27
查看原帖
蒟蒻求hack错在#27
78393
Error_502楼主2020/11/1 12:04
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1000 + 10;
const int inf = 1 << 30;

int f[maxn][maxn][2];
char nxt[maxn][maxn];

int calc(int x,int k)
{
	int ret = 0;
	while(!(x % k) && x)
	{
		x /= k;
		ret++;
	}
	return ret;
}

void print(int x,int y)
{
	if(x == 1 && y == 1) return;
	if(nxt[x][y] == 'U') print(x-1,y);
	else print(x,y-1);
	printf("%c",nxt[x][y] == 'U' ? 'D' : 'R');
}

int main()
{
	int n,num,pos = 0;
	scanf("%d",&n);
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
		{
			scanf("%d",&num);
			if(num == 0)
			{
				pos = i;
				f[i][j][0] = f[i][j][1] = 1;
				continue;
			}
			f[i][j][0] = calc(num,2);
			f[i][j][1] = calc(num,5);
		}
	for(int i = 2;i <= n;i++)
		f[i][0][0] = f[0][i][0] = f[i][0][1] = f[0][i][1] = inf;
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			for(int x = 0;x < 2;x++)
				if(f[i-1][j][x] < f[i][j-1][x])
					f[i][j][x] += f[i-1][j][x],nxt[i][j] = 'U';
				else
					f[i][j][x] += f[i][j-1][x],nxt[i][j] = 'L';
	int ans = min(f[n][n][1],f[n][n][0]);
	if(pos && ans > 1)
	{
		printf("1\n");
		for(int i = 1;i < pos;i++)
			printf("D");
		for(int i = 1;i < n;i++)
			printf("R");
		for(int i = pos;i < n;i++)
			printf("D");
		return 0;
	}
	printf("%d\n",ans);
	print(n,n);
	return 0;
}
2020/11/1 12:04
加载中...