PS/BOJ

백준 4963 섬의개수 / JAVA

얍연구소장 2023. 5. 17.

https://www.acmicpc.net/problem/4963

 

4963번: 섬의 개수

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은 양의 정수이다. 둘째 줄부터 h개 줄에는 지도

www.acmicpc.net

 

> 풀이(DFS)

/*
 * 백준 4963 섬의개수
 * #실버2
 * #DFS
 * 23.05.17
 */
public class boj_4963_DFS {

	static int[] dx = {-1,-1,-1,0,1,1,1,0}; 
	static int[] dy = {-1,0,1,1,1,0,-1,-1};
	static int[][] arr;
	static boolean[][] checked;
	static int answer;
	static int w,h;
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input = "";
		while(true) {
			input = br.readLine();
			if("0 0".equals(input)) break;
			StringTokenizer st = new StringTokenizer(input);
			w = Integer.parseInt(st.nextToken());
			h = Integer.parseInt(st.nextToken());
			arr = new int[h+1][w+1];
			checked = new boolean[h+1][w+1];
			for(int i = 1; i <= h; i++) {
				st = new StringTokenizer(br.readLine());
				for(int j = 1; j <= w; j++) {
					arr[i][j] = Integer.parseInt(st.nextToken());
				}
			}
			
			int answer = 0;
			for(int i = 1; i <= h; i++) {
				for(int j = 1; j <= w; j++) {
					if(!checked[i][j] && arr[i][j] == 1) {
						answer++;
						DFS(i,j);
					}
				}
			}
			System.out.println(answer);
		}
	}
	
	public static void DFS(int x, int y) {
		checked[x][y] = true;
		for(int i = 0; i < 8; i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			
			if(nx > 0 && nx <= h && ny > 0 && ny <= w) {
				if(!checked[nx][ny] && arr[nx][ny] == 1) {
					DFS(nx,ny);
				}
			}
		}
	}
}

 

> 풀이(BFS)

/*
 * 백준 4963 섬의개수
 * #실버2
 * #BFS
 * 23.05.18
 */
public class boj_4963_BFS {

	static int[] dx = {-1,-1,-1,0,1,1,1,0}; 
	static int[] dy = {-1,0,1,1,1,0,-1,-1};
	static int[][] arr;
	static boolean[][] checked;
	static int answer;
	static int w,h;
	static class Node {
		int x, y;
		
		public Node(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input = "";
		while(true) {
			input = br.readLine();
			if("0 0".equals(input)) break;
			StringTokenizer st = new StringTokenizer(input);
			w = Integer.parseInt(st.nextToken());
			h = Integer.parseInt(st.nextToken());
			arr = new int[h+1][w+1];
			checked = new boolean[h+1][w+1];
			for(int i = 1; i <= h; i++) {
				st = new StringTokenizer(br.readLine());
				for(int j = 1; j <= w; j++) {
					arr[i][j] = Integer.parseInt(st.nextToken());
				}
			}
			
			int answer = 0;
			for(int i = 1; i <= h; i++) {
				for(int j = 1; j <= w; j++) {
					if(!checked[i][j] && arr[i][j] == 1) {
						answer++;
						BFS(i,j);
					}
				}
			}
			System.out.println(answer);
		}
	}
	
	public static void BFS(int x, int y) {
		Queue<Node> queue = new LinkedList<>();
		queue.offer(new Node(x,y));
		checked[x][y] = true;
		
		while(!queue.isEmpty()) {
			Node node = queue.poll();
			for(int i = 0; i < 8; i++) {
				int nx = node.x + dx[i];
				int ny = node.y + dy[i];
				if(nx > 0 && nx <= h && ny > 0 && ny <= w) {
					if(arr[nx][ny] == 1 && !checked[nx][ny]) {
						checked[nx][ny] = true;
						queue.offer(new Node(nx,ny));
					}
				}
			}
		}
	}

}

'PS > BOJ' 카테고리의 다른 글

백준 11725 트리의 부모 찾기 / JAVA  (0) 2023.05.23
백준 1388 바닥장식 / JAVA  (0) 2023.05.18
백준 6603 로또 / JAVA  (0) 2023.05.16
백준 2644 촌수계산 / JAVA  (0) 2023.05.15
백준 1206 보물 / JAVA  (0) 2023.05.10

댓글