dfs

Leetcode 题解 - 搜索

坚强是说给别人听的谎言 提交于 2020-01-22 18:30:28
BFS 广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。 第一层: 0 -> {6,2,1,5} 第二层: 6 -> {4} 2 -> {} 1 -> {} 5 -> {3} 第三层: 4 -> {} 3 -> {} 每一层遍历的节点都与根节点距离相同。设 di 表示第 i 个节点与根节点的距离,推导出一个结论:对于先遍历的节点 i 与后遍历的节点 j,有 di <= dj。利用这个结论,可以求解最短路径等 最优解 问题:第一次遍历到目的节点,其所经过的路径为最短路径。应该注意的是,使用 BFS 只能求解无权图的最短路径,无权图是指从一个节点到另一个节点的代价都记为 1。 在程序实现 BFS 时需要考虑以下问题: 队列:用来存储每一轮遍历得到的节点; 标记:对于遍历过的节点,应该将它标记,防止重复遍历。 1. 计算在网格中从原点到特定点的最短路径长度 Leetcode-1091. 二进制矩阵中的最短路径 在一个 N × N 的方形网格中,每个单元格有两种状态:空(0)或者阻塞(1)。 一条从左上角到右下角、长度为 k 的畅通路径,由满足下述条件的单元格 C_1, C_2, …, C_k 组成: 相邻单元格 C_i 和 C_{i+1} 在八个方向之一上连通(此时,C_i 和 C_{i+1}

dfs的简单应用(部分和)

半城伤御伤魂 提交于 2020-01-22 18:29:45
给定整数a1 a2.。。。。an判断是否可以选出若干数凑成sum 输入 n=4 a={1,2,4,7} sum=13 输出 YES 非常经典和基础的一道dfs搜索,搜索逻辑非常简单,先决定第一个数要不要,遍历所有要第一个数的情况之后再遍历所有不要第一个数的情况,当遇到sum是直接退出,起到剪枝的功能 int dfs ( int i , int n ; int k ; ) { if ( sum == k ) return 1 ; if ( i == n ) return 0 ; if ( dfs ( i + 1 , n , k + a [ i ] ) ; ) return 1 ; if ( dfs ( i + 1 , n , k ) ; ) return 1 ; return 0 ; } 事实上应该用bool性实现函数再次不多赘述,可自行选择 来源: CSDN 作者: 徒手装机甲 链接: https://blog.csdn.net/weixin_45757507/article/details/104070791

Contest100000608 - 《算法笔记》8.1小节——搜索专题->深度优先搜索(DFS)

▼魔方 西西 提交于 2020-01-22 13:26:30
文章目录 Contest100000608 - 《算法笔记》8.1小节——搜索专题->深度优先搜索(DFS) 8.1 DFS处理 DFS的概念 DFS与递归的关系 递归实现DFS DFS递归例一:背包选物 例二:最优子序列 Codeup习题 5972-ProblemA-【递归入门】全排列 5973-ProblemB-【递归入门】组合的输出 5974-ProblemC-【递归入门】组合+判断素数 5976-ProblemD-【递归入门】n皇后问题(原始的8皇后问题) 5977-ProblemE-【递归入门】出栈序列统计 5978-ProblemF-【递归入门】走迷宫 DFS小结 Contest100000608 - 《算法笔记》8.1小节——搜索专题->深度优先搜索(DFS) 8.1 DFS处理 DFS的概念 DFS与递归的关系 递归实现DFS DFS递归例一:背包选物 //DFS递归例子:背包选物 /*测试数据: 5 8 3 5 1 2 2 4 5 2 1 3 10 */ #include < cstdio > #include < iostream > using namespace std ; const int maxn = 30 ; int n , V , maxValue = 0 ; //物品件数、背包容量、最大价值 int w [ maxn ] , c [ maxn ]

uva1631-Locker(记忆化搜索)

僤鯓⒐⒋嵵緔 提交于 2020-01-22 08:26:08
Description A password locker with N digits, each digit can be rotated to 0-9 circularly. You can rotate 1-3 consecutive digits up or down in one step. For examples: 567890 → 567901 (by rotating the last 3 digits up) 000000 → 000900 (by rotating the 4th digit down) Given the current state and the secret password, what is the minimum amount of steps you have to rotate the locker in order to get from current state to the secret password? Input Multiple (less than 50) cases, process to EOF. For each case, two strings with equal length (≤ 1000) consists of only digits are given, representing the

Codeforces 620E New Year Tree(DFS序 + 线段树)

五迷三道 提交于 2020-01-22 07:37:15
题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色、询问某结点为根的子树有多少种颜色。 子树,显然DFS序,把子树结点映射到连续的区间。而注意到颜色60种,这样就可以用一个64位整型去表示颜色的集合,然后就是在这个连续区间中用线段树成段更新颜色集合和区间查询颜色集合了。 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 #define MAXN 500000 5 struct Edge{ 6 int v,next; 7 }edge[MAXN<<1]; 8 int NE,head[MAXN]; 9 void addEdge(int u,int v){ 10 edge[NE].v=v; edge[NE].next=head[u]; 11 head[u]=NE++; 12 } 13 14 int l[MAXN],r[MAXN],dfn; 15 void dfs(int u,int fa){ 16 l[u]=++dfn; 17 for(int i=head[u]; i!=-1; i=edge[i].next){ 18 int v=edge[i].v; 19 if(v==fa) continue; 20 dfs(v,u); 21 } 22 r[u]=dfn; 23 }

迷宫-dfs

久未见 提交于 2020-01-22 00:37:20
题目描述 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。 1≤N,M≤5 输入 第一行起点坐标SX,SY,终点坐标FX,FY。 第二行N、M和T,N为行,M为列,T为障碍总数。 接下来T行,每行为障碍点的坐标。 输出 给定起点坐标和终点坐标,问每个方格最多经过1次,从起点坐标到终点坐标的方案总数。 样例输入 1 1 2 2 2 2 1 1 2 样例输出 1 题解: 巨简单的DFS,模板题,数据也很小 #include <bits/stdc++.h> #define MAXN 20 using namespace std; int rub[MAXN][MAXN]; int vis[MAXN][MAXN]; int n, m, t, bx, by, ex, ey; int ans = 0; int zb[4][2] = {{0,-1},{0,1},{1,0},{-1,0}}; void dfs(int x,int y) { if(x == ex && y == ey) { ans++;return; } else for(int i = 0 ; i < 4; ++i) if(x >= 1 && x <= n

LeetCode All in One 题目讲解汇总(持续更新中...)

我是研究僧i 提交于 2020-01-21 20:41:54
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night. Determine the maximum amount of money the thief can rob tonight without alerting the police. Example 1: 3 / \ 2 3 \ \ 3 1 Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. Example 2: 3 / \ 4 5 / \ \ 1 3 1

线段树+Dfs序【CF620E】New Year Tree

吃可爱长大的小学妹 提交于 2020-01-20 10:20:54
Description 你有一棵以1为根的有根树,有n个点,每个节点初始有一个颜色c[i]。 有两种操作: 1 v c 将以v为根的子树中所有点颜色更改为c 2 v 查询以v为根的子树中的节点有多少种不同的颜色 Input 第一行,两个整数 \(n,m\) ,分别代表有 \(n\) 个节点和 \(m\) 个操作。 第二行,共 \(n\) 个整数,代表每个节点的初始颜色 \(c[i]\) 接下来 \(n-1\) 行,描述一条边。 接下来 \(m\) 行,代表每个操作。 Output 对于每个询问操作,输出一行。 刚开始以为是树剖? 结果发现只需要对每个子树操作。 线段树维护 \(dfs\) 序。 对于颜色呢?发现 \(c[i] \leq 60\) 开$long long $可以压成一个数。 因此我们将颜色压缩即可。 记得开$long long $ 虽然没出第二个样例,但我切了 代码 #include<cstdio> #include<iostream> #include<algorithm> #define int long long #define R register using namespace std; const int gz=4e5+8; inline void in(int &x) { int f=1;x=0;char s=getchar(); while(

CF620E New Year Tree 线段树 dfs序

泪湿孤枕 提交于 2020-01-20 08:44:22
luogu链接 题目大意: 有一个节点有颜色的树 操作1.修改子树的颜色 操作2.查询子树颜色的种类 注意,颜色种类小于60种 只有子树的操作,dfs序当然是最好的选择 dfs序列是什么,懒得讲了,自己搜吧 然后开两个数组,begin_和end_记录节点子树在dfs序数组中的开头和结尾 begin,end居然在cf是关键字,还好不是ccf,要不就死了 区间修改一个数,区间查询,线段树的傻逼操作 OK #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define ls rt<<1 #define rs rt<<1|1 #define ll long long const int max4 = 2e6 + 7; const int inf = 0x3f3f3f3f; int n, m; int w[max4], a[max4], begin_[max4], end_[max4]; struct node { int l, r, size, lazy; ll z; void color(int i) { z = z | (1LL << (i - 1)); } void clear() { z = 0LL; } } e[max4];

Find The Multiple/POJ 1426/DFS

笑着哭i 提交于 2020-01-20 08:03:55
Find The Multiple/POJ 1426/DFS 题目 给定一个正整数n,请编写一个程序来寻找n的一个非零的倍数m,这个m应当在十进制表示时每一位上只包含0或者1。你可以假定n不大于200且m不多于100位。 题目来源:POJ 1426 题目链接 解法 虽然题目说是m不多于100位,但其实20位就足够了,所以我们直接用DFS暴搜来枚举m即可。 代码: # include <stdio.h> # include <iostream> # include <algorithm> using namespace std ; int n ; bool flag ; void dfs ( int x , long long y , long long m ) { if ( flag ) return ; if ( x > 19 ) { if ( m == 0 ) return ; if ( m % n == 0 ) { printf ( "%lld\n" , m ) ; flag = true ; return ; } return ; } dfs ( x + 1 , y * 10 , m ) ; dfs ( x + 1 , y * 10 , y + m ) ; } int main ( ) { scanf ( "%d" , & n ) ; while ( n != 0 ) {