Skip to content

Commit a9ebba1

Browse files
committed
srm 211 div 1 med
1 parent e3b2ebd commit a9ebba1

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

grafixMask.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <map>
77
#include <queue>
88
using namespace std;
9+
typedef pair<int, int> pi;
910
int c = 0;
1011
int visited[405][605];
1112
int X[4] = {1,-1,0,0};
@@ -14,17 +15,6 @@ int Y[4] = {0,0,1,-1};
1415
class grafixMask
1516
{
1617
public:
17-
void dfs(int x, int y){
18-
visited[x][y] = 1;
19-
c++;
20-
for(int i = 0; i < 4; i++){
21-
int xx = x+X[i];
22-
int yy = y+Y[i];
23-
if(0 <= xx && xx < 400 && 0 <= yy && yy < 600 && !visited[xx][yy])
24-
dfs(xx, yy);
25-
}
26-
}
27-
2818
vector <int> sortedAreas(vector <string> rec){
2919

3020
for(int i = 0; i < 400; i++)
@@ -41,11 +31,28 @@ class grafixMask
4131
for(int i = 0; i < 400; i++)
4232
for(int j = 0; j < 600; j++){
4333
if(!visited[i][j]){
44-
c = 0;
45-
dfs(i, j);
34+
c = 1;
35+
queue<pi> Q;
36+
Q.push(make_pair(i, j));
37+
while(!Q.empty()){
38+
pi p = Q.front();
39+
Q.pop();
40+
int x = p.first, y = p.second;
41+
visited[x][y] = 1;
42+
for(int i = 0; i < 4; i++){
43+
int xx = x+X[i];
44+
int yy = y+Y[i];
45+
if(0 <= xx && xx < 400 && 0 <= yy && yy < 600 && !visited[xx][yy]){
46+
Q.push(make_pair(xx, yy));
47+
visited[xx][yy] = 1;
48+
c++;
49+
}
50+
}
51+
}
4652
rc.push_back(c);
4753
}
4854
}
55+
4956
sort(rc.begin(), rc.end());
5057
return rc;
5158
}

0 commit comments

Comments
 (0)