DFS之城堡问题
2019-06-01
17:54:51
坚持!!
题目链接:
http://bailian.openjudge.cn/practice/2815
#include <bits/stdc++.h> using namespace std; int n = 0, m = 0; int roomNumber = 0; int roomSize = 0; //当前房间的面积大小 int maxRoomSize = 0; int room[100][100]; int color[100][100]; void dfs(int x, int y); int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &room[i][j]); } } for(int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(color[i][j] == 0) { roomNumber++; roomSize = 0; dfs(i, j); maxRoomSize = max(maxRoomSize, roomSize); } } } cout << roomNumber << endl; cout << maxRoomSize << endl; return 0; } void dfs(int x, int y) { if(color[x][y] != 0) { return; } roomSize++; color[x][y] = roomNumber; if((room[x][y] & 1) == 0) dfs(x, y - 1); //向西走 if((room[x][y] & 2) == 0) dfs(x - 1, y); //向北走 if((room[x][y] & 4 )== 0) dfs(x, y + 1); //向东走 if((room[x][y] & 8) == 0) dfs(x + 1, y); //向南走 }