include bits stdc using namespace std define MAXN 500 define FOR for i

 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
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
#define MAXN 500
#define FOR(i, s, e) for(int i=s; i<=e; i++)
int maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
vector<int> rooms;
int curComp = 0;
int t, r, c;
class fastio {
public:
fastio() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
}
} __fastio;
bool notSafe(int x, int y) {
if(x < 1 || x > r || y < 1 || y > c) return true;
}
void dfs(int i, int j) {
if(!notSafe(i, j) && maze[i][j] == 0) {
vis[i][j] = true;
curComp++;
if(!vis[i+1][j]) dfs(i+1, j);
if(!vis[i][j+1]) dfs(i, j+1);
if(!vis[i-1][j]) dfs(i-1, j);
if(!vis[i][j-1]) dfs(i, j-1);
}
}
void input() {
cin >> t >> r >> c;
FOR(i, 1, r) {
FOR(j, 1, c) {
char ch; cin >> ch;
if(ch == 'I') maze[i][j] = 1;
else maze[i][j] = 0;
}
}
}
void solve() {
input();
FOR(i, 1, r) {
FOR(j, 1, c) {
if(maze[i][j] == 0 && vis[i][j] == false) {
curComp = 0;
dfs(i, j);
rooms.push_back(curComp);
}
}
}
sort(rooms.begin(), rooms.end(), greater<int>());
//for(int i : rooms) cout << i << endl;
int cnt = 0, sum = 0;
for(int i : rooms) {
if(t >= i) {
cnt++;
t -= i;
} else {
break;
}
}
cout << cnt << " rooms, " << t << " square metre(s) left over" << endl;
}
int main() {
__fastio;
solve();
return 0;
}