非常恶心的dp题,测试数据也恶心,坑多,特别是0的坑

#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define fi(i, a, b) for (int i = a; i <= b; ++i)
#define fr(i, a, b) for (int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int, int>;
//#define DEBUG
int s[1005][1005];
int dp[1005][1005][2];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int flag = 0;
int fx, fy;
fi(i, 1, n) fi(j, 1, n)
{
cin >> s[i][j];
if (s[i][j] == 0)
{
if (!flag)
{
flag = 1;
fx = i;
fy = j;
}
continue;
}
while (s[i][j] % 2 == 0)
{
s[i][j] /= 2;
dp[i][j][0]++;
}
while (s[i][j] % 5 == 0)
{
s[i][j] /= 5;
dp[i][j][1]++;
}
}
fi(i, 1, n) fi(j, 1, n)
{
if (i == 1 && j == 1)
continue;
int p, q, r, ss, res1, res2;
p = q = r = ss = 0x3f3f3f3f;
if (s[i - 1][j] != 0)
{
p = dp[i][j][0] + dp[i - 1][j][0];
q = dp[i][j][1] + dp[i - 1][j][1];
}
if (s[i][j - 1] != 0)
{
r = dp[i][j][1] + dp[i][j - 1][1];
ss = dp[i][j][0] + dp[i][j - 1][0];
}
res1 = min(p, ss);
res2 = min(q, r);
if (res1 == 0x3f3f3f3f && res2 == 0x3f3f3f3f)
continue;
dp[i][j][0] = res1;
dp[i][j][1] = res2;
}
int count = 0;
// fi(i, 1, n) fi(j, 1, n)
// {
// count++;
// cout << dp[i][j][0] << " " << dp[i][j][1] << " " << count << endl;
// }
string ans = "";
int res = dp[n][n][0] > dp[n][n][1] ? 1 : 0;
int i = n, j = n;
while (1)
{
if (dp[i - 1][j][res] <= dp[i][j - 1][res])
{
if (s[i - 1][j] != 0)
{
i--;
ans += "D";
}
else if (s[i][j - 1] != 0)
{
j--;
ans += "R";
}
}
else if (dp[i - 1][j][res] >= dp[i][j - 1][res])
{
if (s[i][j - 1] != 0)
{
j--;
ans += "R";
}
else if (s[i - 1][j] != 0)
{
i--;
ans += "D";
}
}
if (s[i - 1][j] == 0 && s[i][j - 1] == 0)
break;
//走到第一行或第一列
if (i == 1)
{
for (int x = 1; x < j; x++)
ans += "R";
break;
}
if (j == 1)
{
for (int x = 1; x < i; x++)
ans += "D";
break;
}
}
reverse(ans.begin(), ans.end());
// fi(i, 1, n) fi(j, 1, n)
// {
// cout << dp[i][j][0] << " " << dp[i][j][1] << endl;
// }
if (ans.size() < 2 * n - 2)
{
cout << 1 << endl;
string temp = "";
fi(i, 1, fx - 1) temp += 'D';
fi(i, 1, fy - 1) temp += 'R';
fi(i, fx, n - 1)
{
temp += 'R';
}
fi(j, fy, n - 1)
{
temp += 'D';
}
cout << temp << endl;
return 0;
}
if (flag)
{
if (1 <= min(dp[n][n][0], dp[n][n][1]))
{
cout << 1 << endl;
string temp = "";
fi(i, 1, fx - 1) temp += 'D';
fi(i, 1, fy - 1) temp += 'R';
fi(i, fx, n - 1)
{
temp += 'R';
}
fi(j, fy, n - 1)
{
temp += 'D';
}
cout << temp << endl;
return 0;
}
else
{
cout << 0 << endl;
cout << ans << endl;
return 0;
}
}
else
{
cout << min(dp[n][n][0], dp[n][n][1]) << endl;
cout << ans << endl;
}
#ifdef DEBUG
//freopen(D:\in.txt,r,stdin);
#endif
return 0;
}

CF2B的更多相关文章

  1. CF2B The least round way(贪心+动规)

    题目 CF2B The least round way 做法 后面\(0\)的个数,\(2\)和\(5\)是\(10\)分解质因数 则把方格中的每个数分解成\(2\)和\(5\),对\(2\)和\(5 ...

  2. CF2B The least round way(dp+记录路径)

    B. The least round way time limit per test 2 seconds memory limit per test 64 megabytes input standa ...

  3. CF2B The least round way 题解

    都是泪呀...↑ 题目传送门 题意(直接复制了QWQ) 题目描述 给定由非负整数组成的\(n \times n\)的正方形矩阵,你需要寻找一条路径: 以左上角为起点, 每次只能向右或向下走, 以右下角 ...

  4. CF2B The least round way

    [题解] 可以发现10的因数除了1和10之外只有2和5了,那么走过的路径上各个数字的2的因数个数之和.5的因数个数之和中较小的一个即是答案.这样的话DP即可.同时需要注意有0的情况,有0的时候有一个答 ...

  5. [CF2B] The least round way - dp

    给定由非负整数组成的n×n 的正方形矩阵,你需要寻找一条路径: 以左上角为起点 每次只能向右或向下走 以右下角为终点 并且,如果我们把沿路遇到的数进行相乘,积应当是最小"round" ...

  6. C# 操作的时候接收用户输入密码进行确认

    首先新建一个原始窗体,如下:

  7. dp入门30题

    前言:本文章主要记录一些 \(dp\) 入门题,都是我做过的,希望读者能从这些基础题中打好 \(dp\) 扎实的基础,有不足的地方也欢迎指出.大部分是 \(CodeFoces\) 和 \(Atcode ...

随机推荐

  1. leaflet叠加图片图层

    <!DOCTYPE html> <html> <head> <title>Layers Control Tutorial - Leaflet</t ...

  2. 网络安全—部署CA证书服务器

    文章目录 网络拓扑 安装步骤 安装证书系统 安装从属证书服务器 申请与颁发 申请证书 CA颁发证书 使用windows Server 2003环境 网络拓扑 两台服务器在同一网段即可,即能够互相pin ...

  3. 用 C 语言开发一门编程语言 — 交互式解释器

    目录 文章目录 目录 前言 环境 编译型 vs 解释型 实现交互式解释器 使用 GNU Readline 函数库 前言 通过开发一门类 Lisp 的编程语言来理解编程语言的设计思想,本实践来自著名的& ...

  4. ef.core 事务不回滚的我遇到的一种情况分享

    比如有几个Repository:_storeRep,_inventoryRep,_storeItemRep.基类封装有BeginTransaction():   using(var trans=_st ...

  5. 鸿蒙HarmonyOS实战-Stage模型(进程模型)

    前言 进程是计算机中运行的程序的实例.它是操作系统对正在执行的程序的一种抽象概念.每个进程都有自己的独立内存空间.运行状态和执行上下文.进程可以包含一个或多个线程,每个线程可以独立执行一部分任务.操作 ...

  6. Python爬图片(面向对象版)

    import requests from lxml import etree from threading import Thread class Spider(object): def __init ...

  7. Newtonsoft.Json 首字母小写

    // 首字母小写 JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); jsonSerialize ...

  8. Json.NET Converting between JSON and XML

    Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter. Elements, attrib ...

  9. LOTO示波器软件新增导览功能

    新版本的大部分型号LOTO示波器的上位机软件我们改成了导航工具条方式.原来的方式是把所有功能都显示在不同的标签页中,这样的优点是非常快捷方便,基本上用鼠标一两次点击就能直达想要的功能设置.但是缺点是不 ...

  10. docker lnmp配置

    1.lnmp网络与目录规划 172.16.10.0/24 nginx:172.16.10.10 mysql:172.16.10.20 php:172.16.10.30 网站访问主目录:/wwwroot ...