diff --git a/Algorithm.sln b/Algorithm.sln index 68c84ca..a8fca0d 100644 --- a/Algorithm.sln +++ b/Algorithm.sln @@ -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 @@ -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 diff --git a/Readme.md b/Readme.md index c4c3561..06abbba 100644 --- a/Readme.md +++ b/Readme.md @@ -8,6 +8,7 @@ |Ŷ-DP|| |-DP|| |Lake_Counting_POJ2386-DFS|άַ飬һWΧ8ռWΪһˮӣˮӼ| +|˫ɫש_POJ1979-BFS_DFS|άַ飬˫ɫĵשһֻһɫһ߶ٿשʹBFSDFSɡ| |Eight_Puzzle-BFS|| |Թ·-BFS|һԹ㵽յ߶ٲ| |Nqueens-ֲ|| diff --git "a/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/BFS.cpp" "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/BFS.cpp" new file mode 100644 index 0000000..79ea7f7 --- /dev/null +++ "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/BFS.cpp" @@ -0,0 +1,44 @@ +#include +#include + +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 P; +int BFS() +{ + queue

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; +} + diff --git "a/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/DFS.cpp" "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/DFS.cpp" new file mode 100644 index 0000000..0b11063 --- /dev/null +++ "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/DFS.cpp" @@ -0,0 +1,43 @@ +#include +#include + +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 P; +int DFS() +{ + stack

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; +} \ No newline at end of file diff --git "a/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/main.cpp" "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/main.cpp" new file mode 100644 index 0000000..2052316 --- /dev/null +++ "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/main.cpp" @@ -0,0 +1,42 @@ +#include +#include +#include + +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; +} + diff --git "a/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS.vcxproj" "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS.vcxproj" new file mode 100644 index 0000000..a567cba --- /dev/null +++ "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS.vcxproj" @@ -0,0 +1,136 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {B6A8E036-6E8B-49E3-9E3F-63035F29564D} + 双色地砖行走POJ1979BFSDFS + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + + + Console + + + + + Level3 + Disabled + true + true + + + Console + + + + + Level3 + MaxSpeed + true + true + true + true + + + Console + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + Console + true + true + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS.vcxproj.filters" "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS.vcxproj.filters" new file mode 100644 index 0000000..30238b3 --- /dev/null +++ "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS.vcxproj.filters" @@ -0,0 +1,31 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + 源文件 + + + + + + \ No newline at end of file diff --git "a/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\351\227\256\351\242\230.md" "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\351\227\256\351\242\230.md" new file mode 100644 index 0000000..5f2c02a --- /dev/null +++ "b/\345\217\214\350\211\262\345\234\260\347\240\226\350\241\214\350\265\260_POJ1979-BFS_DFS/\351\227\256\351\242\230.md" @@ -0,0 +1,78 @@ +# Red and Black POJ 1979 + +http://poj.org/problem?id=1979 + +### Description + +There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles. + +Write a program to count the number of black tiles which he can reach by repeating the moves described above. + +### 解题方法 + +BFS和DFS均可 + +### Input + +The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20. + +There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows. + +'.' - a black tile +'#' - a red tile +'@' - a man on a black tile(appears exactly once in a data set) +The end of the input is indicated by a line consisting of two zeros. + +### Output + +For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself). + +### Sample Input + +``` +6 9 +....#. +.....# +...... +...... +...... +...... +...... +#@...# +.#..#. +11 9 +.#......... +.#.#######. +.#.#.....#. +.#.#.###.#. +.#.#..@#.#. +.#.#####.#. +.#.......#. +.#########. +........... +11 6 +..#..#..#.. +..#..#..#.. +..#..#..### +..#..#..#@. +..#..#..#.. +..#..#..#.. +7 7 +..#.#.. +..#.#.. +###.### +...@... +###.### +..#.#.. +..#.#.. +0 0 +``` + +### Sample Output + +``` +45 +59 +6 +13 +``` \ No newline at end of file