Skip to content

Instantly share code, notes, and snippets.

@niklasjang
Created August 17, 2020 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklasjang/ef68cfd8c08f7fb53dc60bfd92578ae4 to your computer and use it in GitHub Desktop.
Save niklasjang/ef68cfd8c08f7fb53dc60bfd92578ae4 to your computer and use it in GitHub Desktop.
[PS][java][완전탐색][BFS]/[적록색약]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int n;
static char[][] map, map2;
static boolean[][] visit;
static int ans=0,ans2=0;
static int[] dx = {0,0,1,-1};
static int[] dy = {1,-1,0,0};
static Queue<Point> q;
static class Point{
int x,y;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
static boolean check(int i, int j){
if(i <0 || n<= i) return false;
if(j <0 || n<= j) return false;
return true;
}
static void bfs(char[][] map, int i, int j){
visit[i][j] = true;
q.offer(new Point(i,j));
while(!q.isEmpty()){
Point curr = q.poll();
for(int k=0; k<4; k++){
int nx = curr.x + dx[k];
int ny = curr.y + dy[k];
if(!check(nx,ny)) continue;
if(map[i][j] != map[nx][ny]) continue;
if(visit[nx][ny]) continue;
visit[nx][ny] = true;
q.offer(new Point(nx,ny));
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
map = new char[n][n];
map2 = new char[n][n];
q = new LinkedList<>();
for(int i=0; i<n; i++){
String line = br.readLine();
for(int j=0; j<n; j++){
map[i][j] = line.charAt(j);
map2[i][j] = map[i][j];
if(map[i][j] == 'G'){
map2[i][j] = 'R';
}
}
}
visit = new boolean[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(visit[i][j])continue;
bfs(map,i,j);
ans++;
}
}
visit = new boolean[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(visit[i][j])continue;
bfs(map2,i,j);
ans2++;
}
}
System.out.println(ans+" "+ans2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment