From b63a62fad18d50327b580bfd271f0594e89cf065 Mon Sep 17 00:00:00 2001 From: zhuyijun Date: Sun, 14 Nov 2021 16:37:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0bfs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- bfs/111.cpp | 61 ++++++++++++++++++++++++++++++++++ bfs/752.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++ recall/46.cpp | 1 + 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 bfs/111.cpp create mode 100644 bfs/752.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index dbbd2cb..bb2c08c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -85,6 +85,7 @@ "barrier": "cpp", "latch": "cpp", "semaphore": "cpp", - "syncstream": "cpp" + "syncstream": "cpp", + "queue": "cpp" } } \ No newline at end of file diff --git a/bfs/111.cpp b/bfs/111.cpp new file mode 100644 index 0000000..04c58f3 --- /dev/null +++ b/bfs/111.cpp @@ -0,0 +1,61 @@ +/* + * @Description: + * @Version: 1.0 + * @Autor: zhuyijun + * @Date: 2021-11-13 16:39:27 + * @LastEditTime: 2021-11-14 15:11:24 + */ +/* + +给定一个二叉树,找出其最小深度。 +最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 +说明:叶子节点是指没有子节点的节点。 +示例 1: + +输入:root = [3,9,20,null,null,15,7] +输出:2 +示例 2: + +输入:root = [2,null,3,null,4,null,5,null,6] +输出:5 +*/ +#include +using namespace std; +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) + : val(x), left(left), right(right) {} +}; +int minDepth(TreeNode *root) { + if (root == nullptr) { + return 0; + } + queue q; + q.push(*root); + int depth = 1; + while (!q.empty()) { + int sz = q.size(); + for (int i = 0; i < sz; i++) { + //获取队列第一位元素 + TreeNode cur = q.front(); + //移除队列中第一位元素 + q.pop(); + if (cur.left == nullptr && cur.right == nullptr) { + return depth; + } + if (cur.left != nullptr) { + q.push(*cur.left); + } + if (cur.right != nullptr) { + q.push(*cur.right); + } + } + depth++; + } + return depth; +} +int main() { return 0; } \ No newline at end of file diff --git a/bfs/752.cpp b/bfs/752.cpp new file mode 100644 index 0000000..27e9c47 --- /dev/null +++ b/bfs/752.cpp @@ -0,0 +1,76 @@ +/* + * @Description: + * @Version: 1.0 + * @Autor: zhuyijun + * @Date: 2021-11-14 15:33:16 + * @LastEditTime: 2021-11-14 16:30:35 + */ +#include +using namespace std; +string plusOne(string s, int j) { + if (s[j] == '9') { + s[j] = '0'; + } else { + s[j] += 1; + } + return s; +} + +string minusOne(string s, int j) { + if (s[j] == '0') { + s[j] = '9'; + } else { + s[j] -= 1; + } + return s; +} +int openLock(vector& deadends, string target) { + set deads; + deads.insert(deadends.begin(), deadends.end()); + // for (string s : deadends) { + // deads.insert(s); + // } + //记录已经穷举过得密码,防止走回头路 + set visited; + int step = 0; + queue q; + q.push("0000"); + visited.insert("0000"); + while (!q.empty()) { + int sz = q.size(); + for (int i = 0; i < sz; i++) { + string cur = q.front(); + q.pop(); + cout << cur << endl; + //判断是否达到终点 + if (deads.find(cur) != deads.end()) { + continue; + } + if (cur == target) { + return step; + } + + for (int j = 0; j < 4; j++) { + string up = plusOne(cur, j); + if (visited.find(up) == visited.end()) { + q.push(up); + visited.insert(up); + } + string down = minusOne(cur, j); + if (visited.find(down) == visited.end()) { + q.push(down); + visited.insert(down); + } + } + } + //增加步数 + step++; + } + return -1; +} +int main() { + vector deadends{"0201", "0101", "0102", "1212", "2002"}; + string s = "0202"; + cout << openLock(deadends, s); + return 0; +} \ No newline at end of file diff --git a/recall/46.cpp b/recall/46.cpp index 49708c7..2ed414d 100644 --- a/recall/46.cpp +++ b/recall/46.cpp @@ -60,6 +60,7 @@ vector> permute(vector& nums) { } //测试 + int main() { vector nums; nums.push_back(1);