-
Notifications
You must be signed in to change notification settings - Fork 0
/
oil.cpp
101 lines (98 loc) · 1.7 KB
/
oil.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include<queue>
#include<map>
using namespace std;
int vis[255][255];
int count,m,n;
map<int,int> mp;
int a[300][300];
bool check(int x,int y)
{
if(x >= 0 && x < m && y >= 0 && y < n)
return true;
else
return false;
}
void mat(int x,int y)
{
queue<int> q;
q.push(x);
q.push(y);
vis[x][y] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
int v = q.front();
q.pop();
//cout << "popped value is " << u << " , " << v << endl;
count++;
//vis[u][v] = 1;
if(check(u,v+1)) {
if(a[u][v+1] == 1 && vis[u][v+1] == 0) {
q.push(u);
q.push(v+1);
vis[u][v+1] = 1;
}
}
if(check(u,v-1)) {
if(a[u][v-1] == 1 && vis[u][v-1] == 0) {
q.push(u);
q.push(v-1);
vis[u][v-1] = 1;
}
}
if(check(u-1,v)) {
if(a[u-1][v] == 1 && vis[u-1][v] == 0) {
q.push(u-1);
q.push(v);
vis[u-1][v] = 1;
}
}
if(check(u+1,v)) {
if(a[u+1][v] == 1 && vis[u+1][v] == 0) {
q.push(u+1);
q.push(v);
vis[u+1][v] = 1;
}
}
}
}
int main()
{
while(1) {
for(int i = 0;i < 255;i++) {
for(int j = 0;j < 255;j++) {
vis[i][j] = 0;
}
}
int ans = 0;
map<int,int>::iterator it;
cin >> m >> n;
if(m == 0 && n == 0) {
break;
}else {
for(int i = 0;i < m;i++) {
for(int j = 0;j < n;j++) {
cin >> a[i][j];
}
}
for(int i = 0;i < m;i++) {
for(int j = 0;j < n;j++) {
if(a[i][j] == 1 && vis[i][j] == 0) {
//cout << "start nodes are " << i << " and " << j << endl;
count = 0;
mat(i,j);
mp[count]++;
ans++;
}
}
}
}
cout << ans << endl;
for(it = mp.begin();it != mp.end();it++) {
cout << (*it).first << " " << (*it).second << endl;
}
mp.clear();
}
return 0;
}