[백준 10026번] 적록색약
2017. 7. 3. 00:46ㆍ알고리즘/백준
반응형
풀이
구역을 나누는 BFS,DFS의 일반적인 문제이지만 적록색약일 때의 구역도 나누어서 계산해야되는 문제이다.
구역을 나누는 문제처럼 풀되 적록색약을 위한 배열을 새로 만들어주어야한다.
소스코드
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <iostream> using namespace std; int n; char board[102][102], handi_board[102][102]; int result_board[102][102] = { 0, }, handi_result_board[102][102] = { 0, }; int que_x[1000001], que_y[1000001], head_x,tail_x, head_y,tail_y; int cnt=1, handi_cnt = 1; void push(int x, int y) { que_x[tail_x++] = x; que_y[tail_y++] = y; } int pop_x() { return que_x[head_x++]; } int pop_y() { return que_y[head_y++]; } void bfs(char word) { while (head_x != tail_x) { int x = pop_x(); int y = pop_y(); if (x + 1 <= n - 1 && board[x + 1][y] == word && result_board[x+1][y]==0) { push(x + 1, y); board[x+1][y] = 'n'; result_board[x+1][y] = cnt; } if (x - 1 >= 0 && board[x - 1][y] == word&& result_board[x - 1][y] == 0) { push(x - 1, y); board[x-1][y] = 'n'; result_board[x-1][y] = cnt; } if (y + 1 <= n - 1 && board[x][y + 1] == word&& result_board[x][y+1] == 0) { push(x, y+1); board[x][y+1] = 'n'; result_board[x][y+1] = cnt; } if (y - 1 >= 0 && board[x][y - 1] == word&& result_board[x][y-1] == 0) { push(x, y-1); board[x][y-1] = 'n'; result_board[x][y-1] = cnt; } } } void handi_bfs(char word) { while (head_x != tail_x) { int x = pop_x(); int y = pop_y(); if (x + 1 <= n - 1 && handi_board[x + 1][y] == word && handi_result_board[x + 1][y] == 0) { push(x + 1, y); handi_board[x+1][y] = 'n'; handi_result_board[x + 1][y] = handi_cnt; } if (x - 1 >= 0 && handi_board[x - 1][y] == word&& handi_result_board[x - 1][y] == 0) { push(x - 1, y); handi_board[x-1][y] = 'n'; handi_result_board[x - 1][y] = handi_cnt; } if (y + 1 <= n - 1 && handi_board[x][y + 1] == word&& handi_result_board[x][y + 1] == 0) { push(x, y + 1); handi_board[x][y+1] = 'n'; handi_result_board[x][y + 1] = handi_cnt; } if (y - 1 >= 0 && handi_board[x][y - 1] == word&& handi_result_board[x][y - 1] == 0) { push(x, y - 1); handi_board[x][y-1] = 'n'; handi_result_board[x][y - 1] = handi_cnt; } } } void debugging() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << result_board[i][j] << " "; } cout << '\n'; } } void handi_debugging() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << handi_result_board[i][j] << " "; } cout << '\n'; } } int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> board[i][j]; if (board[i][j] == 'R') handi_board[i][j] = 'G'; else handi_board[i][j] = board[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (board[i][j] == 'R' || board[i][j] == 'G' || board[i][j] == 'B') { head_x = 0; tail_x = 0; head_y = 0; tail_y = 0; push(i, j); bfs(board[i][j]); cnt++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (handi_board[i][j] == 'G' || handi_board[i][j] == 'B') { head_x = 0; tail_x = 0; head_y = 0; tail_y = 0; push(i, j); handi_bfs(handi_board[i][j]); handi_cnt++; } } } cout << cnt - 1 << ' ' << handi_cnt - 1; return 0; } | cs |
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준 2161번] 카드1 (0) | 2017.07.05 |
---|---|
[백준 6603번] 로또 (0) | 2017.07.05 |
[백준 2206번]벽 부수고 이동하기 (0) | 2017.07.02 |
[백준 2455번] 지능형 기차 (0) | 2017.06.30 |
[백준 2167번] 2차원 배열의 합 (0) | 2017.06.30 |