mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
更新图论
This commit is contained in:
49
README.md
49
README.md
@ -372,25 +372,38 @@
|
|||||||
|
|
||||||
通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor
|
通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor
|
||||||
|
|
||||||
### 深搜广搜
|
1. [图论:理论基础](./problems/kamacoder/图论理论基础.md)
|
||||||
|
2. [图论:深度优先搜索理论基础](./problems/kamacoder/图论深搜理论基础.md)
|
||||||
|
3. [图论:所有可达路径](./problems/kamacoder/0098.所有可达路径.md)
|
||||||
|
4. [图论:广度优先搜索理论基础](./problems/kamacoder/图论广搜理论基础.md)
|
||||||
|
5. [图论:岛屿数量.深搜版](./problems/kamacoder/0099.岛屿的数量深搜.md)
|
||||||
|
6. [图论:岛屿数量.广搜版](./problems/kamacoder/0099.岛屿的数量广搜.md)
|
||||||
|
7. [图论:岛屿的最大面积](./problems/kamacoder/0100.岛屿的最大面积.md)
|
||||||
|
8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md)
|
||||||
|
9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md)
|
||||||
|
10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md)
|
||||||
|
11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md)
|
||||||
|
12. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md)
|
||||||
|
13. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md)
|
||||||
|
14. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md)
|
||||||
|
15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md)
|
||||||
|
16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md)
|
||||||
|
17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md)
|
||||||
|
18. [图论:冗余连接II](./problems/kamacoder/0109.冗余连接II.md)
|
||||||
|
19. [图论:最小生成树之prim](./problems/kamacoder/0053.寻宝-prim.md)
|
||||||
|
20. [图论:最小生成树之kruskal](./problems/kamacoder/0053.寻宝-Kruskal.md)
|
||||||
|
21. [图论:拓扑排序](./problems/kamacoder/0117.软件构建.md)
|
||||||
|
22. [图论:dijkstra(朴素版)](./problems/kamacoder/0047.参会dijkstra朴素.md)
|
||||||
|
23. [图论:dijkstra(堆优化版)](./problems/kamacoder/0047.参会dijkstra堆.md)
|
||||||
|
24. [图论:Bellman_ford 算法](./problems/kamacoder/0094.城市间货物运输I.md)
|
||||||
|
25. [图论:Bellman_ford 队列优化算法(又名SPFA)](./problems/kamacoder/0094.城市间货物运输I-SPFA.md)
|
||||||
|
26. [图论:Bellman_ford之判断负权回路](./problems/kamacoder/0095.城市间货物运输II.md)
|
||||||
|
27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0095.城市间货物运输II.md)
|
||||||
|
28. [图论:Floyd 算法](./problems/kamacoder/0097.小明逛公园.md)
|
||||||
|
29. [图论:A * 算法](./problems/kamacoder/0126.骑士的攻击astar.md)
|
||||||
|
30. [图论:最短路算法总结篇](./problems/kamacoder/最短路问题总结篇.md)
|
||||||
|
31. [图论:图论总结篇](./problems/kamacoder/图论总结篇.md)
|
||||||
|
|
||||||
* [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md)
|
|
||||||
* [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md)
|
|
||||||
* [图论:广度优先搜索理论基础](./problems/图论广搜理论基础.md)
|
|
||||||
* [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md)
|
|
||||||
* [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md)
|
|
||||||
* [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md)
|
|
||||||
* [图论:1020.飞地的数量](./problems/1020.飞地的数量.md)
|
|
||||||
* [图论:130.被围绕的区域](./problems/0130.被围绕的区域.md)
|
|
||||||
* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md)
|
|
||||||
* [图论:827.最大人工岛](./problems/0827.最大人工岛.md)
|
|
||||||
* [图论:127. 单词接龙](./problems/0127.单词接龙.md)
|
|
||||||
* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md)
|
|
||||||
* [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md)
|
|
||||||
* [图论:并查集理论基础](./problems/图论并查集理论基础.md)
|
|
||||||
* [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md)
|
|
||||||
* [图论:684.冗余连接](./problems/0684.冗余连接.md)
|
|
||||||
* [图论:685.冗余连接II](./problems/0685.冗余连接II.md)
|
|
||||||
|
|
||||||
(持续更新中....)
|
(持续更新中....)
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# dijkstra(堆优化版)精讲
|
# dijkstra(堆优化版)精讲
|
||||||
|
|
||||||
[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047)
|
[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# dijkstra(朴素版)精讲
|
# dijkstra(朴素版)精讲
|
||||||
|
|
||||||
[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047)
|
[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# kruskal算法精讲
|
# kruskal算法精讲
|
||||||
|
|
||||||
[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053)
|
[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# prim算法精讲
|
# prim算法精讲
|
||||||
|
|
||||||
[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053)
|
[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053)
|
||||||
|
@ -1,11 +1,4 @@
|
|||||||
|
|
||||||
<p align="center">
|
|
||||||
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
|
|
||||||
<img src="../pics/训练营.png" width="1000"/>
|
|
||||||
</a>
|
|
||||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
|
||||||
|
|
||||||
|
|
||||||
# 替换数字
|
# 替换数字
|
||||||
|
|
||||||
[卡码网题目链接](https://kamacoder.com/problempage.php?pid=1064)
|
[卡码网题目链接](https://kamacoder.com/problempage.php?pid=1064)
|
||||||
@ -376,9 +369,3 @@ main();
|
|||||||
### Rust:
|
### Rust:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
|
|
||||||
<p align="center">
|
|
||||||
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
|
|
||||||
<img src="../pics/训练营.png" width="1000"/>
|
|
||||||
</a>
|
|
||||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
|
||||||
|
|
||||||
|
|
||||||
# 右旋字符串
|
# 右旋字符串
|
||||||
@ -343,9 +338,3 @@ var reverseLeftWords = function(s, n) {
|
|||||||
### Rust:
|
### Rust:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
|
||||||
</a>
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# Bellman_ford 队列优化算法(又名SPFA)
|
# Bellman_ford 队列优化算法(又名SPFA)
|
||||||
|
|
||||||
[卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152)
|
[卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# Bellman_ford 算法精讲
|
# Bellman_ford 算法精讲
|
||||||
|
|
||||||
[卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152)
|
[卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# bellman_ford之判断负权回路
|
# bellman_ford之判断负权回路
|
||||||
|
|
||||||
[卡码网:95. 城市间货物运输 II](https://kamacoder.com/problempage.php?pid=1153)
|
[卡码网:95. 城市间货物运输 II](https://kamacoder.com/problempage.php?pid=1153)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# bellman_ford之单源有限最短路
|
# bellman_ford之单源有限最短路
|
||||||
|
|
||||||
[卡码网:96. 城市间货物运输 III](https://kamacoder.com/problempage.php?pid=1154)
|
[卡码网:96. 城市间货物运输 III](https://kamacoder.com/problempage.php?pid=1154)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# Floyd 算法精讲
|
# Floyd 算法精讲
|
||||||
|
|
||||||
[卡码网:97. 小明逛公园](https://kamacoder.com/problempage.php?pid=1155)
|
[卡码网:97. 小明逛公园](https://kamacoder.com/problempage.php?pid=1155)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 98. 所有可达路径
|
# 98. 所有可达路径
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1170)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1170)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 99. 岛屿数量
|
# 99. 岛屿数量
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 99. 岛屿数量
|
# 99. 岛屿数量
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 100. 岛屿的最大面积
|
# 100. 岛屿的最大面积
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1172)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1172)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 101. 孤岛的总面积
|
# 101. 孤岛的总面积
|
||||||
|
|
||||||
[卡码网:101. 孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173)
|
[卡码网:101. 孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 102. 沉没孤岛
|
# 102. 沉没孤岛
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1174)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1174)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 103. 水流问题
|
# 103. 水流问题
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1175)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1175)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 104.建造最大岛屿
|
# 104.建造最大岛屿
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1176)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1176)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 105.有向图的完全可达性
|
# 105.有向图的完全可达性
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1177)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1177)
|
||||||
@ -192,7 +194,7 @@ int main() {
|
|||||||
|
|
||||||
**第二种写法注意有注释的地方是和写法一的区别**
|
**第二种写法注意有注释的地方是和写法一的区别**
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
写法二:dfs处理下一个要访问的节点
|
写法二:dfs处理下一个要访问的节点
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 106. 岛屿的周长
|
# 106. 岛屿的周长
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1178)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1178)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 107. 寻找存在的路径
|
# 107. 寻找存在的路径
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1179)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1179)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 108. 冗余连接
|
# 108. 冗余连接
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1181)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1181)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 109. 冗余连接II
|
# 109. 冗余连接II
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 110. 字符串接龙
|
# 110. 字符串接龙
|
||||||
|
|
||||||
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182)
|
[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 拓扑排序精讲
|
# 拓扑排序精讲
|
||||||
|
|
||||||
[卡码网:117. 软件构建](https://kamacoder.com/problempage.php?pid=1191)
|
[卡码网:117. 软件构建](https://kamacoder.com/problempage.php?pid=1191)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
|
||||||
|
|
||||||
# 大数减法
|
# 大数减法
|
||||||
|
|
||||||
本题测试数据超过int 和 longlong了,所以考察的使用 string 来模拟 两个大数的 加减操作。
|
本题测试数据超过int 和 longlong了,所以考察的使用 string 来模拟 两个大数的 加减操作。
|
||||||
|
49
problems/kamacoder/0129.小美的蛋糕切割.md
Normal file
49
problems/kamacoder/0129.小美的蛋糕切割.md
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
|
||||||
|
前缀和
|
||||||
|
|
||||||
|
```CPP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <climits>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
int main () {
|
||||||
|
int n, m;
|
||||||
|
cin >> n >> m;
|
||||||
|
int sum = 0;
|
||||||
|
vector<vector<int>> vec(n, vector<int>(m, 0)) ;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
cin >> vec[i][j];
|
||||||
|
sum += vec[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 统计横向
|
||||||
|
vector<int> horizontal(n, 0);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0 ; j < m; j++) {
|
||||||
|
horizontal[i] += vec[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 统计纵向
|
||||||
|
vector<int> vertical(m , 0);
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
for (int i = 0 ; i < n; i++) {
|
||||||
|
vertical[j] += vec[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int result = INT_MAX;
|
||||||
|
int horizontalCut = 0;
|
||||||
|
for (int i = 0 ; i < n; i++) {
|
||||||
|
horizontalCut += horizontal[i];
|
||||||
|
result = min(result, abs(sum - horizontalCut - horizontalCut));
|
||||||
|
}
|
||||||
|
int verticalCut = 0;
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
verticalCut += vertical[j];
|
||||||
|
result = min(result, abs(sum - verticalCut - verticalCut));
|
||||||
|
}
|
||||||
|
cout << result << endl;
|
||||||
|
}
|
||||||
|
```
|
79
problems/kamacoder/0130.小美的字符串变换.md
Normal file
79
problems/kamacoder/0130.小美的字符串变换.md
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
|
||||||
|
# 130.小美的字符串变换
|
||||||
|
|
||||||
|
本题是[岛屿数量](./0099.岛屿的数量广搜.md)的进阶版,主要思路和代码都是一样的,统计一个图里岛屿的数量,也是染色问题。
|
||||||
|
|
||||||
|
1、 先枚举各个可能出现的矩阵
|
||||||
|
2、 针对矩阵经行广搜染色(深搜,并查集一样可以)
|
||||||
|
3、 统计岛屿数量最小的数量。
|
||||||
|
|
||||||
|
```CPP
|
||||||
|
#include <iostream>
|
||||||
|
#include <climits>
|
||||||
|
#include <vector>
|
||||||
|
#include <queue>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// 广搜代码同 卡码网:99. 岛屿数量
|
||||||
|
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
|
||||||
|
void bfs(const vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y, char a) {
|
||||||
|
queue<pair<int, int>> que;
|
||||||
|
que.push({x, y});
|
||||||
|
visited[x][y] = true; // 只要加入队列,立刻标记
|
||||||
|
while(!que.empty()) {
|
||||||
|
pair<int ,int> cur = que.front(); que.pop();
|
||||||
|
int curx = cur.first;
|
||||||
|
int cury = cur.second;
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nextx = curx + dir[i][0];
|
||||||
|
int nexty = cury + dir[i][1];
|
||||||
|
if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过
|
||||||
|
if (!visited[nextx][nexty] && grid[nextx][nexty] == a) {
|
||||||
|
que.push({nextx, nexty});
|
||||||
|
visited[nextx][nexty] = true; // 只要加入队列立刻标记
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
string s;
|
||||||
|
cin >> n;
|
||||||
|
int result = INT_MAX;
|
||||||
|
cin >> s;
|
||||||
|
for (int k = 1; k < n; k++) {
|
||||||
|
if (n % k != 0) continue;
|
||||||
|
// 计算出 矩阵的 行 和 列
|
||||||
|
int x = n / k;
|
||||||
|
int y = k;
|
||||||
|
//cout << x << " " << y << endl;
|
||||||
|
vector<vector<char>> vec(x, vector<char>(y, 0));
|
||||||
|
// 填装矩阵
|
||||||
|
int sCount = 0;
|
||||||
|
for (int i = 0; i < x; i++) {
|
||||||
|
for (int j = 0; j < y; j++) {
|
||||||
|
vec[i][j] = s[sCount++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始广搜染色
|
||||||
|
vector<vector<bool>> visited(x, vector<bool>(y, false));
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < x; i++) {
|
||||||
|
for (int j = 0; j < y; j++) {
|
||||||
|
|
||||||
|
if (!visited[i][j]) {
|
||||||
|
count++; // 遇到没访问过的陆地,+1
|
||||||
|
bfs(vec, visited, i, j, vec[i][j]); // 将与其链接的陆地都标记上 true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 取岛屿数量最少的
|
||||||
|
result = min (result, count);
|
||||||
|
|
||||||
|
}
|
||||||
|
cout << result << endl;
|
||||||
|
}
|
||||||
|
```
|
5
problems/kamacoder/0131.小美的树上染色.md
Normal file
5
problems/kamacoder/0131.小美的树上染色.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# 131. 小美的树上染色
|
||||||
|
|
||||||
|
贪心的思路 : https://blog.csdn.net/weixin_43739821/article/details/136299012
|
||||||
|
|
||||||
|
dp思路:https://www.cnblogs.com/ganyq/p/18111114
|
29
problems/kamacoder/小美的排列询问.md
Normal file
29
problems/kamacoder/小美的排列询问.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
小美的排列询问
|
||||||
|
|
||||||
|
注意 x 和y 不分先后
|
||||||
|
|
||||||
|
```CPP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
int main() {
|
||||||
|
int n, x, y;
|
||||||
|
cin >> n;
|
||||||
|
vector<int> vec(n, 0);
|
||||||
|
for (int i =0; i < n; i++) {
|
||||||
|
cin >> vec[i];
|
||||||
|
}
|
||||||
|
cin >> x >> y;
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
if (x == vec[i] && y == vec[i + 1]) || (y == vec[i] && x == vec[i + 1]) ) {
|
||||||
|
cout << "Yes" << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << "No" << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
30
problems/kamacoder/小美走公路.md
Normal file
30
problems/kamacoder/小美走公路.md
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
|
||||||
|
两个注意点
|
||||||
|
|
||||||
|
1. x 可以比 y 大,题目没规定 x 和y 的大小顺序
|
||||||
|
2. 累计相加的数可能超过int
|
||||||
|
|
||||||
|
|
||||||
|
```CPP
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
int main () {
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vector<int> vec(2* n + 1, 0);
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
cin >> vec[i];
|
||||||
|
vec[n + i] = vec[i];
|
||||||
|
}
|
||||||
|
int x, y;
|
||||||
|
cin >> x >> y;
|
||||||
|
int xx = min(x ,y); // 注意点1:x 可以比 y 大
|
||||||
|
int yy = max(x, y);
|
||||||
|
long long a = 0, b = 0; // 注意点2:相加的数可能超过int
|
||||||
|
for (int i = xx; i < yy; i++) a += vec[i];
|
||||||
|
for (int i = yy; i < xx + n; i++ ) b += vec[i];
|
||||||
|
cout << min(a, b) << endl;
|
||||||
|
}
|
||||||
|
```
|
Reference in New Issue
Block a user