minimal sparse ruler problem 最少尺子刻度问题
2024-09-28 22:35:44
一个长度13的尺子,如果在1位置刻点可以量出1和12,13三种刻度.那么至少刻几个点,可以直接量出1-13所有的长度,分别刻在哪几个位置?
注:必须是直接量。即在尺子上能找出一个1-13任意的整数长度。
写了个没什么技术含量的dfs暴力求解。一个可行解是 1, 2, 6, 10。
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std; class Solution {
public:
vector<vector<int>> ruler(int n, vector<int> &minPath) {
dfs(, n, minPath);
return result;
}
vector<vector<int>> result;
vector<int> path;
void dfs(int start, int n, vector<int> &minPath) {
if (start == n + ) {
if (fullScale(path)) {
result.push_back(path);
if (path.size() < minLen) {
minLen = path.size();
minPath = path;
}
}
return;
}
for (int i = start; i <= n; i++) {
path.push_back(i);
dfs(i + , n, minPath);
path.pop_back();
}
}
bool fullScale(vector<int> path) {
if (path.size() < ) {
return false;
}
unordered_map<int, int> umap;
umap[]++;
umap[]++;
for (int i = ; i < path.size(); i++) {
for (int j = ; j < i; j++) {
if (path[i] - path[j] < ) {
umap[path[i] - path[j]]++;
umap[path[j]]++;
umap[path[i]]++;
umap[ - path[i]]++;
umap[ - path[j]]++;
}
if (umap.size() >= ) {
return true;
}
}
}
return false;
}
private:
int minLen = ;
}; int main() {
int n = ;
Solution solu;
vector<int> minPath;
vector<vector<int>> res = solu.ruler(n, minPath);
for (auto x : minPath) {
cout << x << ", ";
}
}
ref: https://en.wikipedia.org/wiki/Sparse_ruler
最新文章
- HttpClient 4.5.x 工具类设计与实现
- 一个通过网络转换Ico到Png图片的小小程序(Ico2Png)
- 《30天自制操作系统》14_day_学习笔记
- python多进程共享变量Value使用tips
- fetion for linux
- 平衡搜索树(二) Rb 红黑树
- 字符串编码---hash函数的应用
- Android项目中gen文件下R文件无法生成的解决的方法
- 介绍一个成功的 Git 分支模型 Release 分支
- 请教下关于CKEditor富文本编辑框设置字体颜色的问题
- Java字符编码
- django csrftoken
- Vue框架是什么,有什么特点,怎么用
- linux 杀死进程kill 等用法
- poj2965 【枚举】
- vs 2015 编译cocos2d-x-3.9
- cmd命令关闭占用程序的端口
- UVA-10539 Almost Prime Numbers
- MySQL学习笔记:Engine存储引擎
- OA之框架的搭建