DFS之城堡问题
Posted artimis-fightting
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); //向南走
以上是关于DFS之城堡问题的主要内容,如果未能解决你的问题,请参考以下文章