-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoj2815_stack.cpp
57 lines (53 loc) · 1.28 KB
/
Poj2815_stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <stack>
using namespace std;
#define MAXN 50
int mark[MAXN][MAXN];
int rooms[MAXN][MAXN];
int R, C;
int roomNum, maxArea, area;
void Dfs(int i, int j)
{
struct Room
{
int r, c;
Room(int rr, int cc):
r(rr), c(cc) {};
};
stack<Room> stk;
stk.push(Room(i, j));
while (!stk.empty()) {
Room rm = stk.top();
int i = rm.r, j = rm.c;
if (mark[i][j])
stk.pop();
else {
area++;
mark[i][j] = 1;
if ((rooms[i][j] & 1) == 0)
stk.push(Room(i, j - 1));
if ((rooms[i][j] & 2) == 0)
stk.push(Room(i - 1, j));
if ((rooms[i][j] & 4) == 0)
stk.push(Room(i, j + 1));
if ((rooms[i][j] & 8) == 0)
stk.push(Room(i + 1, j));
}
}
}
int main()
{
cin >> R >> C;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
cin >> rooms[i][j];
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
if (!mark[i][j]) {
roomNum++;
area = 0;
Dfs(i, j);
maxArea = max(area, maxArea);
}
cout << roomNum << endl << maxArea << endl;
}