본문 바로가기
알고리즘 & 자료구조/백준

백준 6186

by 신재권 2021. 12. 20.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main6186 {
	
	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0,0, -1, 1};

	static int R, C;

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		R = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		
		char[][] map = new char[R][C];
		for(int i=0; i<R; i++) {
			String str = br.readLine();
			for(int j=0; j<C; j++) {
				map[i][j] = str.charAt(j);
			}
		}
		boolean[][] check = new boolean[R][C];
		int cnt = 0;
		for(int i=0; i<R; i++) {
			for(int j=0; j<C; j++) {
				if(!check[i][j] && map[i][j] == '#') {
					check[i][j] = true;
					for(int k=0; k<4; k++) {  //유효한 좌표이면서 , 풀이면서,체크한 풀이 아니라면 
						if( valid(i+dx[k], j+dy[k]) && map[i+dx[k]][j+dy[k]] == '#' &&!check[i+dx[k]][j+dy[k]] ) {  
							check[i+dx[k]][j+dy[k]] = true; //붙어있는 것으로 간
						}
					}
					cnt++;
				}		
			}
		}
		
		System.out.println(cnt);
		
		
	}

	static boolean valid(int x, int y) {
		if(x < 0 || x > R-1 || y < 0 || y > C-1) {
			return false;
		}
		return true;
	}
}

dfs사용

package Cho.week_5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main6186_1 {
	
	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0,0, -1, 1};
	static char[][] map;
	static boolean[][] check;

	static int R, C;

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		R = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		
		map = new char[R][C];
		check = new boolean[R][C];
		
		for(int i=0; i<R; i++) {
			String str = br.readLine();
			for(int j=0; j<C; j++) {
				map[i][j] = str.charAt(j);
			}
		}

		int cnt = 0;
		for(int i=0; i<R; i++) {
			for(int j=0; j<C; j++) {
				if(map[i][j] == '#' && !check[i][j]) {
					dfs(new Pair(i,j));
					cnt++;
				}
			}
		}
		System.out.println(cnt);
		

	}
	
	static void dfs(Pair pair) {
		
		int x = pair.x;
		int y = pair.y;
		
		check[x][y] = true;
				
		for(int i=0; i<4; i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if(valid(nx, ny) && map[nx][ny] == '#') {
				dfs(new Pair(nx,ny));
			}
		}

	}
	
	static class Pair{
		int x;
		int y;
		public Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}
		
	}

	static boolean valid(int x, int y) {
		if(x < 0 || x > R-1 || y < 0 || y > C-1 || check[x][y]) {
			return false;
		}
		return true;
	}
}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 17198  (0) 2021.12.22
백준 14562  (0) 2021.12.21
백준 16956  (0) 2021.12.19
백준 7795  (0) 2021.12.18
백준 2012  (0) 2021.12.17