Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit add956a

Browse files
Update
1 parent cf46334 commit add956a

8 files changed

+41
-21
lines changed

‎problems/0416.分割等和子集.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
* [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html)
6161
* [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)
6262

63-
### 01背包问题
63+
## 01背包问题
6464

6565
01背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
6666

@@ -92,7 +92,7 @@
9292

9393
动规五部曲分析如下:
9494

95-
1. 确定dp数组以及下标的含义
95+
### 1. 确定dp数组以及下标的含义
9696

9797
01背包中,dp[j] 表示: 容量(所能装的重量)为j的背包,所背的物品价值最大可以为dp[j]
9898

@@ -104,7 +104,7 @@
104104

105105
而dp[6] 就可以等于6了,放进1 和 5,那么dp[6] == 6,说明背包装满了。
106106

107-
2. 确定递推公式
107+
### 2. 确定递推公式
108108

109109
01背包的递推公式为:dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
110110

@@ -113,7 +113,7 @@
113113
所以递推公式:dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]);
114114

115115

116-
3. dp数组如何初始化
116+
### 3. dp数组如何初始化
117117

118118
在01背包,一维dp如何初始化,已经讲过,
119119

@@ -133,7 +133,7 @@
133133
vector<int> dp(10001, 0);
134134
```
135135
136-
4. 确定遍历顺序
136+
### 4. 确定遍历顺序
137137
138138
在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历!
139139
@@ -148,7 +148,7 @@ for(int i = 0; i < nums.size(); i++) {
148148
}
149149
```
150150

151-
5. 举例推导dp数组
151+
### 5. 举例推导dp数组
152152

153153
dp[j]的数值一定是小于等于j的。
154154

‎problems/0695.岛屿的最大面积.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
这里其实涉及到dfs的两种写法。
4646

47-
写法一,dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地
47+
写法一,dfs处理当前节点的相邻节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地
4848

4949
```CPP
5050
// 版本一
@@ -87,7 +87,7 @@ public:
8787
};
8888
```
8989
90-
写法二,dfs处理当前节点,即即在主函数遇到岛屿就计数为0,dfs处理接下来的全部陆地
90+
写法二,dfs处理当前节点,即在主函数遇到岛屿就计数为0,dfs处理接下来的全部陆地
9191
9292
dfs
9393
```CPP

‎problems/kamacoder/0098.所有可达路径.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
【题目描述】
99

10-
给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个函数,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。
10+
给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个程序,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。
1111

1212
【输入描述】
1313

‎problems/kamacoder/0099.岛屿的数量广搜.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。
7474

75-
![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png)
75+
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250124094043.png)
7676

7777
超时写法 (从队列中取出节点再标记,注意代码注释的地方)
7878

‎problems/kamacoder/0100.岛屿的最大面积.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
这里其实涉及到dfs的两种写法。
6565

66-
写法一,dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地
66+
写法一,dfs处理当前节点的相邻节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地
6767

6868
```CPP
6969
// 版本一

‎problems/kamacoder/0101.孤岛的总面积.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@
7272
#include <vector>
7373
using namespace std;
7474
int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // 保存四个方向
75-
int count; // 统计符合题目要求的陆地空格数量
7675
void dfs(vector<vector<int>>& grid, int x, int y) {
7776
grid[x][y] = 0;
78-
count++;
7977
for (int i = 0; i < 4; i++) { // 向四个方向遍历
8078
int nextx = x + dir[i][0];
8179
int nexty = y + dir[i][1];
@@ -109,30 +107,29 @@ int main() {
109107
if (grid[0][j] == 1) dfs(grid, 0, j);
110108
if (grid[n - 1][j] == 1) dfs(grid, n - 1, j);
111109
}
112-
count = 0;
110+
int count = 0;
113111
for (int i = 0; i < n; i++) {
114112
for (int j = 0; j < m; j++) {
115-
if (grid[i][j] == 1) dfs(grid, i, j);
113+
if (grid[i][j] == 1) count++;
116114
}
117115
}
118116
cout << count << endl;
119117
}
120118
```
121119

120+
122121
采用广度优先搜索的代码如下:
123122

124123
```CPP
125124
#include <iostream>
126125
#include <vector>
127126
#include <queue>
128127
using namespace std;
129-
int count = 0;
130128
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
131129
void bfs(vector<vector<int>>& grid, int x, int y) {
132130
queue<pair<int, int>> que;
133131
que.push({x, y});
134132
grid[x][y] = 0; // 只要加入队列,立刻标记
135-
count++;
136133
while(!que.empty()) {
137134
pair<int ,int> cur = que.front(); que.pop();
138135
int curx = cur.first;
@@ -143,7 +140,6 @@ void bfs(vector<vector<int>>& grid, int x, int y) {
143140
if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过
144141
if (grid[nextx][nexty] == 1) {
145142
que.push({nextx, nexty});
146-
count++;
147143
grid[nextx][nexty] = 0; // 只要加入队列立刻标记
148144
}
149145
}
@@ -169,15 +165,16 @@ int main() {
169165
if (grid[0][j] == 1) bfs(grid, 0, j);
170166
if (grid[n - 1][j] == 1) bfs(grid, n - 1, j);
171167
}
172-
count = 0;
168+
int count = 0;
173169
for (int i = 0; i < n; i++) {
174170
for (int j = 0; j < m; j++) {
175-
if (grid[i][j] == 1) bfs(grid, i, j);
171+
if (grid[i][j] == 1) count++;
176172
}
177173
}
178174

179175
cout << count << endl;
180176
}
177+
181178
```
182179

183180

‎problems/kamacoder/图论深搜理论基础.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ if (终止条件) {
162162

163163
终止添加不仅是结束本层递归,同时也是我们收获结果的时候。
164164

165-
另外,其实很多dfs写法,没有写终止条件,其实终止条件写在了, 下面dfs递归的逻辑里了,也就是不符合条件,直接不会向下递归。这里如果大家不理解的话,没关系,后面会有具体题目来讲解。
165+
另外,其实很多dfs写法,没有写终止条件,其实终止条件写在了, 隐藏在下面dfs递归的逻辑里了,也就是不符合条件,直接不会向下递归。这里如果大家不理解的话,没关系,后面会有具体题目来讲解。
166166

167167
3. 处理目前搜索节点出发的路径
168168

‎problems/kamacoder/图论理论基础.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,29 @@
128128

129129
主要是 朴素存储、邻接表和邻接矩阵。
130130

131+
关于朴素存储,这是我自创的名字,因为这种存储方式,就是将所有边存下来。
132+
133+
例如图:
134+
135+
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511112951.png)
136+
137+
图中有8条边,我们就定义 8 * 2的数组,即有n条边就申请n * 2,这么大的数组:
138+
139+
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250110114348.png)
140+
141+
数组第一行:6 7,就表示节点6 指向 节点7,以此类推。
142+
143+
当然可以不用数组,用map,或者用 类 到可以表示出 这种边的关系。
144+
145+
这种表示方式的好处就是直观,把节点与节点之间关系很容易展现出来。
146+
147+
但如果我们想知道 节点1 和 节点6 是否相连,我们就需要把存储空间都枚举一遍才行。
148+
149+
这是明显的缺点,同时,我们在深搜和广搜的时候,都不会使用这种存储方式。
150+
151+
因为 搜索中,需要知道 节点与其他节点的链接情况,而这种朴素存储,都需要全部枚举才知道链接情况。
152+
153+
在图论章节的后面文章讲解中,我会举例说明的。大家先有个印象。
131154

132155
### 邻接矩阵
133156

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /