BOJ 10026 :: 적록색약
문제 링크 : https://www.acmicpc.net/problem/10026
나의 코드
Github : https://github.com/j2wooooo/Daliy_Algorithms/blob/master/Daliy_Algorithms/BOJ_10026/BOJ_10026.cpp
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 87 88 89 90 91 92 93 94 95 96 97 98 | // 백준알고리즘 10026번 :: 적록색약 #include<iostream> #include<queue> #include<string> #include<string.h> using namespace std; int N; bool flag; int picture[101][101]; int visited[101][101]; queue<pair<int, int>> q; int pub; int npub; int dx[4] = { 0,0,1,-1 }; int dy[4] = { 1,-1,0,0 }; void bfs(int ch) { while (!q.empty()) { int qsz = q.size(); while (qsz--) { int x = q.front().first; int y = q.front().second; int color = picture[x][y]; q.pop(); for (int i = 0; i < 4; i++) { int mx = x + dx[i]; int my = y + dy[i]; int mcolor = picture[mx][my]; if (mx < 0 || mx >= N || my < 0 || my >= N) continue; if (visited[mx][my]) continue; if((flag && (mcolor == 'R' || mcolor == 'G')) || color == mcolor) { q.push(make_pair(mx, my)); visited[mx][my] = 1; } } } } if (ch == 0) pub++; else npub++; return; } int main(void) { string str; cin >> N; for (int i = 0; i < N; i++) { cin >> str; for (int j = 0; j < N; j++) { picture[i][j] = str[j]; } } // 일반인 for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (!visited[i][j]) { q.push(make_pair(i,j)); bfs(0); } } } memset(visited, 0, sizeof(visited)); // 적록색약인 for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (!visited[i][j]) { if (picture[i][j] == 'R' || picture[i][j] == 'G') flag = true; else flag = false; q.push(make_pair(i, j)); bfs(1); } } } cout << pub << ' ' << npub; return 0; } | cs |
'Problem > BFS' 카테고리의 다른 글
[C/C++] BOJ 1113 :: 수영장 만들기 (0) | 2019.02.08 |
---|---|
[C/C++] BOJ 1726 :: 로봇 (0) | 2019.02.07 |
[C/C++] BOJ 14923 :: 미로 탈출 (0) | 2019.01.24 |
[C/C++] BOJ 3184 :: 양 (0) | 2019.01.23 |
[C/C++] BOJ 2146 :: 다리 만들기 (0) | 2019.01.22 |