题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root s…
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
int cmp(const void*a,const void*b){int*m(int*)a;int*n(int*)b;return *m-*n;
}
void DFS(int *nums, int numsSize, int depth, int *path, int *use, int **res,int*returnSize) {if …
题目
法1:岛屿数量
class Solution {public int numIslands(char[][] grid) {int m grid.length, n grid[0].length;int[][] used new int[m][n];int res 0;for (int i 0; i < m; i) {for (int j 0; j < n; j) {if (grid[i][j] 0 || used[i][j] 1) …
题目: HDU - 1010 Tempter of the Bone本人今天第一次学剪枝,成功ac和大家分享学习成果。这道题我是用深度优先搜索来解题的但是只用搜索的话 提交就 TLE
今天做这道题纠结了一个下午,最后终于solve了呵呵
这题用到了奇偶剪枝
奇偶剪枝就…
题面 题解 DFS搜索简单题,我们以二维矩阵的每一个点为起点,然后开始dfs,只要有一种情况满足就可以返回true 代码
class Solution {
public:bool dfs(vector<vector<char>> &matrix, string &str, int len, int x, int y…
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 思路:回溯
int all;//排列总数
void DFS(int *nums, int numsSize, int depth, int *path, int *use, int **res) {if (depth numsSize) {//当前深度达到总…
分数 25
全屏浏览题目
作者 CHEN, Yue
单位 浙江大学
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product fr…
文章目录图的定义和术语图的存储结构邻接矩阵表示法邻接表表示法邻接矩阵与邻接表表示法的关系图的遍历深度优先搜索( DFS ——Depth First Search)基于邻接矩阵的DFS算法基于邻接表的DFS算法的实现广度优先搜索( BFS——Breadth First Search)基于邻接表的BFS算法的实现有向无…
100.相同的树题目如下解题思路c代码题目如下 解题思路
这个题目是用来认识树的,不要觉得它很神秘,比较树,我们用到了递归,通过递归层层分工,代码思路变成非常简单。
c代码
/*** Definition for a binary tree node.…
Conversion of feet/inches to meters-英尺、英里装换为米,允许重复计算://Conversion of feet/inches to meters-英尺、英里装换为米,允许重复计算
#include<iostream>
#include<cmath>using namespace std;void get_input(doub…
DFS(Depth first search)
适用范围:不重不漏地枚举到目标状态的每一条路径。
算法过程:对一个当前的合法状态A,对其所有的子状态(子节点),按顺序选择一种进行搜索,递归…
import java.util.Scanner;public class TestOne {static int ans 0;static int[] arr {0,0,0,0,0,0,0,1,1,1,1,1};//12个元素(将减取得格子标记为1)。与之前全排列不同,该数组具有重复元素static boolean vis[] new boolean[12]; //标记元素有没有被访问public …
题目: 代码:
import java.util.Scanner;public class Main {/** 思路:
测试数据
2 4
##..
..##* */static int n;//行static int m;//列static boolean[][] vis;//访问数组static int[][] t {{1,0},{-1,0},{0,1},{0,-1}};public static void…
102.二叉树的层序遍历题目如下解题思路c代码题目如下 解题思路
每层有一定的节点,这个节点与层数有关,我们引入一个depth来表示这个树有多少层,递归来完成二叉树的层序遍历
c代码
/*** Definition for a binary tree node.* struct TreeNo…
题目
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer.
Starting from one root…
此题可以转化为求删除一个结点后的连通子图的数量,删除结点可以通过DFS直接return的方法来实现。
#include <cstdio>
#include <vector>using namespace std;vector<int> city[1010];
bool visited[1010];
int n, m, k;void DFS(int index, int …
题目 The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K-P factorization of N for any positive integers N, K and P. Input Specification: Each inp…
关键:
dfs(深搜),然后判断行列,这里先按照一行一行的查找,我当初也是这样想的,只是写到了一半就放弃了。下次应该注意!
#include <bits/stdc.h>
using namespace std;
int a…
【Q310】(md) 被围绕的区域 给定一个二维的矩阵,包含 ‘X’ 和 ‘O’(字母 O)。 找到所有被 ‘X’ 围绕的区域,并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。 示例: X X X XX O O X X X O XX O X X 运行你的函数后࿰…
c排序再累加最后去个尾求个和#include<bits/stdc.h>
using namespace std;
#define int long long
typedef long long ll;
const int mod1e97,inv2(mod1)/2;
int ksm(int b,int n){int res1;while(n){if(n&1) res1ll*res*b%mod;b1ll*b*b%mod; n>>1;}return res…
题目连接 这道题是n皇后问题的延伸 只需要先搜索黑皇后,再在搜索黑皇后的前提下遍历白皇后即可
#include <bits/stdc.h>
using namespace std;
const int N 15;
int pic[N][N];
int res;
int n;
//0表示不能放,1表示能放,2表示黑皇后…
排列数字
#include <bits/stdc.h>
using namespace std;
typedef long long ll;
const int N10;
int path[N]; //用来存方案
int st[N]; //用来检查哪一个数被用过
int n;
void dfs(int u)
{//表示深搜到最后了,即可以输出结果if(un){for(int i0;i<n;i)co…
543. 二叉树的直径 【DFS】首先要想直径长,那么一定是计算两个叶子节点之间的路径。这样就可以递归地去计算以每个节点为根节点时他的最远的两个叶子之前的路径长度。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeN…
【LetMeFly】1026.节点与其祖先之间的最大差值
力扣题目链接:https://leetcode.cn/problems/maximum-difference-between-node-and-ancestor/
给定二叉树的根节点 root,找出存在于 不同 节点 A 和 B 之间的最大值 V,其中 V |A.val - B.val…
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
using ll long long ;
const int M1e610;
int n,x,dep[M],mx[M],dp[M];///dep深度,mx从该点到达的最深节点位置
vector<int> g[M];
void dfs(int x,int f…
跟着柳婼学姐学习的笔记ヾ(≧▽≦*)o题意分析知识点词汇CODE原题目:
1034 Head of a Gang (30 分).题意
警察通过犯罪团伙成员间的通话时长来判断头目。 一个犯罪团伙至少有3人,彼此间的总权值(通话时长)…
public class TestOne {static String[] mg new String[10]; //迷宫数组static int ans 0; //记录结果static int[][] vis new int[10][10]; //访问数组public static void main(String[] args) {//1.输入迷宫数组mg[0] "UDDLUULRUL";mg[1] "UURLLLRRRU&qu…
import java.util.*;/** public class TreeNode {* int val 0;* TreeNode left null;* TreeNode right null;* }*/public class Solution {/*** * param root TreeNode类 * param sum int整型 * return int整型ArrayList<ArrayList<>>思路:使用…
跟着柳婼学姐学习的笔记ヾ(≧▽≦*)o题意分析CODE原题目:
1043 Is It a Binary Search Tree (25 分).题意
给出BST的结点数N以及其结点序列(经递增排序后可得BST中序序列), ① 要求判断给出的序列是否是B…
前置技能:
1.反素数,推荐博客:ACdreamers讲解反素数。定义:对于任何正整数 n ,其约数的个数记做 f(n) .例如 f(1) 1, f(6) 4 .如果某个正整数n满足:对于任意 i ( 0 < i < n ) ,都有 f( i ) < f( n ),则称 …
文章目录首个公共祖先 First Common Ancestor思路Tag首个公共祖先 First Common Ancestor
如何找出二叉树中某两个节点的第一个共同祖先。不得将其他的节点存储在另外的数据结构中。不一定是BST
[3,4,1,6,2,9,8,null,null,7,5], p 6, q 2
公共祖先就是4
p6,q 5,公共祖先就…
跟着柳婼学姐学习的笔记ヾ(≧▽≦*)o题意分析CODE原题目:
1079 Total Sales of Supply Chain (25 分).题意
给出供应链的成员总数N(0-N-1),根供应商给的价格P,增长率r,以及来自供应…
跟着柳婼学姐学习的笔记ヾ(≧▽≦*)o题意分析知识点string类~scanf函数词汇CODE原题目: 1086 Tree Traversals Again (25 分)
题意
给出结点总数N(1-N),接下来2N行给出堆栈的入栈和出栈操作, …
题目来源:PAT (Advanced Level) Practice
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to …
A. Gregor and Cryptography 构造... #include <bits/stdc.h>
#define all(a) a.begin(),a.end()
#define pb push_back
using namespace std;
using ll long long ;
void solve()
{ll p;cin>>p;for(ll i2;i*i<p;i){if(p%i0){cout<<i<<" &quo…
import java.util.Scanner;public class Main {/**思路:每次按字典序往前缀中加字母,如果合法,就将字母加入,并打印 和 记录当前的已找到的困难串顺序*(1)dfs(prefix) 初始时前缀prefix""* 依次…
import java.util.Scanner;public class Main {/*思路:* (1)从头开始进行遍历,寻找第一个积水W* (1.1)从该处进行dfs遍历,* 寻找其8个方向是否有积水W,如果存在将其改为…
The Area of an Arbitrary Triangle-任意三角形的面积,允许重复计算://The Area of an Arbitrary Triangle-任意三角形的面积
#include<iostream>
#include<cmath>
using namespace std;bool IsTriangle(double a,double b,double c);
void areafun(double a,doubl…
你有一架天平和 N 个砝码,这 N 个砝码重量依次是 W1,W2,⋅⋅⋅,WN。
请你计算一共可以称出多少种不同的正整数重量?
注意砝码可以放在天平两边。
输入格式 输入的第一行包含一个整数 N。
第二行包含 N 个整数:W1,W2,W3,⋅⋅⋅,WN。
输出…
Lake Counting时间限制:1000 ms | 内存限制:65535 KB难度:3描述:由于最近的降雨,农民约翰的田地里的水聚集在不同的地方,用一个nm(1≤n≤100;1 < m < 100)平方。每个正方形包含水( ‘ w ’ )或旱地( …
题目描述 Elma is learning chess figures. She learned that a rook can move either horizontally or vertically. To enhance her understanding of rook movement Elma’s grandmother gave Elma an 8 8 chess board and asked her to find a way to move the rook from a…
题目描述 Hearing that the latest fashion trend was cows with two spots on their hides, Farmer John has purchased an entire herd of two-spot cows. Unfortunately, fashion trends tend to change quickly, and the most popular current fashion is cows with only o…
1103 Integer Factorization (30point(s))
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, …
题目描述
大学班级选班长,N 个同学均可以发表意见 若意见为 A B 则表示 A 认为 B 合适,意见具有传递性,即 A 认为 B 合适,B 认为 C 合适,则 A 也认为 C 合适 勤劳的 TT 收集了M条意见,想要知道最高票数&am…
题目描述
Given n positive numbers, ZJM can select exactly K of them that sums to S. Now ZJM wonders how many ways to get it!
Input
The first line, an integer T<100, indicates the number of test cases. For each case, there are two lines. The first lin…
题目:HDU 1016 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid1016 题目: Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37741 Accepted Submission(s): …
LeetCode257题目如下思路与代码题目如下 思路与代码
二叉树,很熟悉,还是简单的二叉树,缪杀! 二叉树路径直接dfs深搜就可以了啊!
/*** Definition for a binary tree node.* struct TreeNode {* int val;* Tr…
输入两棵二叉树A、B,判断 B 是不是 A 的子结构。(ps:我们约定空树不是任意一个树的子结构) /**
public class TreeNode {int val 0;TreeNode left null;TreeNode right null;public TreeNode(int val) {this.val val;}
}
*/
p…
1161. 最大层内元素和
dfs深搜,能比较方便的统计层数。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/
class Solution {private int[] val…
TWOTWOFOUR题目含义解题思路全排列枚举深搜结果展示其中深搜运行0.009s其中全排运行0.137s如何测试运行时间题目含义
t w o ,f u r分别是0,1,2,3,4,5,6,7,8,…
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
思路分析&…
【题目来源】https://www.acwing.com/problem/content/4523/【题目描述】 给定一个正整数 X,请你在 X 后面添加若干位数字(至少添加一位数字;添加的数不能有前导0),使得结果为质数,在这个前提下所得的结果应…
AtCoder Beginner Contest 329
比赛地址:Sky Inc, Programming Contest 2023(AtCoder Beginner Contest 329) - AtCoder
E - Stamp
Problem Statement
You are given two strings: S S S, which consi…
文章目录 一、题目二、题解 一、题目
There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island’s left and top edges, and the Atlantic Ocean touches the island’s right and bottom edges…
相关题目: 207. 课程表 210. 课程表 II 1462. 课程表 IV
class CourseSchedule:"""207.课程表https://leetcode.cn/problems/course-schedule/"""def __init__(self):# 记录⼀次递归堆栈中的节点self.onPath []# 记录遍历过的节点&…
目录
Lake Counting S
求细胞数量
海战
组合的输出
div3 A. Square
div3 B. Arranging Cats Lake Counting S
P1596 [USACO10OCT] Lake Counting S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
感谢大佬的指点!!!!
思…
1、题目
问题描述
过年小蓝想要回家串门。
蓝桥村可以抽象为 n n n 个节点, n − 1 n-1 n−1 条边的一棵树,每条边有边权长度 w i w_i wi。
小蓝可以选择任意一个点作为起点,然后选择一条路径,可以访问每个节点至少一次。…
文章目录 一、题目二、题解 一、题目
You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.
Return the size of the largest island in grid after applying this operation.
An island is a 4-directionally connected group of…
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f 来源:牛客网
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike…
蓝桥OJ 2942数字王国之军训排队 #include<bits/stdc.h>
using namespace std;const int N 15;//最多10队
int a[N], n;
vector<int>v[N];//二维数组 v[i]记录队伍i中所有人的编号bool dfs(int cnt, int dep)
{if (dep n1){//判断合法性for (int i 1; i < n; …
题目:
Given a binary tree and a sum, find all root-to-leaf paths where each paths sum equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum 22, 5/ \4 8/ / \11 13 4/ \ / \
7 …
一 、 DFS
#include<iostream>
#include<vector>
#include<cstring>
#define MAX 20005
using namespace std;int m, n;
vector<int> g[MAX];
int max_i 0;
int max_s 0;
int visit[MAX] {0};
void dfs(int cur, int s){if(s > max_s){max_s …
题目 Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request. Input…
题目
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.
Now given any weighted tree, yo…
题目 One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core. Input …
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root s…
A 签到
#include<bits/stdc.h>#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int a[105];
int main(){iosint n,t;cin>>t;while(t--){cin>>n;for(int i1;i<n;i){cin>>a[i];}sort(a1,an1);bool flag1;for…
题目 Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness. Input Specification: Each input file contains one test case. For each case, the…
题目: 即每一行、每一列、每一3*3的格子都含有1~9的全部数字,且不重复 解析:递归求解。 每次确定一个位置的数字,就一路走到底或者中间数字不合法,再返回上一层继续查找 import java.util.Scanner;public class Main …
题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: Ea…
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;public class Main {static int n; //元素个数static int[] a; //元素数组static int k; //需得到的数字static ArrayList<Integer> res new ArrayList();public static void main(Stri…
文章目录二叉树的堂兄弟 Cousins in Binary Tree思路Tag二叉树的堂兄弟 Cousins in Binary Tree
在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k1 处。
如果二叉树的两个节点深度相同,但 父节点不同 ,则…
题目来源:PAT (Advanced Level) Practice
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.…
1、题目
问题描述
小蓝和小桥上完课后,小桥回顾了课上教的树形数据结构,他在地上画了一棵根节点为 1 的树,并且对每个节点都赋上了一个权值 w i w_i wi。
小蓝对小桥多次询问,每次询问包含两个整数 x , k x,k x,kÿ…
463. 岛屿的周长
class Solution:def islandPerimeter(self, grid: List[List[int]]) -> int:res 0m, n len(grid), len(grid[0])# 遍历grid,就是所有的封闭岛屿for i in range(m):for j in range(n):if grid[i][j] 1:res self.dfs_matrix(grid, i, j)retur…
问题描述如下图所示,3 x 3 的格子中填写了一些整数。--*----|10* 1|52|--****--|20|30* 1|*******--| 1| 2| 3|------我们沿着图中的星号线剪开,得到两个部分,每个部分的数字和都是60。本题的要求就是请你编程判定:对给定的m x n …
题目
1021. Deepest Root (25)
时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you …
题目
1018. Public Bike Management (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the wo…
Leetcode 2858. Minimum Edge Reversals So Every Node Is Reachable 1. 解题思路2. 代码实现 题目链接:2858. Minimum Edge Reversals So Every Node Is Reachable
1. 解题思路
这一题也有点惭愧,因为没能自力做出来,不过思路上其实是想到…
题目链接 Leetcode.2867 统计树中的合法路径数目 rating : 2428 题目描述
给你一棵 n n n 个节点的无向树,节点编号为 1 1 1 到 n n n 。给你一个整数 n n n 和一个长度为 n − 1 n - 1 n−1 的二维整数数组 e d g e s edges edges ,其中 e d g …
题目链接 Leetcode.2477 到达首都的最少油耗 rating : 2012 题目描述
给你一棵 n n n 个节点的树(一个无向、连通、无环图),每个节点表示一个城市,编号从 0 0 0 到 n − 1 n - 1 n−1 ,且恰好有 n − 1 n - 1 n−…
2.危险系数 - 蓝桥云课 (lanqiao.cn) n, m map(int, input().split())
map_ [[] for i in range(n 1)]
used [0 for i in range(n 1)]
used_ [0 for i in range(n 1)]
cnt 0
res []
for _ in range(m):u, v map(int, input().split())map_[u].append(v)map_[v].appen…
1.全球变暖 - 蓝桥云课 (lanqiao.cn) import os import sys # 请在此输入您的代码 sys.setrecursionlimit(60000) n int(input()) dao [] for _ in range(n): tmp list(input()) dao.append(tmp) dict [(1, 0), (0, 1), (-1, 0), (0, -1)] used [[0 for _ in range(n)] fo…
一个不知名大学生,江湖人称菜狗 original author: Jacky Li Email : 3435673055qq.com Time of completion:2024.03.27 Last edited: 2024.03.27 目录
算法6.4-6.6DFS
第1关:算法6.5采用邻接矩阵表示图的深搜
任务描述
相关知识
编程要求…
题目链接 Leetcode.2316 统计无向图中无法互相到达点对数 rating : 1604 题目描述
给你一个整数 n n n ,表示一张 无向图 中有 n n n 个节点,编号为 0 0 0 到 n − 1 n - 1 n−1 。同时给你一个二维整数数组 e d g e s edges edges ,其…
http://ybt.ssoier.cn:8088/problem_show.php?pid1219
#include<bits/stdc.h>
using namespace std;
int a[20][20];
int v[20][20];
int go[8][2] {{1,2},{2,1},{-1,2},{2,-1},{-1,-2},{-2,-1},{-2,1},{1,-2}};
int cns;
int n,m,x,y,T;
int dian1; // 开始就是1
vo…
直接爆搜,枚举每个数取出的质因子即可。
PS:我是真的菜。
#include <bits/stdc.h>
#define ll long long
using namespace std;
const int N 1e310;
const int inf 0x3f3f3f3f;
//vector <int> pd[20];
int pr[N],cnt,a[20];
bool vis[N…
NC13886 Shortest Path
示例1
输入
2 4 1 2 5 2 3 8 3 4 6 6 1 3 5 3 2 3 4 5 4 4 3 9 4 6 10
输出
11 31
说明 In the first example, you can divide them into (1,2), and (3,4), then the minimum sum of length is 5611 In the second example, you can divide the…
题目
You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols and -. For each integer, you should choose one from and - as its new symbol. Find out how many ways to assign symbols to make sum of integers eq…
#题目 In LeetCode Store, there are some kinds of items to sell. Each item has a price.
However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.
You are given the each item’s price, a…
给定一个m n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置ÿ…
Abandoned countryDescription
An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤100…