Skip to content

Commit c3ff574

Browse files
committed
[Silver I] Title: 양, Time: 188 ms, Memory: 21368 KB -BaekjoonHub
1 parent 73f97dc commit c3ff574

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# [Silver I] 양 - 3184
2+
3+
[문제 링크](https://www.acmicpc.net/problem/3184)
4+
5+
### 성능 요약
6+
7+
메모리: 21368 KB, 시간: 188 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2024년 12월 2일 17:26:35
16+
17+
### 문제 설명
18+
19+
<p>미키의 뒷마당에는 특정 수의 양이 있다. 그가 푹 잠든 사이에 배고픈 늑대는 마당에 들어와 양을 공격했다.</p>
20+
21+
<p>마당은 행과 열로 이루어진 직사각형 모양이다. 글자 '.' (점)은 빈 필드를 의미하며, 글자 '#'는 울타리를, 'o'는 양, 'v'는 늑대를 의미한다.</p>
22+
23+
<p>한 칸에서 수평, 수직만으로 이동하며 울타리를 지나지 않고 다른 칸으로 이동할 수 있다면, 두 칸은 같은 영역 안에 속해 있다고 한다. 마당에서 "탈출"할 수 있는 칸은 어떤 영역에도 속하지 않는다고 간주한다.</p>
24+
25+
<p>다행히 우리의 양은 늑대에게 싸움을 걸 수 있고 영역 안의 양의 수가 늑대의 수보다 많다면 이기고, 늑대를 우리에서 쫓아낸다. 그렇지 않다면 늑대가 그 지역 안의 모든 양을 먹는다.</p>
26+
27+
<p>맨 처음 모든 양과 늑대는 마당 안 영역에 존재한다.</p>
28+
29+
<p>아침이 도달했을 때 살아남은 양과 늑대의 수를 출력하는 프로그램을 작성하라.</p>
30+
31+
### 입력
32+
33+
<p>첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다.</p>
34+
35+
<p>다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.</p>
36+
37+
### 출력
38+
39+
<p>하나의 줄에 아침까지 살아있는 양과 늑대의 수를 의미하는 두 정수를 출력한다.</p>
40+

백준/Silver/3184. 양/양.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int R,C;
6+
static char arr[][];
7+
static int dx[] = {-1,1,0,0};
8+
static int dy[] = {0,0,-1,1};
9+
static int sheep =0;
10+
static int wolf = 0;
11+
static boolean visited[][];
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
15+
StringTokenizer st =new StringTokenizer(br.readLine());
16+
17+
R = Integer.parseInt(st.nextToken());
18+
C = Integer.parseInt(st.nextToken());
19+
20+
arr = new char[R][C];
21+
visited= new boolean[R][C];
22+
23+
for(int i =0; i<R; i++) {
24+
String input = br.readLine();
25+
for(int j =0; j<C; j++) {
26+
arr[i][j]= input.charAt(j);
27+
}
28+
}
29+
for(int i =0; i<R; i++) {
30+
for(int j =0; j<C; j++) {
31+
char currentChar = arr[i][j];
32+
if(currentChar == '.' || currentChar == 'v' || currentChar =='o') {
33+
if(visited[i][j] == false) {
34+
bfs(i,j);
35+
}
36+
}
37+
}
38+
}
39+
System.out.print(sheep +" " +wolf);
40+
41+
}
42+
43+
static void bfs(int x, int y) {
44+
int currentSheep = 0;
45+
int currentWolf = 0;
46+
Queue<int[]> queue = new LinkedList<>();
47+
queue.add(new int[]{x,y});
48+
visited[x][y]= true;
49+
50+
if(arr[x][y] == 'o')
51+
currentSheep++;
52+
53+
if(arr[x][y] =='v') {
54+
currentWolf++;
55+
}
56+
57+
while(!queue.isEmpty()) {
58+
59+
int now[] = queue.poll();
60+
int nowx = now[0];
61+
int nowy = now[1];
62+
63+
for(int i =0; i<4; i++) {
64+
int nx = nowx + dx[i];
65+
int ny = nowy + dy[i];
66+
67+
if(nx >=0 && nx <R && ny>=0 && ny <C) {
68+
if(visited[nx][ny] == false && arr[nx][ny] != '#') {
69+
queue.add(new int[] {nx,ny});
70+
visited[nx][ny] = true;
71+
72+
if(arr[nx][ny] == 'o')
73+
currentSheep++;
74+
if(arr[nx][ny] == 'v') {
75+
currentWolf++;
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
if(currentSheep > currentWolf) {
83+
sheep += currentSheep;
84+
}
85+
else {
86+
wolf += currentWolf;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)