Skip to content

Commit

Permalink
双色地砖行走_POJ1979-BFS_DFS
Browse files Browse the repository at this point in the history
  • Loading branch information
StupidRabbit29 committed Apr 20, 2020
1 parent 119df30 commit 200b885
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Algorithm.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lake_Counting_POJ2386-DFS",
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "迷宫最短路径-BFS", "迷宫最短路径-BFS\迷宫最短路径-BFS.vcxproj", "{3E14D7A9-4C77-4126-B9FB-99429AC9FD8A}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "双色地砖行走_POJ1979-BFS_DFS", "双色地砖行走_POJ1979-BFS_DFS\双色地砖行走_POJ1979-BFS_DFS.vcxproj", "{B6A8E036-6E8B-49E3-9E3F-63035F29564D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -148,6 +150,14 @@ Global
{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
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Debug|x64.ActiveCfg = Debug|x64
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Debug|x64.Build.0 = Debug|x64
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Debug|x86.ActiveCfg = Debug|Win32
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Debug|x86.Build.0 = Debug|Win32
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Release|x64.ActiveCfg = Release|x64
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Release|x64.Build.0 = Release|x64
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Release|x86.ActiveCfg = Release|Win32
{B6A8E036-6E8B-49E3-9E3F-63035F29564D}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
|最优二叉搜索树-DP||
|最长单调递增子序列-DP||
|Lake_Counting_POJ2386-DFS|二维字符数组,一个W周围8个空间有W,则相连为一个水坑,对水坑计数。|
|双色地砖行走_POJ1979-BFS_DFS|二维字符数组,其中有双色的地砖,一个人在起点出发只能走一种颜色,求一共可以走多少块地砖,使用BFS和DFS均可。|
|Eight_Puzzle-BFS||
|迷宫最短路径-BFS|在一个迷宫里从起点到终点最少走多少步|
|Nqueens-局部快速搜索||
Expand Down
44 changes: 44 additions & 0 deletions 双色地砖行走_POJ1979-BFS_DFS/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<iostream>
#include<queue>

using namespace std;

extern int W, H;
extern char room[20][21];
extern int sx, sy;
extern bool arrive[20][20];
extern int dx[4], dy[4];

typedef pair<int, int> P;
int BFS()
{
queue<P> que;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
arrive[i][j] = false;
arrive[sx][sy] = true;
que.push(P(sx, sy));
int ans = 1;

while (que.size())
{
P p = que.front();
que.pop();

for (int i = 0; i < 4; i++)
{
int nx = p.first + dx[i];
int ny = p.second + dy[i];

if (nx >= 0 && nx < H && ny >= 0 && ny < W && room[nx][ny] != '#' && arrive[nx][ny] == false)
{
que.push(P(nx, ny));
arrive[nx][ny] = true;
ans++;
}
}
}

return ans;
}

43 changes: 43 additions & 0 deletions 双色地砖行走_POJ1979-BFS_DFS/DFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<iostream>
#include<stack>

using namespace std;

extern int W, H;
extern char room[20][21];
extern int sx, sy;
extern bool arrive[20][20];
extern int dx[4], dy[4];

typedef pair<int, int> P;
int DFS()
{
stack<P> sta;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
arrive[i][j] = false;
arrive[sx][sy] = true;
sta.push(P(sx, sy));
int ans = 1;

while (sta.size())
{
P p = sta.top();
sta.pop();

for (int i = 0; i < 4; i++)
{
int nx = p.first + dx[i];
int ny = p.second + dy[i];

if (nx >= 0 && nx < H && ny >= 0 && ny < W && room[nx][ny] != '#' && arrive[nx][ny] == false)
{
sta.push(P(nx, ny));
arrive[nx][ny] = true;
ans++;
}
}
}

return ans;
}
42 changes: 42 additions & 0 deletions 双色地砖行走_POJ1979-BFS_DFS/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<iostream>
#include<queue>
#include<stack>

using namespace std;

int W, H;
char room[20][21];
int sx, sy;
bool arrive[20][20];
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };

int BFS();
int DFS();

int main()
{
while (true)
{
cin >> W >> H;
if (W == 0 && H == 0)
break;

for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
{
cin >> room[i][j];
if (room[i][j] == '@')
{
sx = i;
sy = j;
}
}

//int ans = BFS();
int ans = DFS();
cout << ans << endl;
}

return 0;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?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>{B6A8E036-6E8B-49E3-9E3F-63035F29564D}</ProjectGuid>
<RootNamespace>双色地砖行走POJ1979BFSDFS</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>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</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="BFS.cpp" />
<ClCompile Include="DFS.cpp" />
<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,31 @@
<?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="BFS.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="DFS.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="问题.md" />
</ItemGroup>
</Project>
Loading

0 comments on commit 200b885

Please sign in to comment.