반응형
문제 출처 :
https://www.acmicpc.net/problem/3184
알고리즘 분석 :
문제 해결에 필요한 사항
1. BFS
기본적인 BFS 문제인 것 같다.
각 구역마다의 늑대와 양의 수를 체크하고
양 > 늑대일 때는 전체 늑대 수에서 현재 카운트 된 늑대의 수를 빼주고
양 <= 늑대일 때는 전체 양 수에서 현재 카운트 된 양의 수를 빼주면 된다.
소스 코드 :
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <iostream> #include <cstdio> #include <queue> using namespace std; char str[255][255]; bool visit[255][255]; int dy[4] = { 1,0,-1,0 }; int dx[4] = { 0,1,0,-1 }; int main() { int n, m; scanf("%d %d", &n, &m); int wolf = 0, lamb = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf(" %c", &str[i][j]); if (str[i][j] == 'v') wolf++; else if (str[i][j] == 'o') lamb++; } } for (int i = 0; i < n; i++) { queue<pair<int, int> > q; for (int j = 0; j < m; j++) { int tWolf = 0, tLamb = 0; if (str[i][j] != '#' && !visit[i][j]) { q.push({ i,j }); while (!q.empty()) { int y = q.front().first; int x = q.front().second; q.pop(); if (visit[y][x]) continue; visit[y][x] = true; if (str[y][x] == 'v') tWolf++; else if (str[y][x] == 'o') tLamb++; for (int k = 0; k < 4; k++) { int ny = y + dy[k]; int nx = x + dx[k]; if (!((0 <= ny && ny < n) && (0 <= nx && nx < m)) || str[ny][nx] == '#' || visit[ny][nx]) continue; q.push({ ny,nx }); } } if (tWolf >= tLamb) lamb -= tLamb; else wolf -= tWolf; } } } printf("%d %d", lamb, wolf); return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[2213번] 트리의 독립집합 (0) | 2017.09.25 |
---|---|
[14716번] 현수막 (0) | 2017.09.25 |
[11313번] Best Buddies (0) | 2017.09.23 |
[11320번] 삼각 무늬 - 1 (2) | 2017.09.21 |
[2810번] 컵홀더 (0) | 2017.09.21 |