티스토리 뷰
3184번: 양
첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다. 다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.
www.acmicpc.net
풀이
1) 맵, 방문체크, 총 양, 총 늑대를 static으로 선언
2) 2중 포문으로 맵을 돌면서 방문체크가 안되어있고 #인 경우 bfs탐색
2-1) bfs를 돌때마다 한 울타리 내에 양의 수와 늑대의 수를 기억함
2-2) 양의 수가 크면 총 양에 더함, 작거나 같으면 총 늑대에 더함
3) 총 양과 총 늑대의 수를 출력
주의사항
1) 없다. 함정이 없는 깔끔한 문제.
package com.baekJoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BJ_S2_3184_양 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int R,C,totalWolf,totalSheep;
static boolean isVisited[][];
static char map[][];
public static void main(String[] args) throws NumberFormatException, IOException {
input = new BufferedReader(new StringReader(src));
tokens = new StringTokenizer(input.readLine());
R = Integer.parseInt(tokens.nextToken());
C = Integer.parseInt(tokens.nextToken());
map = new char [R][C];
isVisited = new boolean[R][C];
for(int r=0; r<R; r++) {
String line = input.readLine();
for(int c=0; c<C; c++) {
map[r][c] = line.charAt(c);
}
}
for(int r=0; r<R; r++) {
for(int c=0; c<C; c++) {
if(!isVisited[r][c] && map[r][c] != '#') {
bfs(r,c);
}
}
}
System.out.println(totalSheep +" "+ totalWolf);
}
private static void bfs(int r, int c) {
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(r, c));
isVisited[r][c] = true;
int sheep = 0, wolf = 0;
while(!queue.isEmpty()) {
Node front = queue.poll();
if(map[front.r][front.c] == 'o') {
sheep++;
}else if(map[front.r][front.c] == 'v') {
wolf++;
}
for(int d=0; d<4; d++) {
int nr = front.r + dr[d];
int nc = front.c + dc[d];
if(isIn(nr, nc) && !isVisited[nr][nc] && map[nr][nc] != '#') {
queue.offer(new Node(nr, nc));
isVisited[nr][nc] = true;
}
}
}
if(sheep > wolf) {
totalSheep += sheep;
}else {
totalWolf += wolf;
}
}
static int dr[] = {0,0,-1,1};
static int dc[] = {-1,1,0,0};
static boolean isIn(int r, int c) {
return (r>=0 && c>=0 && r<R && c<C);
}
static class Node{
int r;
int c;
public Node(int r, int c) {
super();
this.r = r;
this.c = c;
}
@Override
public String toString() {
return "Node [r=" + r + ", c=" + c + "]";
}
}
static String src =
"6 6\r\n" +
"...#..\r\n" +
".##v#.\r\n" +
"#v.#.#\r\n" +
"#.o#.#\r\n" +
".###.#\r\n" +
"...###";
}
후기
보자마자 후다닥 풀 수 있었을 것 같지만 혹시몰라서 노트에 먼저 설계를 한 뒤에 시작했다.
다행히 막힘 없이 풀어서 한번에 통과했다.
'Algorithm' 카테고리의 다른 글
[백준] S3 2559 수열 (java) (0) | 2021.01.04 |
---|---|
[백준] G5 1600 말이 되고픈 원숭이 (java) (1) | 2021.01.04 |
[백준] S1 16198 에너지 모으기 (java) (0) | 2021.01.03 |
[백준] S3 17413 단어 뒤집기 2 (java) (0) | 2021.01.03 |
[백준] G4 16197 두 동전 (java) (0) | 2021.01.03 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java
- S3
- 알고리즘
- 백트래킹
- 현꾸라지
- 백준
- PriorityQueue
- g4
- react native
- laugh4mile
- Spring Boot
- 자바
- BFS
- G5
- 그리디
- map
- 객체지향
- 리액트
- 코딩새내기
- 문자열
- DFS
- SWEA
- 리액트 네이티브
- 시뮬레이션
- react
- 우선순위큐
- 다익스트라
- Spring
- 구현
- S2
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함