Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 10026번 적록색약 (gold 5 본문
import java.util.*;
import java.io.*;
class Point{
int x;
int y;
char stat;
public Point(int x, int y, char stat){
this.x=x;
this.y=y;
this.stat=stat;
}
}
public class Main{
static int[] numx={-1,1,0,0};
static int[] numy={0,0,-1,1};
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int n=Integer.valueOf(br.readLine());
char[][] map=new char[n+1][n+1];
for(int i=1; i<=n; i++){
String a=br.readLine();
for(int j=1; j<=n; j++){
map[i][j]=a.charAt(j-1);
}
}
Queue<Point> que=new LinkedList<>();
boolean[][] visit=new boolean[n+1][n+1];
int answer1=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(!visit[i][j]){
answer1++;
visit[i][j]=true;
que.offer(new Point(j,i,map[i][j]));
while(!que.isEmpty()){
Point temp=que.poll();
for(int k=0; k<4; k++){
int tpx=temp.x+numx[k];
int tpy=temp.y+numy[k];
if(tpx<1 || tpx>n || tpy<1 || tpy>n || visit[tpy][tpx] || temp.stat != map[tpy][tpx]){
continue;
}
visit[tpy][tpx]=true;
que.offer(new Point(tpx,tpy, temp.stat));
}
}
}
}
}
que.clear();
visit=new boolean[n+1][n+1];
int answer2=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(!visit[i][j]){
answer2++;
visit[i][j]=true;
que.offer(new Point(j,i,map[i][j]));
while(!que.isEmpty()){
Point temp=que.poll();
for(int k=0; k<4; k++){
int tpx=temp.x+numx[k];
int tpy=temp.y+numy[k];
if(tpx<1 || tpx>n || tpy<1 || tpy>n || visit[tpy][tpx] || (temp.stat=='B' || map[tpy][tpx]=='B') && (temp.stat != map[tpy][tpx])){
continue;
}
visit[tpy][tpx]=true;
que.offer(new Point(tpx,tpy, map[tpy][tpx]));
}
}
}
}
}
bw.write(answer1+" "+answer2);
bw.flush();
}
}
'백준공부 > java' 카테고리의 다른 글
[백준] 2630번 색종이 만들기 (silver 2 (0) | 2023.12.24 |
---|---|
[백준] 1647번 도시 분할 계획 (gold 4 (0) | 2023.12.24 |
[백준] 2225번 합분해 (gold 5 (0) | 2023.12.21 |
[백준] 1806번 부분합 (gold 4 (0) | 2023.12.20 |
[백준] 18110번 solved.ac (silver 4 (2) | 2023.12.20 |