[백준] 3184_양

김태민·2022년 5월 9일
0

알고리즘

목록 보기
49/77

mingssssss

1. 문제

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


2. 코드

package mymain;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;

public class BOJ_3184 {

	static int[][] list;
	static boolean[][] visited;
	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0, 0, -1, 1};
	static int sheep;
	static int wolf;
	static int[] answer;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

//		Scanner sc = new Scanner(System.in);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());

		list = new int[N][M];
		visited = new boolean[N][M];
		answer = new int[2];

		for (int i = 0; i < N; i++) {
//			String[] s = sc.next().split("");
			char c[] = br.readLine().toCharArray();
			for (int j = 0; j < M; j++) {
				if (c[j] == '#') {
					list[i][j] = 1;
				} else if (c[j] == 'v') {
					list[i][j] = 2;
				} else if (c[j] == 'o') {
					list[i][j] = 3;
				}
			}
		}
		// 입력 종료(0 = 빈 곳, 1 = 울타리, 2 = 늑대, 3 = 양)
		
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				
				if (visited[i][j] == false && (list[i][j] == 2 || list[i][j] == 3)) {
					bfs(i, j, N, M, list, visited);
				}
			}
		}
		
		System.out.println(answer[0] + " " + answer[1]);
		
		
		
		
//		// 출력
//		for (int i = 0; i < N; i++) {
//			for (int j = 0; j < M; j++) {
//				System.out.printf("%d ", list[i][j]);
//			}
//			System.out.println();
//		}

	}
	
	public static void bfs (int r, int c, int N, int M, int[][] list, boolean[][] visited) {
		
		Queue<int[]> q = new LinkedList<>();
		
		q.add(new int[] {r, c});
		visited[r][c] = true;
		
		sheep = 0;
		wolf = 0;
		
		if (list[r][c] == 2) {
			wolf++;
		} else if (list[r][c] == 3) {
			sheep++;
		}
		
		while (!q.isEmpty()) {
			
			int now[] = q.poll();
			
			for (int i = 0; i < 4; i++) {
				
				int nextX = now[0] + dx[i];
				int nextY = now[1] + dy[i];
				
				if (nextX < 0 || nextY < 0 || nextX >= N || nextY >= M) {
					continue;
				}
				
				if (visited[nextX][nextY] == true || list[nextX][nextY] == 1 ) {
					continue;
				}
				
				if (list[nextX][nextY] == 2) {
					wolf++;
				} else if (list[nextX][nextY] == 3) {
					sheep++;
				}
				visited[nextX][nextY] = true;
				q.add(new int[] {nextX, nextY});
			}
			
		}
		
		if (wolf >= sheep) {
			answer[1] += wolf;
		} else {
			answer[0] += sheep;
		}
		
	}

}

3. 리뷰

입력이 숫자가 아닌 특수문자로 들어오는 문제이다.

공백 없이 입력이 주어져서 StringTokenizer를 이용할 수 없어서

Scanner로 받아서 제출하니 다른 분들보다 2배가 걸렸다.

구글링 해서 char 형태로 받아올 수 있는 것을 알고

제출하니 절반의 시간으로 제출할 수 있었다.

입력받을 때 빈 곳과 울타리와 양과 늑대를 구분하여 list에 넣었다.

탐색하면서 양과 늑대가 있는 좌표일 때만 큐에 넣고 bfs를 돌렸다.

탐색이 끝난 후 양과 늑대의 수를 비교해서 늑대와 양의 수가 같거나 클 때는

늑대를 추가하고, 아닌 경우에는 양의 마리수를 추가했다.

profile
어제보다 성장하는 개발자

0개의 댓글