
BFS
csu_xiji
这个作者很懒,什么都没留下…
展开
-
力扣 847. 访问所有节点的最短路径 bfs+状压
https://leetcode-cn.com/problems/shortest-path-visiting-all-nodes/思路:考虑用二进制表示走过的点集(状态压缩),那么可以用dis[i][j]dis[i][j]dis[i][j]表示在状态为iii的情况下到jjj点的最短路径,那么我们期望的答案就是最小的dis[2n−1][...]dis[2^n-1][...]dis[2n−1][...]。依然可以使用bfsbfsbfs来求解最短路,只不过状态是两个维度的。也可以认为这是刷表法的dpdpdp原创 2021-08-06 21:36:32 · 293 阅读 · 0 评论 -
力扣 LCP 07. 传递信息 bfs/dfs/dp
https://leetcode-cn.com/problems/chuan-di-xin-xi/思路一:无脑遍历,dfs/bfs均可,时间复杂度O(nk)O(n^k)O(nk)。class Solution {public: int numWays(int n, vector<vector<int>>& relation, int k) { vector<vector<int>> vec(n,vector<int原创 2021-07-02 21:56:40 · 158 阅读 · 0 评论 -
力扣 815. 公交路线 多源多汇bfs 思维
https://leetcode-cn.com/problems/bus-routes/思路一:首先看数据范围,暴力连边的话肯定是不行滴,因为车站的数量可能达到10610^6106。考虑转换一下思路,把一条公交路线抽象为一个点,那么任意两条有公共点的公交线路之间有一条边权为1的边,这样我们可以得到一张图,在这个图上做bfsbfsbfs即可,注意此时是多源多汇的bfs。class Solution {public: int numBusesToDestination(vector<vec原创 2021-06-29 12:47:17 · 285 阅读 · 0 评论 -
力扣 909. 蛇梯棋 bfs
https://leetcode-cn.com/problems/snakes-and-ladders/思路:思路很简单,就是bfsbfsbfs,但是这题目翻译的……挺离谱的。class Solution {public: vector<int> get2Pos(int n,int pos) { --pos; int tmp=pos/n; pos%=n; if(tmp&1)原创 2021-06-27 01:06:23 · 131 阅读 · 0 评论 -
力扣 773. 滑动谜题 bfs \ A*
https://leetcode-cn.com/problems/sliding-puzzle/思路:本质上还是bfs……但是数组不好计算哈希,我们可以把它转换为字符串,即按照从左到右、从上到下的顺序连接起来。这样交换操作和哈希操作都比较容易执行。class Solution {public: string getStr(const vector<vector<int>>& board) { string s; for原创 2021-06-26 01:32:33 · 253 阅读 · 0 评论 -
力扣 752. 打开转盘锁 bfs\启发式搜索(A*)
https://leetcode-cn.com/problems/open-the-lock/添加链接描述思路:显然题目是另外一种类型的最短路问题,最优解可用bfsbfsbfs解决。class Solution {public: int openLock(vector<string>& deadends, string target) { unordered_map<string,bool> vis; for(const str原创 2021-06-25 21:18:10 · 609 阅读 · 0 评论 -
力扣 132. 分割回文串 II dp bfs/dfs 剪枝
https://leetcode-cn.com/problems/palindrome-partitioning-ii/思路一:先O(n2)O(n^2)O(n2)预处理出dpdpdp数组,若s[i…j]s[i…j]s[i…j]是回文串,则dpij=1dp_{ij}=1dpij=1。我们可以把这个数组当作一个邻接矩阵,即把dpij=1dp_{ij}=1dpij=1抽象为iii到jjj的一条边,最终可以得到一个图,那么答案就等于从000开始到n−1n-1n−1的最短路径,直接bfsbfsbfs即可。c原创 2021-03-08 23:49:08 · 164 阅读 · 0 评论 -
PIPIOJ 1470: 中等迷宫问题 bfs
http://pipioj.online/problem.php?id=1470思路:stepxykstep_{xyk}stepxyk表示从起点SSS出发到(x,y)(x,y)(x,y)还能使用kkk次遁地时所需要的最少步数,bfsbfsbfs保证按照最少次数扩展,简单搜索题。#include<bits/stdc++.h>#define INF 0x3f3f3f3fusing namespace std;const int maxn=505;struct node{原创 2021-03-05 18:33:35 · 236 阅读 · 0 评论 -
力扣 765. 情侣牵手 思维 并查集 bfs
https://leetcode-cn.com/problems/couples-holding-hands/思路:其实想明白不存在最优的交换方式,只能一一交换,这题就解决了。那么可以直接模拟这个过程,复杂度为O(n2)O(n^2)O(n2)。也可以像官方题解一样,抽象出一个图,把答案和连通分量联系起来,然后用并查集或者bfsbfsbfs做,时间复杂度可以更低。class Solution {public: int minSwapsCouples(vector<int>&原创 2021-02-14 20:08:10 · 176 阅读 · 0 评论 -
PIPIOJ 1176: 逃离迷宫 状压bfs
http://39.106.164.46/problem.php?id=1176思路:状压bfsbfsbfs模板题。step[x][y][key]step[x][y][key]step[x][y][key]表示从起点出发到(x,y)(x,y)(x,y)且拥有的钥匙状态为keykeykey时所需要的最少步数,然后做bfsbfsbfs即可,只需要注意对keykeykey的判断和更新。举个例子,假设有三把钥匙,那么二进制000000000表示没有钥匙,001001001表示拥有第一把钥匙,011011011表原创 2020-10-14 01:22:07 · 231 阅读 · 0 评论 -
力扣 面试题32 - I. 从上到下打印二叉树 bfs
https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/思路:bfsbfsbfs即可,每次处理一整个层次。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *l...原创 2020-04-24 14:16:48 · 244 阅读 · 0 评论 -
力扣 面试题32 - III. 从上到下打印二叉树 III deque+bfs
https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/submissions/思路:bfsbfsbfs时用双端队列就行了,每次处理一层。/** * Definition for a binary tree node. * struct TreeNode { * int val...原创 2020-04-23 22:45:14 · 189 阅读 · 0 评论 -
力扣 面试题37. 序列化二叉树 bfs
思路:自己规定格式,字符串处理很恶心的不过可以用ostringstreamostringstreamostringstream和stringstreamstringstreamstringstream简化操作。想法其实很简单,bfsbfsbfs构造就行了。/** * Definition for a binary tree node. * struct TreeNode { * i...原创 2020-04-22 16:38:06 · 275 阅读 · 0 评论 -
codeforces 1105 D Kilani and the Game 多源bfs
https://codeforces.com/problemset/problem/1105/D题目大意:n∗mn*mn∗m的地图,ppp个人,每个人有若干个堡垒;ppp个人每次从111开始轮流行动,一次行动中第iii个人从他已经占领的格子往外走sp[i]sp[i]sp[i]步能到的格子都会被他占领。当没有格子可以再被占领时游戏结束,问结束后每个人各占领了多少个格子。题目大意:多源bfsbfs...原创 2019-09-29 17:50:00 · 178 阅读 · 0 评论 -
HDU 1429 胜利大逃亡(续) 状态压缩+bfs
http://acm.hdu.edu.cn/showproblem.php?pid=1429添加链接描述Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignat...原创 2019-09-28 10:21:37 · 110 阅读 · 0 评论 -
CSU1232 懒汉的旅行 bfs+优先队列
题目大意:懒汉需要从A城市开车到B城市去见一个朋友,他的汽车的邮箱的容积L升,车每行驶1公里就要消耗1升油。途中只有在到达一个城市之后才可以找的到加油站并加油,而且懒汉的车很特殊,每次加油只能加满整个邮箱。懒汉希望途中加油的次数越少越好,当加油的次数相同时,他希望加油所花的钱数越少越好。现在懒汉把地图告诉了你,你能帮帮他吗?可以认为懒汉出发的时候油箱是满的,如果到达 B城市时油箱不是满的,也无需...原创 2019-09-25 15:35:35 · 150 阅读 · 0 评论 -
POJ 3278 Catch That Cow bfs
http://poj.org/problem?id=3278Farmer 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 co...原创 2019-07-27 23:58:48 · 131 阅读 · 0 评论 -
HDU 1240 三维bfs(其实是英语阅读理解
http://acm.hdu.edu.cn/showproblem.php?pid=1240You're in space.You want to get home.There are asteroids.You don't want to hit them.InputInput to this problem will consist of a (non-empty) seri...原创 2019-01-24 14:31:01 · 273 阅读 · 0 评论 -
HDU 1312 dfs、bfs
http://acm.hdu.edu.cn/showproblem.php?pid=1312There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can...原创 2019-01-23 17:38:17 · 132 阅读 · 0 评论 -
CSU 1726 优先队列+bfs
http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=17264月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活。当地15名消防员耗时一天解救围困的“猪坚强”。不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚强”相比,熊本的猪可没那么幸运,因为它们最终还是没能逃过被送往屠宰场的命运...原创 2019-01-23 17:44:31 · 159 阅读 · 0 评论 -
洛谷 P1135 搜索
https://www.luogu.org/problemnew/show/P1135题目描述呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第i层楼(1≤i≤N)上有一个数字Ki(0≤Ki≤N)电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3,3,1,2,5代表了Ki(K1=3,K...原创 2019-02-16 16:22:39 · 264 阅读 · 0 评论 -
洛谷 P1443
https://www.luogu.org/problemnew/show/P1443题目描述有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步输入输出格式输入格式: 一行四个数据,棋盘的大小和马的坐标 输出格式: 一个n*m的矩阵,代表马到达某个点最少要走几步(左对齐,宽5格,不能到达...原创 2019-02-11 15:19:20 · 755 阅读 · 0 评论 -
洛谷 P1126
https://www.luogu.org/problemnew/show/P1126题目描述机器人移动学会(RMI)现在正尝试用机器人搬运物品。机器人的形状是一个直径$1.6米的球。在试验阶段,机器人被用于在一个储藏室中搬运货物。储藏室是一个N×M的网格,有些格子为不可移动的障碍。机器人的中心总是在格点上,当然,机器人必须在最短的时间内把物品搬运到指定的地方。机器人接受的指令有:向前移动...原创 2019-02-11 15:30:37 · 3845 阅读 · 0 评论 -
HDU 1495 bfs
http://acm.hdu.edu.cn/showproblem.php?pid=1495Problem Description大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 ...原创 2019-03-10 20:58:48 · 111 阅读 · 0 评论 -
洛谷 P1363 dfs
https://www.luogu.org/problemnew/show/P1363题目描述背景 Background(喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫。)WD:呜呜,肿么办啊……LHX:momo...我们一定能走出去的!WD:嗯,+U+U!描述 Description幻象迷宫可以认为是无限大的,不过它...原创 2019-03-29 18:22:34 · 281 阅读 · 0 评论 -
CSU 2305 Magina Loves Bounty Rune 优先队列+bfs
http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2305Description在刀塔的世界里,有一位被称为敌法师的英雄,也就是Magina,他是一个自带Blink的男人。Blink是敌法师的一个技能,敌法师可以使用Blink完成瞬间移动。刀塔的世界是一个n × m 的矩阵(n行m列)。敌法师的Blink技能的瞬移...原创 2019-04-15 21:04:21 · 546 阅读 · 0 评论 -
CSU 2153 我不想改题面了那就改个标题吧 三维dfs/bfs
http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2153Description工业和医学上经常要用到一种诊断技术——核磁共振成像(Magnetic Resonance Imagers)。利用该技术可以对三维物体(例如大脑)进行扫描。扫描的结果用一个三维的数组来保存,数组的每一个元素表示空间的一个像素。数组的元素是0-255的...原创 2019-05-15 15:23:44 · 187 阅读 · 0 评论 -
POJ 1426 Find The Multiple
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there...原创 2019-07-28 22:05:10 · 117 阅读 · 0 评论 -
POJ 3126 Prime Path bfs
http://poj.org/problem?id=3126The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their ...原创 2019-07-29 01:19:07 · 170 阅读 · 0 评论 -
POJ 2612 Find a way bfs
http://acm.hdu.edu.cn/showproblem.php?pid=2612Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a go...原创 2019-07-29 23:19:10 · 150 阅读 · 0 评论 -
POJ 2251 Dungeon Master 三维bfs
http://poj.org/problem?id=2251You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minu...原创 2019-07-27 22:57:11 · 205 阅读 · 0 评论 -
POJ 3984 bfs
http://poj.org/problem?id=3984定义一个二维数组:int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,};它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程...原创 2019-01-23 20:24:43 · 125 阅读 · 0 评论