πAlgorithm/π₯Baekjoon
[Baekjoon] 1303_μ μ - μ ν¬
λΏμΌ._.
2022. 4. 26. 12:15
λ¬Έμ (μΆμ²: https://www.acmicpc.net/problem/1303)
<μ μ-μ ν¬>
λ¬Έμ
μ μμ μ΄λλ§ μ λ©΄μ μ΄ μμλμλ€. κ²°κ΅ μ ν¬λ λμ μ΄ λμκ³ , μ°λ¦¬ λ³μ¬μ μ κ΅ λ³μ¬κ° μμ¬ μΈμ°κ² λμλ€. κ·Έλ¬λ λΉμ μ λ³μ¬λ€μ ν°μ μ·μ μ κ³ , μ κ΅μ λ³μ¬λ€μ νλμ μ·μ μ μκΈ° λλ¬Έμ μλ‘κ° μ μΈμ§ μκ΅°μΈμ§λ ꡬλΆν μ μλ€. λ¬Έμ λ κ°μ νμ λ³μ¬λ€μ λͺ¨μ΄λ©΄ λͺ¨μΌμλ‘ κ°ν΄μ§λ€λ μ¬μ€μ΄λ€.
Nλͺ μ΄ λμ³μμ λλ N2μ μλ ₯μ λΌ μ μλ€. κ³Όμ° μ§κΈ λμ μ μν©μμλ λκ° μΉλ¦¬ν κ²μΈκ°? λ¨, κ°μ νμ λ³μ¬λ€μ΄ λκ°μ μΌλ‘λ§ μΈμ ν κ²½μ°λ λμ³ μλ€κ³ λ³΄μ§ μλλ€.
μ λ ₯
첫째 μ€μλ μ μν°μ κ°λ‘ ν¬κΈ° N, μΈλ‘ ν¬κΈ° M(1 ≤ N, M ≤ 100)μ΄ μ£Όμ΄μ§λ€. κ·Έλ€μ λ λ²μ§Έ μ€μμ M+1λ²μ§Έ μ€μλ κ°κ° (X, Y)μ μλ λ³μ¬λ€μ μ·μμ΄ λμ΄μ°κΈ° μμ΄ μ£Όμ΄μ§λ€. λͺ¨λ μ리μλ λ³μ¬κ° ν λͺ μλ€. Bλ νλμ, Wλ ν°μμ΄λ€. λΉμ μ λ³μ¬μ μ κ΅μ λ³μ¬λ ν λͺ μ΄μ μ‘΄μ¬νλ€.
μΆλ ₯
첫 λ²μ§Έ μ€μ λΉμ μ λ³μ¬μ μλ ₯μ ν©κ³Ό μ κ΅μ λ³μ¬μ μλ ₯μ ν©μ μΆλ ₯νλ€.
λ¬Έμ νμ΄
- my solution (Python)
import sys
from collections import deque
def bfs(arr, visited, start, find):
dx=[-1,1,0,0] # μνμ’μ°
dy=[0,0,-1,1]
queue=deque([start])
visited[start[0]][start[1]]=1
cnt=1
while queue:
temp=queue.popleft()
for i in range(4):
x=temp[0]+dx[i]
y=temp[1]+dy[i]
if 0<=x<len(arr) and 0<=y<len(arr[0]):
if arr[x][y]==find and visited[x][y]==0:
visited[x][y]=1
queue.append([x,y])
cnt+=1
return cnt
if __name__=='__main__':
n,m=map(int,sys.stdin.readline().split()) # κ°λ‘, μΈλ‘
arr=[]
for i in range(m):
arr.append(list(sys.stdin.readline().strip()))
visited_w=[[0]*n for i in range(m)]
visited_b = [[0] * n for i in range(m)]
w_cnt=0
b_cnt=0
for i in range(m):
for j in range(n):
if arr[i][j]=='W' and visited_w[i][j]==0:
cnt=bfs(arr, visited_w, [i,j], 'W')
w_cnt+=(cnt**2)
elif arr[i][j]=='B' and visited_b[i][j]==0:
cnt=bfs(arr, visited_b, [i,j], 'B')
b_cnt+=(cnt**2)
print(w_cnt, end=' ')
print(b_cnt)
- my solution (JAVA)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class _1303_μ μ_μ ν¬ {
static int bfs(String[][] arr, int[][] visited, int[] start, String find) {
int dx[]= {-1,1,0,0}; // μνμ’μ°
int dy[]= {0,0,-1,1};
visited[start[0]][start[1]]=1;
Queue<int[]> queue=new LinkedList<>();
queue.add(start);
int cnt=1;
while(queue.size()!=0) {
int temp[]=queue.poll();
for(int i=0; i<4; i++) {
int x=temp[0]+dx[i];
int y=temp[1]+dy[i];
if(x>=0 && x<arr.length && y>=0 && y<arr[0].length) { // λ²μ μ
if(arr[x][y].equals(find) && visited[x][y]==0) {
visited[x][y]=1;
queue.add(new int[] {x,y});
cnt+=1;
}
}
}
}
return cnt;
}
public static void main(String[] args) throws IOException{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String s=bf.readLine();
int n=Integer.parseInt(s.split(" ")[0]); // κ°λ‘
int m=Integer.parseInt(s.split(" ")[1]); // μΈλ‘
String arr[][]=new String[m][n];
for(int i=0; i<m; i++) {
s=bf.readLine();
for(int j=0; j<n; j++) {
arr[i][j]=s.split("")[j];
}
}
int visited_w[][]=new int[m][n]; // λ°©λ¬Έ μ¬λΆ
int visited_b[][]=new int[m][n];
int cnt_w=0;
int cnt_b=0;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(arr[i][j].equals("W") && visited_w[i][j]==0) {
int cnt=bfs(arr, visited_w, new int[] {i,j}, "W");
cnt_w+=(cnt*cnt);
}
if(arr[i][j].equals("B") && visited_b[i][j]==0) {
int cnt=bfs(arr, visited_b, new int[] {i,j}, "B");
cnt_b+=(cnt*cnt);
}
}
}
System.out.println(cnt_w +" "+ cnt_b);
}
}
μνμ’μ°λ₯Ό νμΈνμ¬ μμ κ³Ό κ°μ νμ΄λΌλ©΄ λͺ λͺ μΈμ§ ꡬν ν N μ κ³±μ λν΄μ€λλ€.
- bfs ( μ
λ ₯ μ 보, λ°©λ¬Έ μ¬λΆ, μμ μμΉ, μ°Ύλ κ° 'W' or 'B' )
: μνμ’μ°λ₯Ό νμνμ¬ νμ¬ μμΉμ κ°κ³Ό κ°μ κ°μ΄λΌλ©΄ cntλ₯Ό μ¦κ°μμΌμ£Όλ©° κ³μν΄μ νμν΄μ€λλ€. - Main
: κ°λ‘, μΈλ‘ μ λ ₯
: arr μ 보 μ λ ₯
: visited_w, visited_b = λ°©λ¬Έ μ¬λΆ
: w_cnt, b_cnt = λ³μ¬μ μλ ₯
: arr μ 체λ₯Ό μννλ©° 'W'μΌ λμ 'B'μΌ λ bfs νμ
-> λ°ν κ° = λμ³ μλ μ μ΄λ―λ‘ κ° cntμ μ κ³±ν κ°μ λν¨
: w_cnt, b_cnt μΆλ ₯
β λ°νμ μλ¬?
λ¬Έμ μμ κ°λ‘, μΈλ‘ μμΌλ‘ μ λ ₯λ°μλλ° μ²μμ μ½λ μμ± μ μΈλ‘, κ°λ‘λ‘ νμ©νκΈ° λλ¬Έμ μΈλ±μ€ μλ¬κ° λ°μνμλ€.
μκ°π€
BFSλ₯Ό μ¬μ©νμ¬ μ½κ² ꡬν μ μλ λ¬Έμ μλ€.