dfs_百炼 2815 陈堡 (从深度优先遍历做了什么来理解深度优先遍历)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> #include <algorithm> #include <stdlib.h> #include <vector> #include <map> #include <queue> #include <string> #include <iostream> #include <ctype.h> #include <string.h> #include <set> #include <stack> #include<functional> using namespace std; #define Size 22 #define maxn 1<<30 #define minn 1e-6 const int dir[4][2] = { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 } }; int r, c, rooms = 0, max_area = 0, area; int wall[Size][Size], visit[Size][Size]; void dfs(int i, int j) { area++; visit[i][j] = true;//进行深搜的目的是为了1,标记这一个块,2面积++; for (int t = 0; t<4; t++) if (!visit[i + dir[t][0]][j + dir[t][1]] && !(wall[i][j] & (1 << t))) dfs(i + dir[t][0], j + dir[t][1]); return; } int main() { scanf("%d%d\n", &r, &c); for (int i = 0; i<r; i++) for (int j = 0; j<c; j++) scanf("%d", &wall[i][j]); for (int i = 0; i<r; i++) for (int j = 0; j<c; j++) if (!visit[i][j]) { rooms++; area = 0; dfs(i, j); if (area>max_area) max_area = area; } printf("%d\n%d\n", rooms, max_area); return 0; }