[dfs] [FloodFill] POJ1979 Red And Black

分析题目:

用dfs实现FloodFill。

@和.实际上都是表示该格可走,#则表示该格不能走,想到用 bool map[i][j] 存储是否可走。

用visit[i][j]表示是否已经访问过。

从一个黑格只能到达上,下,左,右四块相邻的格,该格应该未访问过且该格不能是红格。

inmap(i, j)判断该格是否在map中。

解题代码:

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4
 5 bool map[22][22];
 6 bool visit[22][22];
 7 int answer;
 8 int W, H;                              // W->cols, H->rows
 9 int dr[4] = {0, 1, -1, 0};
10 int dc[4] = {-1, 0, 0, 1};
11
12 bool inmap(int r, int c)
13 {
14     if(r>=1 && r<=H && c>=1 && c<=W)
15         return true;
16     else
17         return false;
18 }
19
20 void dfs(int now_r, int now_c)
21 {
22     for(int i=0; i<4; i++)
23     {
24         int r = now_r + dr[i];
25         int c = now_c + dc[i];
26         if(inmap(r,c) && map[r][c] && !visit[r][c])
27         {
28             answer++;
29             visit[r][c] = true;
30             dfs(r, c);
31         }
32
33     }
34 }
35
36 int main()
37 {
38     int i, j, start_r, start_c;
39     while( cin >> W >> H)
40     {
41         if(W == 0 && H == 0)
42             break;
43         memset(visit, false, sizeof(visit));
44
45         for(i=1; i<=H; i++)
46             for(j=1; j<=W; j++)
47             {
48                 char c;
49                 cin >> c;
50                 if(c == ‘.‘)
51                     map[i][j] = true;
52                 else
53                 {
54                     if(c == ‘#‘)
55                         map[i][j] = false;
56                     else
57                     {
58                         map[i][j] = true;
59                         visit[i][j] = true;
60                         start_r = i;
61                         start_c = j;
62                     }
63                 }
64             }
65         answer = 1;
66         dfs(start_r, start_c);
67         cout << answer << endl;
68     }
69
70     return 0;
71 } 
时间: 2024-07-30 13:53:15

[dfs] [FloodFill] POJ1979 Red And Black的相关文章

POJ1979 Red and Black

问题链接:POJ1979 Red and Black. 题意简述:输入正整数w和h,w为列数,h为行数.输入h×w矩阵 (1 <= h <= 20; 1 <= w <= w),其中'.'代表可到达,'#'代表不可到达,'@'代表开始点.问从'@'开始可以到达最多多少个点. 问题分析:本题可以使用深度优先搜索求解,用广度优先搜索也可以求解,差别不大.需要注意的是'@'也算一个可以到达的点. 程序说明如下: 1.方向数组 使用方向数组后,各个方向的试探的程序就会变得简洁了,用循环处理即

DFS深搜-Red and Black

深搜,从一点向各处搜找到所有能走的地方. Problem 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

POJ1979 Red and Black (简单DFS)

POJ1979 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 mo

poj1979 Red and Black(DFS)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25797   Accepted: 13967 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

11.17 dfs poj1979 Red and Black

---恢复内容开始--- https://www.cnblogs.com/OctoptusLian/p/7429645.html 解救小哈--dfs入门 //果然博客园质量高丫 #include<iostream>#include<cstdio>#include<queue>using namespace std;int dx[] = {0,-1,0,1};int dy[] = {-1,0,1,0};int ans,n,m;char a[25][25];void dfs

[DFS] &amp; [BFS] poj1979 poj3009 poj3669

都比较简单,直接贴代码吧. poj1979 DFS 题目大意:给你一个二维数组,.表示可以到达,#表示障碍,@表示起始位置,问你能到达的最大地点有多少个,每次只能走上下左右 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int n, m, sx, sy, ans; int pd[30][30]; char maze[30][30]; int dx[4] = {0

poj1979 Red and Black【搜索】

点击打开题目 Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23194   Accepted: 12513 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.

poj-1979 red and black(搜索)

Time limit1000 ms Memory limit30000 kB 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 r

Oil Deposits DFS FloodFill漫水填充法求连通块问题

Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It