![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
搜索算法
文章平均质量分 54
tomjobs
别慌,慌也没用
展开
-
LeetCode 124. 二叉树中的最大路径和(递归)
路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。路径和 是路径中各节点值的总和。给你一个二叉树的根节点 root ,返回其 最大路径和 。思路:递归的时候记录从这个点往下得到路径的最大权值和;那么遍历每一个点,找到其左右叶子向下遍历能得到的最大路径和,加上该点点权,得到的就是包含子树经过中经过该点的最大路径和。需要注意的一点是点权可能是负的。class Solution {原创 2021-03-13 17:20:31 · 272 阅读 · 0 评论 -
Codeforces-1494 D. Dogeforces(递归,建树)
The Dogeforces company has ???? employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The h原创 2021-03-12 14:48:54 · 280 阅读 · 0 评论 -
LeetCode 542. 01 矩阵(多源BFS,DP)
给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。两个相邻元素间的距离为 1 。示例 1:输入:[[0,0,0],[0,1,0],[0,0,0]]输出:[[0,0,0],[0,1,0],[0,0,0]]示例 2:输入:[[0,0,0],[0,1,0],[1,1,1]]输出:[[0,0,0],[0,1,0],[1,2,1]]提示:给定矩阵的元素个数不超过 10000。给定矩阵中至少有一个元素是 0。矩阵中的元素只在四个方向上相邻: 上、下、左、原创 2021-03-11 15:02:15 · 187 阅读 · 0 评论 -
2021年度训练联盟热身训练赛第一场 C. New Maths(爆搜)
题意:给你一个结果xxx,求一个最小整数yyy通过与自己进行不进位乘法得到结果xxx。思路:假设xxx长度为nnn,则可以得到yyy的长度为(n+1)/2(n+1)/2(n+1)/2(要求n+1n+1n+1是偶数)。假设已经确定了最低的kkk位,那么最终结果的最低kkk位也确定了。直到确定了全部位,就可以确定最终结果的所有位。直接爆搜经过这样的剪枝可以剪掉很多,复杂度不会太高。#include<cstdio>#include<cstring>#include<.原创 2021-03-10 23:51:11 · 168 阅读 · 0 评论 -
LeetCode 987. 二叉树的垂序遍历(dfs)
思路:直接dfs遍历一遍。因为同层的要按值排序,所以存的时候同时存值大小以及所在层次。先按层次排序,再按值排序。class Solution {public: map<int,vector<pair<int,int>>>ans; void dfs(TreeNode* root,int flag,int dep) { if(root == nullptr) return; ans[flag].push_back({d.原创 2021-03-08 23:48:05 · 148 阅读 · 0 评论 -
LeetCode 22. 括号生成(递归)
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。示例 1:输入:n = 3输出:["((()))","(()())","(())()","()(())","()()()"]示例 2:输入:n = 1输出:["()"]思路:递归一下,每个位置枚举放左括号或者右括号。只要满足任意前缀中左括号数目大于等于右括号数目,最终序列中左括号数目等于右括号数目则说明该序列合法。class Solution {public: void dfs(vect原创 2021-02-01 16:28:50 · 201 阅读 · 0 评论 -
K - L Textiles Gym - 102911L(递归,L方块铺满)
The Philippines is home to more than 175 ethnolinguistic groups, with many indigenous peoples still carrying on their heritage through traditions which are hundreds of years old. Among these traditions is the weaving of fabric and textiles adorned with int原创 2021-01-27 17:19:06 · 408 阅读 · 0 评论 -
AcWing 169. 数独2(复杂的搜索+剪枝)
思路:可以看出来,这就是数独加强版,9∗99*99∗9变成了16∗1616*1616∗16。对应的算法效率要求也更高。一开始想在9∗99*99∗9的代码上加剪枝水过去,搜索功力还是不足过不去。。。参考了《进阶指南》的思路和代码。思路就是搜索的时候遍历每个点,如果这个点不能选数了,那么剪掉,如果只能选一个数,那就选上。(这里以及下面所有“点”都是针对空格点)遍历每一行,如果这一行没数可选那就剪掉,如果只能选一个数那就选上。列和每个4*4小块同理。最后再按照常规思路,遍历每个格子选出可选数最少的格.原创 2021-01-22 17:14:40 · 347 阅读 · 0 评论 -
LeetCode 37. 解数独(搜索+剪枝)
思路:用数组x,y,zx,y,zx,y,z记录每一行,每一列,和每一个3∗33*33∗3小块里面0~9的使用情况。使用状态,0代表使用了,1代表没有使用。然后搜索的时候遍历每一个格子,找到里面可选数字最少的那一个(也就是其行、列、小块的数字使用情况状态的与值中1最少的那个。直到搜索到每个格子都被填完class Solution {public: char s[105]; int x[105],y[105],z[105]; int X[105],Y[105],Z[105.原创 2021-01-21 21:16:50 · 322 阅读 · 0 评论 -
AcWing 225. 矩阵幂求和(递归)
思路:用朴素的递归就可以解决。如果kkk是偶数,那么f(A,k)=A1+A2+A3+...+Ak=A1+A2+...+Ak2+Ak2(A1+A2+A3...+Ak2)f(A,k)=A^1+A^2+A^3+...+A^k=A^1+A^2+...+A^{\frac{k}{2}}+A^{\frac{k}{2}}(A^1+A^2+A^3...+A^{\frac{k}{2}})f(A,k)=A1+A2+A3+...+Ak=A1+A2+...+A2k+A2k(A1+A2+A3...+A2k)=>=.原创 2021-01-19 21:55:03 · 316 阅读 · 0 评论 -
LeetCode 407. 接雨水 II(优先队列+BFS,类Dijkstra)
给你一个 m x n 的矩阵,其中的值均为非负整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。示例:给出如下 3x6 的高度图:[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]返回 4 。如上图所示,这是下雨前的高度图[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] 的状态。下雨后,雨水将会被存储在这些方块中。总的接雨水量是4。提示:1 <= m, n <= 110原创 2021-01-10 22:19:09 · 262 阅读 · 0 评论 -
LeetCode 440. 字典序的第K小数字(递归,十叉树)
字典序的第K小数字给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字。注意:1 ≤ k ≤ n ≤ 109。示例 :输入:n: 13 k: 2输出:10解释:字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],所以第二小的数字是 10。思路:gjx说这是字节面试题,感觉有点意思。写过各种求第k大数(第k个排列,第k个错排排列,通过数位DP求有奇怪限制的第k个二进制串。。。)本题则是字典序第k大。要求字典序第k.原创 2020-11-25 22:25:06 · 417 阅读 · 0 评论 -
HDU1427 24点问题(DFS)
题意:算24点,可以加括号思路:补一下之前的基础东西#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>#include <vector>#include <queue>using namespace std;const int maxn = 2e3 + 7;char s[10];int a[10];int an原创 2020-11-10 15:51:06 · 342 阅读 · 0 评论 -
2020小米网络赛第一场 Walking Machine(BFS)
题意:每个点有一个方向值,代表这个点出发只能走这个方向。求多少个点出发可以走到棋盘外。思路:经典题了吧,直接从外边界bfs进来看能遍历到多少个点。当然遍历的时候我们要记得把每个点的方向值反一下。#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef.原创 2020-11-09 10:20:54 · 142 阅读 · 0 评论 -
Round F 2020 - Kick Start 2020 Painters‘ Duel(博弈搜索)
ProblemA new art museum is about to open! It is a single-story building in the shape of a large equilateral triangle. That triangle is made up of many smaller identical equilateral-triangle-shaped rooms, and the side length of the museum is S times the si原创 2020-09-27 18:22:39 · 682 阅读 · 0 评论 -
Codeforces383 B. Volcanoes(难题,大矩形能否走到终点)
Iahub got lost in a very big desert. The desert can be represented as a n × n square matrix, where each cell is a zone of the desert. The cell (i, j) represents the cell at row i and column j (1 ≤ i, j ≤ n). Iahub can go from one cell (i, j) only down or r原创 2020-09-24 16:43:03 · 194 阅读 · 0 评论 -
Codeforces1401 D. Maximum Distributed Tree(DFS)
You are given a tree that consists of ???? nodes. You should label each of its ????−1 edges with an integer in such way that satisfies the following conditions:each integer must be greater than 0;the product of all ????−1 numbers should be equal to ????;原创 2020-09-09 19:17:49 · 196 阅读 · 1 评论 -
Codeforces11 C. How Many Squares?(dfs,模拟)
You are given a 0-1 rectangular matrix. What is the number of squares in it? A square is a solid square frame (border) with linewidth equal to 1. A square should be at least 2 × 2. We are only interested in two types of squares:squares with each side para原创 2020-09-02 12:12:58 · 1182 阅读 · 0 评论 -
Codeforces1388 D. Captain Flint and Treasure(贪心)
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure’s location or may not. That’s why captain Flint decided to leave the solving the problem to his crew and offered an abs原创 2020-08-17 00:44:27 · 241 阅读 · 0 评论 -
牛客多校第九场 The Flee Plan of Groundhog(dfs)
题意:A在点1,B在点n。A先一直往B走t秒,每秒走1m,一条边1m。t秒后A可以任意走,且B速度为2m/s。A先走一秒B再走一秒,问最迟多久A可以被B抓住。思路:只需要考虑A到一个点的时间和B到一个点的时间,只要在这个点A早于B,那就可以算出A被抓住的时间了。d2[i]∗2<=d3[i]d2[i] * 2 <= d3[i]d2[i]∗2<=d3[i]也就是要满足,d2为A离i的距离,d3为B离的距离。#include <cstdio>#include <c原创 2020-08-13 10:38:37 · 151 阅读 · 0 评论 -
2020杭电多校第二场 Lead of Wisdom(爆搜)
Problem DescriptionIn an online game, “Lead of Wisdom” is a place where the lucky player can randomly get powerful items.There are k types of items, a player can wear at most one item for each type. For the i-th item, it has four attributes ai,bi,ci and原创 2020-07-25 20:17:41 · 437 阅读 · 0 评论 -
牛客算法周周练15 璀璨光滑(BFS,二进制)
思路:一开始看成了只有连边才有一个二进制位不同,这样的话题解对12 31 22 33 4的输出就成了0 1 1 1,就不对了。后来知道,意思是只要有二进制位不同,就能连边。这就好办了,我们肯定能确定f[1]=0f[1]=0f[1]=0,那么连边的两个点,二进制中1的数目相差为1。那么与1最短距离为1的点,二进制位有1个1,最短距离为2的点,二进制位有2个1。我们对这个过程跑bfs,最后就能得到一组解。但是要求字典序最小。可以得到,对所有数的二进制位进行交换,仍然为解。则我们对二进制位.原创 2020-07-17 23:15:49 · 230 阅读 · 0 评论 -
Fancy Antiques Gym - 101002A(搜索+剪枝)
题意:有nnn个物品,mmm个商店,最多可以去kkk个商店。给出每个物品在哪些商店可以买,求买完nnn个物品的最小花费。思路:参考claris的博客:https://www.cnblogs.com/clrs97/p/6130145.html维护minv[i][j]minv[i][j]minv[i][j]代表在大于等于iii编号的商店中买jjj物品的最小花费。搜索时候的话就是遍历每一个商店枚举买还是不买(回溯法),维护pre[i]pre[i]pre[i]数组代表去过的商店中买jjj物品的最小花费。.原创 2020-07-09 21:42:09 · 274 阅读 · 0 评论 -
I - Ivan and the swimming pool Gym - 102448I(二分+bfs)
After another great year for UFPE’s competitive programming team, the coach Ivan decided to gift the team and invest in building a pool at an area nearby. After reuniting with the group to arrange the details, four things were decided:The pool must have s原创 2020-06-29 14:23:02 · 516 阅读 · 0 评论 -
K - Birdwatching GYM102501(dfs)
题意: 给定一个特定点,求有多少个点能到达该特定点,且只能通过与该点直接相连的点到达思路:对于所有与特定点直接相连的点设为集和S,如果存在点 a∈S,使得a能到达b∈S,则a不是所求点。我们去掉与特定点直接相连的边,其他边建立反图,再对集和S中的点跑dfs,判断有多少个点能到达自己。如果S中的点遍历到的点的个数大于二(除了自己还有S中其他点),则说明还有其他边到达特定点。#include <cstdio>#include <cstring>#include <al.原创 2020-06-14 13:58:50 · 500 阅读 · 0 评论 -
ICPC NEAU Programming Contest 2020 L. 城市排水(优先队列bfs)
思路: 实际上是用已经确定水位高度的点去更新还没有确定高度的点。一开始有坑的点的水位高度就是方块高度。每次取当前最小水位高度的点去更新其他点,就可以保证每个点只需要更新一次。本题中边界无限高,所以不用管边界的影响(更新完就是无穷大)。假设没有边界或者边界高度为0,那么同理可以看作已经确定边界水位高度了,把边界入堆即可。#include <cstdio>#include <cstring>#include <vector>#include <algorit.原创 2020-06-12 11:20:20 · 216 阅读 · 0 评论 -
Nastya and Time Machine CodeForces - 1341F(dfs构造)
Having come to Nastya, Denis discovered that she was not happy to see him… But the young man has the last hope. He wants to buy all things that Nastya likes. Then she will certainly agree to talk to h...原创 2020-04-26 22:11:15 · 523 阅读 · 0 评论 -
CF1341E. Nastya and Unexpected Guest(01bfs)
If the girl doesn’t go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.On the way from Denis’s house to the girl’s house is a roa...原创 2020-04-26 18:04:01 · 633 阅读 · 0 评论 -
H - Hyacinth Gym - 101482H(dfs)
As a new employee at the Northwestern Europe Rout- ing Company (NWERC), you do a lot of thinking about wireless network architectures. Lately you learned about a multi-channel mesh network architectur...原创 2020-04-19 23:54:52 · 323 阅读 · 0 评论 -
2019湖南省赛 Numbers(dfs)
NumbersBobo has nnn distinct integers a1,a2,…,ana_1, a_2, \dots, a_na1,a2,…,an in [0,99][0, 99][0,99]. He writes them in decimal notation without leading zeros in a row, obtaning a string sss.Giv...原创 2020-03-03 21:34:19 · 439 阅读 · 1 评论 -
Connected Components? CodeForces - 920E(求补图连通块)
You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between ...原创 2020-02-13 14:15:36 · 333 阅读 · 0 评论 -
ACWING175. 电路维修(双端队列bfs)
达达是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女翰翰,从而被收留在地球上。翰翰的家里有一辆飞行车。有一天飞行车的电路板突然出现了故障,导致无法启动。电路板的整体结构是一个R行C列的网格(R,C≤500),如下图所示。电路.png每个格点都是电线的接点,每个格子都包含一个电子元件。电子元件的主要部分是一个可旋转的、连接一条对角线上的两个接点的短电缆。在旋转之后,它...原创 2019-11-26 18:52:33 · 182 阅读 · 0 评论 -
ACWING 190. 字串变换(双向bfs)
已知有两个字串 A, B 及一组字串变换的规则(至多6个规则):A1 -> B1A2 -> B2…规则的含义为:在 A 中的子串 A1 可以变换为 B1、A2 可以变换为 B2 …。例如:A=’abcd’ B=’xyz’变换规则为:‘abc’->‘xu’ ‘ud’->‘y’ ‘y’->‘yz’则此时,A 可以经过一系列的变换变为 B,其变换的过程为:...原创 2019-11-26 00:12:19 · 248 阅读 · 0 评论 -
C. League of Leesins cf1255c (拓扑排序,搜索)
Bob is an avid fan of the video game “League of Leesins”, and today he celebrates as the League of Leesins World Championship comes to an end!The tournament consisted of ???? (????≥5) teams around the worl...原创 2019-11-20 13:11:44 · 536 阅读 · 0 评论 -
ACWING170. 加成序列 poj2248(迭代加深搜索)
满足如下条件的序列X(序列中元素被标号为1、2、3…m)被称为“加成序列”:1、X[1]=12、X[m]=n3、X[1]<X[2]<…<X[m-1]<X[m]4、对于每个 k(2≤k≤m)都存在两个整数 i 和 j (1≤i,j≤k−1,i 和 j 可相等),使得X[k]=X[i]+X[j]。你的任务是:给定一个整数n,找出符合上述条件的长度m最小的“加成序列”。...原创 2019-11-10 23:52:27 · 217 阅读 · 0 评论 -
ACWING172. 立体推箱子 poj3322(bfs巨麻烦)
立体推箱子是一个风靡世界的小游戏。游戏地图是一个N行M列的矩阵,每个位置可能是硬地(用”.”表示)、易碎地面(用”E”表示)、禁地(用”#”表示)、起点(用”X”表示)或终点(用”O”表示)。你的任务是操作一个1×1×2的长方体。这个长方体在地面上有两种放置形式,“立”在地面上(1×1的面接触地面)或者“躺”在地面上(1×2的面接触地面)。在每一步操作中,可以按上下左右四个键之一。按下按...原创 2019-11-10 17:02:05 · 312 阅读 · 0 评论 -
ACWING194. 涂满它!poj4007(IDA星,dfs)
Flood-it是谷歌+平台上的非常好玩的一款游戏,游戏界面如下所示:flood.png在游戏开始时,系统将随机生成N×N的方形区域,并且区域内的每个网格都被涂成了六种颜色中的一种。玩家从左上角开始游戏。在每个步骤中,玩家选择一种颜色并将与左上角连通的所有格子(包括左上角)都变成该种颜色。这里连通定义为:两个格子有公共边,并且颜色相同。通过这种方式,玩家可以从左上角开始将所有格子都变为...原创 2019-11-10 00:28:58 · 289 阅读 · 0 评论 -
ACWING173. 矩阵距离 BZOJ2225(多起点bfs)
给定一个N行M列的01矩阵A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为:dist(A[i][j],A[k][l])=|i−k|+|j−l|输出一个N行M列的整数矩阵B,其中:B[i][j]=min1≤x≤N,1≤y≤M,A[x][y]=1dist(A[i][j],A[x][y])输入格式第一行两个整数n,m。接下来一个N行M列的01矩阵,数字之间没有空格。输出格式...原创 2019-11-08 20:20:27 · 226 阅读 · 0 评论 -
ACWING168. 生日蛋糕(剪枝dfs)
月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体。设从下往上数第i层蛋糕是半径为Ri, 高度为Hi的圆柱。当i < M时,要求Ri > Ri+1且Hi > Hi+1。由于要在蛋糕上抹奶油,为尽可能节约经费,我们希望蛋糕外表面(最下一层的下底面除外)的面积Q最小。令Q = Sπ ,请编程对给出的N和M,找出蛋糕的制作方案(适...原创 2019-11-08 16:31:15 · 259 阅读 · 2 评论 -
ACWING167. 木棒(剪枝+dfs)POJ1011
乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位。然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度。请你设计一个程序,帮助乔治计算木棒的可能最小长度。每一节木棍的长度都用大于零的整数表示。输入格式输入包含多组数据,每组数据包括两行。第一行是一个不超过64的整数,表示砍断之后共有多少节木棍。第二行是截断以后,所得到...原创 2019-11-05 20:48:36 · 304 阅读 · 0 评论