[BOJ] 2667 단지번호붙히기

Time Lapse :NONE

2667.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
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string>
#include <queue>
using namespace std;

string map[100];
bool visit[100][100];
int N;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
vector<int> ans;

void BFS()
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
if(!visit[i][j]&&map[i][j]=='1')
{
queue<pair<int,int>> q;
q.push(make_pair(i,j));
visit[i][j] = true;
int cnt=0;
while(!q.empty())
{
cnt++;
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int k=0;k<4;k++)
{
int next_x = x+dx[k];
int next_y = y+dy[k];
if(next_x<0||next_x>N||next_y<0||next_y>N)
continue;
if(!visit[next_x][next_y]&&map[next_x][next_y]=='1')
{
q.push(make_pair(next_x,next_y));
visit[next_x][next_y]=true;
}
}
}
ans.push_back(cnt);
}
}
}

int main(void)
{

memset(visit,false,sizeof(visit));
cin>>N;
for(int i=0;i<N;i++)
cin>>map[i];
BFS();
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<endl;
}

2667.java

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
102
103
104
105
import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
static String[] map = new String[25];
static boolean[][] v = new boolean[25][25];
static int[] ans = new int[625];
static int[] dx = {0, 1, 0, -1};
static int[] dy = {-1, 0, 1, 0};
static Queue<pair> q = new LinkedList<>();
static int n, g;
public static void main(String[] args) throws IOException {
Reader r = new Reader();
n = r.nextInt();
for(int i = 0; i < n; ++i)
map[i] = r.readLine();

for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j) {
if(map[i].charAt(j) == '0' || v[i][j]) continue;
q.add(new pair(i, j));
v[i][j] = true;
int cnt = 1;
while(!q.isEmpty()){
pair p = q.remove();
for(int k = 0; k < 4; ++k) {
int nx = p.s + dx[k];
int ny = p.f + dy[k];
if(0<= nx && nx < n && 0 <= ny && ny < n && !v[ny][nx] && map[ny].charAt(nx) == '1'){
q.add(new pair(ny, nx));
v[ny][nx] = true;
++cnt;
}
}
}
ans[g++] = cnt;
}

Arrays.sort(ans, 0, g);
System.out.println(g);
for(int i = 0; i < g; ++i)
System.out.println(ans[i]);
}

static class pair{
public int f, s;
public pair(int f, int s){
this.f = f;
this.s = s;
}
}

static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;

public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public int nextInt() throws IOException {
int ret = 0, flag = 1;
byte c = read();
if(c == '-') {
flag = -1;
c = read();
}
do {
ret = (ret<<3) + (ret<<1) + (c & 0b1111);
} while ((c = read()) >= '0' && c <= '9');

return ret * flag;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}


private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}

private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2667/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.