
bfs
xuanweiace
一个热爱算法竞赛的弱校ACMer路过。青大本,浙大硕,方向后端开发,菜鸡一枚,奋斗ing...
展开
-
LeetCode 397. 整数替换
题目大意:给定一个正整数n ,你可以做如下操作:如果n是偶数,则用n / 2替换n 。如果n是奇数,则可以用n + 1或n - 1替换n 。n变为 1 所需的最小替换次数是多少?链接:https://leetcode-cn.com/problems/integer-replacement示例 1:输入:n = 8输出:3解释:8 -> 4 -> 2 -> 1示例 2:输入:n = 7输出:4解释:7 -> 8 -> 4 ...原创 2021-11-19 17:29:01 · 348 阅读 · 0 评论 -
【牛客 - 369C】小A与欧拉路(bfs树的直径)
题干:链接:https://ac.nowcoder.com/acm/contest/369/C来源:牛客网小A给你了一棵树,对于这棵树上的每一条边,你都可以将它复制任意(可以为0)次(即在这条边连接的两个点之间再加一条边权相同的边),求所有可能新形成的图中欧拉路的最短长度欧拉路:从图中任意一个点开始到图中任意一个点结束的路径,并且图中每条边只通过恰好一次输入描述:第一行...原创 2019-03-03 16:05:07 · 334 阅读 · 0 评论 -
【牛客 - 188C】水图(bfs树的直径,思维)
题干:链接:https://ac.nowcoder.com/acm/contest/188/C来源:牛客网小w不会离散数学,所以她van的图论游戏是送分的小w有一张n个点n-1条边的无向联通图,每个点编号为1~n,每条边都有一个长度小w现在在点x上她想知道从点x出发经过每个点至少一次,最少需要走多少路输入描述:第一行两个整数 n,x,代表点数,和小w所处的位置第二...原创 2019-03-03 16:07:12 · 379 阅读 · 0 评论 -
【HRBUST - 1613】迷宫问题 (bfs)
题干:小z身处在一个迷宫中,小z每分钟可以走到上下左右四个方向的相邻格之一。迷宫中有一些墙和障碍物。同时迷宫中也有一些传送门,当小z走到任意一个传送门时,可以选择传送到其他任意的传送门(传送是不花费时间的),当然也可以停留在原地。现在小z想知道走出迷宫需要花费的最少时间。Input输入第一行为组数T(t<=10)。对于每组数据第一行为两个整数R和C(1<=R,C<...原创 2019-03-06 15:04:24 · 435 阅读 · 0 评论 -
【CodeForces - 920E】Connected Components? (dsu,补图连通块,STLset+map,bfs 或bitset)
题干:You are given an undirected graph consisting ofnvertices andedges. Instead of giving you the edges that exist in the graph, we give youmunordered pairs (x, y) such that there is no edge bet...原创 2019-04-08 10:51:51 · 580 阅读 · 0 评论 -
【CodeForces - 190E】Counter Attack (补图bfs,卡常,dsu)
题干:无向图中给定n个顶点,m条不存在的边(除了这m条边,其余都存在),求图的连通分量,及每个连通分量的大小。解题报告:https://codeforces.com/blog/entry/4556AC代码:#include<cstdio>#include<iostream>#include<algorithm>#include&l...原创 2019-04-08 19:41:16 · 385 阅读 · 0 评论 -
【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图)
题干:The 44th World Finals of the International Collegiate Programming Contest (ICPC 2020) will be held in Moscow, Russia. To celebrate this annual event for the best competitive programmers around th...原创 2019-04-27 18:54:28 · 1278 阅读 · 0 评论 -
【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图)
题干:The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilizati...原创 2019-05-04 16:14:00 · 254 阅读 · 0 评论 -
*【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)
题干:Leo has a grid withNrows andMcolumns. All cells are painted with either black or white initially.Two cellsAandBare calledconnectedif they share an edge and they are in the same color,...原创 2019-04-24 20:18:27 · 260 阅读 · 0 评论 -
【HDU - 5706】GirlCat(bfs)
题干:As a cute girl, Kotori likes playing ``Hide and Seek'' with cats particularly.Under the influence of Kotori, many girls and cats are playing ``Hide and Seek'' together.Koroti shots a photo. T...原创 2019-05-06 11:36:24 · 433 阅读 · 0 评论 -
【CodeForces - 616C】The Labyrinth(bfs,并查集,STLset)
题干:求每个*能够到达的格子数量,只有.可以走(四个方向扩展),结果mod 10,替换 * 后输出。InputThe first line contains two integersn, m(1 ≤ n, m ≤ 1000) — the number of rows and columns in the field.Each of the nextnlines contai...原创 2019-08-05 20:10:26 · 293 阅读 · 0 评论 -
【POJ - 3126】Prime Path(bfs)
题干:给你两个四位的素数a,b。a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变。请你计算a最少经过多少次上述变换才能变成b。例如:1033 -> 81791033173337333739377987798179最少变换了6次。Input第一行输入整数T,表示样例数。 (T <= 100)每个样例输入两个四位...原创 2019-06-03 21:11:14 · 286 阅读 · 0 评论 -
【HDU - 4784】Dinner Coming Soon(记忆化搜索bfs,dp)
题干: Coach Pang loves his boyfriend Uncle Yang very much. Today is Uncle Yang’s birthday, Coach Pang wants to have a romantic candlelit dinner at Uncle Yang’s house and he has to arrive there in T m...原创 2019-07-10 22:22:15 · 264 阅读 · 0 评论 -
【HDU 4394】Digital Square(bfs,数位搜索,思维,数学)
题干:Given an integer N,you should come up with the minimumnonnegativeinteger M.M meets the follow condition: M2%10x=N (x=0,1,2,3....)InputThe first line has an integer T( T< = 1000), the n...原创 2019-07-07 10:48:28 · 244 阅读 · 0 评论 -
【HDU - 5876】Sparse Graph(补图bfs,STLset)
题干:In graph theory, thecomplementcomplementof a graphGGis a graphHHon the same vertices such that two distinct vertices ofHHare adjacent if and only if they arenotnotadjacent inGG.Now yo...原创 2019-08-30 20:36:39 · 397 阅读 · 0 评论 -
【CodeForces - 129C】Statues(思维,bfs)
题干:In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 × 8): Anna is in the upper right corner, and Maria is in the lo...原创 2019-10-09 20:25:56 · 237 阅读 · 0 评论 -
【ZOJ - 4020 】Traffic Light (bfs,分层图)
题干:n*m矩阵a.若a[i][j]==1则可以往左右走,若a[i][j]==0 则可以往上下走.每一秒可以按上述规则移动,并且每秒钟矩阵所有的值翻转。n*m<=1e5.问从(sx,sy)到(tx,ty)的最短时间.解题报告: 这题因为不带权值所以不需要考虑Dijkstra,可以根据时间直接判断状态是0还是1。如果状态的变换很复杂的话就考虑分层图就好了。AC代码:...原创 2019-03-03 15:38:38 · 339 阅读 · 0 评论 -
【CodeForces - 558C】Amr and Chemistry(位运算,bfs,计数,思维,tricks)
题干:Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.Amr hasndifferent types of chemicals. Each chemicalihas an initial volume ofailiters....原创 2019-03-02 21:23:55 · 420 阅读 · 0 评论 -
【牛客 - 330C】Applese 走迷宫(bfs)
题干: 精通程序设计的 Applese 双写了一个游戏。在这个游戏中,它被困在了一个 n×mn×m 的迷宫中,它想要逃出这个迷宫。在迷宫中,有一些方格是水池,只有当 Applese 处于水属性的时候才可以通过;有一些方格是岩浆,只有当 Applese 是火属性的时候可以通过;有一些方格是墙壁,无论如何都无法通过;另一些格子是空地(包括起点和终点),可以自由通过。 在一些空地上有神...原创 2019-01-29 22:00:43 · 250 阅读 · 0 评论 -
【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)
题干:An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This p...原创 2018-08-02 15:32:16 · 491 阅读 · 0 评论 -
【HDU - 5094】 Maze (状态压缩+bfs)
题干:This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS. The capta...原创 2018-08-14 21:34:35 · 341 阅读 · 0 评论 -
*【HDU - 1242 】 Rescue (反向dfs,或bfs)
题干:Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends wa...原创 2018-08-29 21:18:41 · 343 阅读 · 0 评论 -
【HRBUST - 1621】迷宫问题II (bfs)
题干:小z身处在一个迷宫中,小z每分钟可以走到上下左右四个方向的相邻格之一。迷宫中有一些墙和障碍物。同时迷宫中也有一些怪兽,当小z碰到任意一个怪兽时,小z需要将怪兽消灭掉才可以离开此方格。但消灭怪兽会花费一定的时间。现在小z想知道走出迷宫需要花费的最少时间。Input输入第一行为组数T(T<=10)。对于每组数据第一行为两个整数R和C(1<=R,C<=200)...原创 2019-05-09 21:57:23 · 364 阅读 · 0 评论 -
【HDU - 2102】A计划 (麻烦一点的bfs)
题干:可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,...原创 2018-09-02 21:20:05 · 167 阅读 · 0 评论 -
【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)
题干:The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the pro...原创 2018-09-12 00:28:39 · 273 阅读 · 0 评论 -
【POJ - 2631】Roads in the North (树的直径,模板)
题干:Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village th...原创 2018-09-15 17:56:51 · 270 阅读 · 0 评论 -
【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)
题干:Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ ...原创 2018-09-12 15:03:09 · 699 阅读 · 0 评论 -
*【CodeForces - 122C 】Lucky Sum (bfs记录状态,二分查找,有坑)(或分块)
题干:Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky ...原创 2018-10-03 16:23:44 · 324 阅读 · 0 评论 -
【HDU - 2444】The Accomodation of Students(二分图判断 + 匈牙利算法求最大匹配)
题干:There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each o...原创 2018-10-25 22:53:16 · 284 阅读 · 0 评论 -
【HDU - 1254 】推箱子 (双bfs)
题干:推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动. 现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格. ...原创 2018-09-27 19:33:34 · 567 阅读 · 1 评论 -
【牛客 - 181D】小叶的巡查(树的直径,数学)
题干:8102年,牛客系列竞赛空前繁荣。为了更好地管理竞赛,小叶决定巡查于各大城市之间,体察民情。所以,从一个城市马不停蹄地到另一个城市成了小叶最常做的事情。小叶有一个钱袋,用于存放往来城市间的路费。这个国家有一套优秀的交通方案,使得任何一个大城市都能从首都直接或者通过其他大城市间接到达。同时,如果不重复经过大城市,从首都到达每个大城市的方案都是唯一的。如果不在某个城市停下来修整,在连...原创 2018-10-10 21:24:34 · 302 阅读 · 0 评论 -
*【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐打游戏(bfs,双元bfs,思维)
题干: 小乐乐觉得学习太简单了,剩下那么多的时间好无聊,于是便想打游戏。 最近新出了一个特别火的游戏,叫吃猪,小乐乐准备玩一玩。 吃猪游戏很简单,给定一个地图,大小为n*m,在地图中会随机出现一个火山口,只要小乐乐能逃离这个地图,他便能吃猪! 但吃鸡远没有那么简单: 1.小乐乐每走一次只能上下左右四个方向中走一步。 ...原创 2018-12-02 22:19:17 · 361 阅读 · 0 评论 -
【Uva - 10047 】The Monocycle(搜索,bfs记录状态)
题干:Uva的题目就不粘贴题干了,,直接上题意吧。有你一个独轮车,车轮有5种颜色,为了5中颜色的相对位置是确定的。有两种操作:1.滚动:轮子必须沿着顺时针方向滚动,每滚动一次会到达另一个格子,着地的颜色会改变(顺时针转)。例如当前是绿色着地下一次就是黑色,依次是红蓝白。2.转动:就是改变了轮子的方向(不改变颜色),转动每次只能选择左转90度或者右转90度,即不能掉头。车子每向前走一格(滚动...原创 2018-12-08 10:14:38 · 225 阅读 · 0 评论 -
【CodeForces - 520B】Two Buttons (bfs或dp或时光倒流,trick)
题干:Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multipli...原创 2018-12-26 15:12:06 · 320 阅读 · 0 评论 -
【CF#505B】Mr. Kitayuta's Colorful Graph (并查集或Floyd或BFS)
题干:Mr. Kitayuta has just bought an undirected graph consisting ofnvertices andmedges. The vertices of the graph are numbered from 1 ton. Each edge, namely edgei, has a colorci, connecting ver...原创 2019-03-25 21:57:42 · 258 阅读 · 0 评论