티스토리 뷰

728x90

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

 

3184번: 양

첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다. 다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.

www.acmicpc.net

 

 

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
R, C = map(int, input().split())
lst = [[]*for _ in range(R)]
visited = [[0* C for _ in range(R)]
direction = [(10), (01), (-10), (0-1)]
sheep, wolf = 00
for i in range(R):
    S = (list(input().rstrip()))
    lst[i] = S
 
queue = []
 
 
def bfs(x, y):
    global w, s
    visited[x][y] = 1
    queue.append([x, y])
 
    while queue:
        x, y = queue[0][0], queue[0][1]
        del queue[0]
        if lst[x][y] == 'v':
            w += 1
        elif lst[x][y] == 'o':
            s += 1
 
        for dx, dy in direction:
            nx, ny = x + dx, y + dy
            if 0 <= nx < R and 0 <= ny < C and lst[nx][ny] != '#' and visited[nx][ny] == 0:
                queue.append((nx, ny))
                visited[nx][ny] = 1
 
 
for i in range(R):
    for j in range(C):
        if lst[i][j] != '#' and visited[i][j] == 0:
            w, s = 00
            bfs(i, j)
            if w >= s:
                s = 0
            else:
                w = 0
            wolf += w
            sheep += s
print(sheep, wolf)
cs

 

댓글