7 7
0 0 0 0 0 0 0
0 1 1 1 1 2 0
0 3 3 1 2 2 0
0 4 3 1 2 2 0
0 5 5 5 5 0 0
0 5 6 6 6 6 0
0 0 0 0 0 0 0

the count of 4blocks is 7

#include <iostream> #include <vector> using namespace std; #define ROW 100
#define COL 100 int g_map[ROW][COL] = { 0 }; int g_ergodic[ROW][COL] = { 0 };
// 遍历 int g_count = 0; void draw_map(int row, int col) { if (row < 1 || col <
1) { cout << "输入不合法"; exit(1); } for (int i = 0; i < row; ++i) { for (int j =
0; j < col; ++j) { cin >> g_map[i][j]; } } } void dfs_4blocks(int x, int y, int
row, int col) { if (g_ergodic[x][y] == 0) { ++g_count; g_ergodic[x][y] = -1; }
if (y + 1 < col && g_map[x][y] == g_map[x][y + 1] && g_ergodic[x][y + 1] == 0)
{ g_ergodic[x][y + 1] = -1; dfs_4blocks(x, y + 1, row, col); } if (y - 1 >= 0
&& g_map[x][y] == g_map[x][y - 1] && g_ergodic[x][y - 1] == 0) { g_ergodic[x][y
- 1] = -1; dfs_4blocks(x, y - 1, row, col); } if (x + 1 < row && g_map[x][y] ==
g_map[x + 1][y] && g_ergodic[x + 1][y] == 0) { g_ergodic[x + 1][y] = -1;
dfs_4blocks(x + 1, y, row, col); } if (x - 1 >= 0 && g_map[x][y] == g_map[x -
1][y] && g_ergodic[x - 1][y] == 0) { g_ergodic[x - 1][y] = -1; dfs_4blocks(x -
1, y, row, col); } } int main() { int row, col; cin >> row >> col;
draw_map(row, col); for (int i = 0; i < row; ++i) { for (int j = 0; j < col;
++j) { dfs_4blocks(i, j, row, col); } } cout << "the count of 4blocks is " <<
g_count << endl; return 0; }

GitHub

Gitee