Skip to content

Commit

Permalink
DFS和BFS例题
Browse files Browse the repository at this point in the history
  • Loading branch information
StupidRabbit29 committed Apr 15, 2020
1 parent 3a7daed commit 119df30
Show file tree
Hide file tree
Showing 12 changed files with 628 additions and 5 deletions.
20 changes: 20 additions & 0 deletions Algorithm.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "士兵布置_POJ3069-贪心
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "字典序最小问题_POJ3617-贪心", "字典序最小问题_POJ3617-贪心\字典序最小问题_POJ3617-贪心.vcxproj", "{4B8A771F-ACB7-4044-86B3-E2E5CDA10742}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lake_Counting_POJ2386-DFS", "Lake_Counting_POJ2386-DFS\Lake_Counting_POJ2386-DFS.vcxproj", "{253879D6-C770-47AF-B620-AF582ED5F32A}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "迷宫最短路径-BFS", "迷宫最短路径-BFS\迷宫最短路径-BFS.vcxproj", "{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -128,6 +132,22 @@ Global
{4B8A771F-ACB7-4044-86B3-E2E5CDA10742}.Release|x64.Build.0 = Release|x64
{4B8A771F-ACB7-4044-86B3-E2E5CDA10742}.Release|x86.ActiveCfg = Release|Win32
{4B8A771F-ACB7-4044-86B3-E2E5CDA10742}.Release|x86.Build.0 = Release|Win32
{253879D6-C770-47AF-B620-AF582ED5F32A}.Debug|x64.ActiveCfg = Debug|x64
{253879D6-C770-47AF-B620-AF582ED5F32A}.Debug|x64.Build.0 = Debug|x64
{253879D6-C770-47AF-B620-AF582ED5F32A}.Debug|x86.ActiveCfg = Debug|Win32
{253879D6-C770-47AF-B620-AF582ED5F32A}.Debug|x86.Build.0 = Debug|Win32
{253879D6-C770-47AF-B620-AF582ED5F32A}.Release|x64.ActiveCfg = Release|x64
{253879D6-C770-47AF-B620-AF582ED5F32A}.Release|x64.Build.0 = Release|x64
{253879D6-C770-47AF-B620-AF582ED5F32A}.Release|x86.ActiveCfg = Release|Win32
{253879D6-C770-47AF-B620-AF582ED5F32A}.Release|x86.Build.0 = Release|Win32
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Debug|x64.ActiveCfg = Debug|x64
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Debug|x64.Build.0 = Debug|x64
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Debug|x86.ActiveCfg = Debug|Win32
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Debug|x86.Build.0 = Debug|Win32
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Release|x64.ActiveCfg = Release|x64
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Release|x64.Build.0 = Release|x64
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Release|x86.ActiveCfg = Release|Win32
{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
134 changes: 134 additions & 0 deletions Lake_Counting_POJ2386-DFS/Lake_Counting_POJ2386-DFS.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{253879D6-C770-47AF-B620-AF582ED5F32A}</ProjectGuid>
<RootNamespace>LakeCountingPOJ2386DFS</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="问题.md" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="问题.md" />
</ItemGroup>
</Project>
46 changes: 46 additions & 0 deletions Lake_Counting_POJ2386-DFS/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<iostream>

using namespace std;

//深度优先搜索的方式来遍历一个水坑周围的所有地点,将水坑变为空地
void dfs(char field[][101], int i, int j, int N, int M)
{
//将水坑变为空地
field[i][j] = '.';

//遍历8个方向
for (int dx = -1;dx<=1;dx++)
for (int dy = -1; dy <= 1; dy++)
{
int nx = i + dx;
int ny = j + dy;
if (nx >= 1 && nx <= N && ny >= 1 && ny <= M && field[nx][ny] == 'W')
dfs(field, nx, ny, N, M);
}
return;
}

int main()
{
int N, M;
char field[101][101] = { '\0' };
cin >> N >> M;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
cin >> field[i][j];

int lake = 0;
//遍历所有水坑,有点水坑会在DFS过程中由于合并的需要被改写为空地
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
if(field[i][j] == 'W')
{
//使用DFS遍历一个水坑,及其相连的水坑
dfs(field, i, j, N, M);
lake++;
}

cout << lake << endl;

return 0;
}
49 changes: 49 additions & 0 deletions Lake_Counting_POJ2386-DFS/问题.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Lake Counting POJ 2386

http://poj.org/problem?id=2386

### Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.

关于水坑的相连:一个W周围的8个位置如果也是W,则可以连起来,算作一个水坑。因此想到了用穷竭搜索的方式来统计一个水坑,这里选择了用DFS,因为好写程序,栈的形式和递归函数调用很相似,如果用BFS,还要想存储结构。

### Input

\* Line 1: Two space-separated integers: N and M

\* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

### Output

\* Line 1: The number of ponds in Farmer John's field.

### Sample Input

```
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
```

### Sample Output

```
3
```

### Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.
5 changes: 4 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
| 项目名 | 描述 |
| ------ | ---- |
|平面最近点对-DC| 二维平面点集,寻找距离最近的两个点,使用分治法一分为二的解决 |
|01背包-DP||
|01背包-DP|01背包问题,用DP解决,有多种DP格式|
|钢条切割-DP||
|最优二叉搜索树-DP||
|最长单调递增子序列-DP||
|Lake_Counting_POJ2386-DFS|二维字符数组,一个W周围8个空间有W,则相连为一个水坑,对水坑计数。|
|Eight_Puzzle-BFS||
|迷宫最短路径-BFS|在一个迷宫里从起点到终点最少走多少步|
|Nqueens-局部快速搜索||
|木板切割开销_POJ3235-贪心Huffman|切割一块木板的花费等于木板长度,给定切割结果,求最小花费,等同于Huffman问题,用贪心算法求解|
|士兵布置_POJ3069-贪心|在一维数轴上,每个士兵有一定控制范围,要求给定的若干点必须被士兵控制,问最小士兵数,从数轴的一端开始用贪心算法放置士兵|
|字典序最小问题_POJ3617-贪心|从一个字符串S构造另一个字符串T,从S的开头或结尾删除一个字符,放在T的尾部。目标:T的字典序尽可能小。|
|||

12 changes: 8 additions & 4 deletions 注意事项.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
[TOC]

## 解题思路
1. 字典序比较类的问题常常用得上贪心法
1. 字典序比较类的问题常常用得上贪心法。
2. 深度优先搜索隐式地利用了栈进行计算,占用的内存空间与最大的递归深度有关;而宽度优先搜索则利用了队列,通常需要与状态数成正比的内存空间。一般认为深度优先搜索更省内存
3. 宽度优先搜索按照距开始状态由近及远的顺序搜索,因此可以很容易的用来求最短路径、最少操作之类的问题。适用于宽度优先搜索的求最短路径,应该具有单位路径长度,或者说求的是最短步数,如果路径带权重,要用Dijkstra等算法。
4.



Expand Down Expand Up @@ -37,6 +40,7 @@

1. C中没有整除运算,但是可以通过将结果赋值给整数型变量,或强制转换,得到整数结果,例如 int c = 5.6 / 2 ,结果为2,说明浮点数转换到整数是截断,不会舍入。
2. 数组也可以用sort函数排序,区间为左闭右开。
3. 模除运算得到的是0到模数-1,不会出现-1,因此不能写i % 80 == -1这种语句
4. 字符串逆序,有多种办法,可以借用rbegin和rend迭代器,见项目**字典序最小问题_POJ3617-贪心**
5. string类型字符串可以直接用<比较大小
3. 模除运算得到的是0到模数-1,不会出现-1,因此不能写i % 80 == -1这种语句。
4. 字符串逆序,有多种办法,可以借用rbegin和rend迭代器,见项目**字典序最小问题_POJ3617-贪心**
5. string类型字符串可以直接用<比较大小。
6. 有很多地方需要设置一个数为INF,但是不把INF当做例外,而是也参与普通运算的情况也很常见,这种情况下如果INF设置的过大会带来溢出的危险,一般会将INF设置为放大2~4倍也不会溢出的大小。
55 changes: 55 additions & 0 deletions 知识点.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,58 @@ str4 0987654321
str2 0987654321
```
其中reverse函数会将str2逆序,直接打印可以看到,不需要特别的头文件。
### stack
##### stack::push()
栈顶放入元素
##### stack::pop()
移除栈顶元素
##### stack::top()
访问栈顶元素
### queue
##### queue::push()
队列顶部放入元素
##### queue::pop()
移除队列底部的元素
##### queue::front()
访问队列底部的元素
### next_permutation函数
### 内存
##### 栈内存
栈内存在程序启动时统一分配,此后不能扩大,由于这一区域有上限,所以函数的递归深度也有上限。
##### 堆内存
如果需要申请巨大的数组,将其放置在堆内存上可以减少栈溢出的风险,通常只需定义满足最大需要的数列的大小。
Loading

0 comments on commit 119df30

Please sign in to comment.