|
| 1 | +const fs = require('fs'); |
| 2 | +let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n'); |
| 3 | + |
| 4 | +const N = Number(input[0]); |
| 5 | +let colorArr = []; |
| 6 | +let count = 0; |
| 7 | +let count2 = 0; |
| 8 | +let visitedArr = []; |
| 9 | +let changedColorVisitedArr = []; |
| 10 | +let changedColorArr = []; |
| 11 | + |
| 12 | +for (let i = 1; i <= N; i++) { |
| 13 | + colorArr[i] = [,]; |
| 14 | + changedColorArr[i] = [,]; |
| 15 | + colorArr[i].push(...input[i].split('')); |
| 16 | + changedColorArr[i].push(...input[i].split('')); |
| 17 | + visitedArr[i] = new Array(N + 1).fill(false); |
| 18 | + changedColorVisitedArr[i] = new Array(N + 1).fill(false); |
| 19 | +} |
| 20 | + |
| 21 | +// 적록색약 색 변환 |
| 22 | +for (let i = 1; i <= N; i++) { |
| 23 | + for (let j = 1; j <= N; j++) { |
| 24 | + let color = changedColorArr[i][j]; |
| 25 | + if (color == 'G') { |
| 26 | + changedColorArr[i][j] = 'R'; |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +const dfs = (x, y, color) => { |
| 32 | + if (visitedArr[x][y] || x <= 0 || x > N || y <= 0 || y > N) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + visitedArr[x][y] = true; |
| 36 | + |
| 37 | + if (x - 1 >= 1 && !visitedArr[x - 1][y] && colorArr[x - 1][y] == color) { |
| 38 | + dfs(x - 1, y, color); |
| 39 | + } |
| 40 | + if (x + 1 <= N && !visitedArr[x + 1][y] && colorArr[x + 1][y] == color) { |
| 41 | + dfs(x + 1, y, color); |
| 42 | + } |
| 43 | + if (y - 1 >= 1 && !visitedArr[x][y - 1] && colorArr[x][y - 1] == color) { |
| 44 | + dfs(x, y - 1, color); |
| 45 | + } |
| 46 | + if (y + 1 <= N && !visitedArr[x][y + 1] && colorArr[x][y + 1] == color) { |
| 47 | + dfs(x, y + 1, color); |
| 48 | + } |
| 49 | + return true; |
| 50 | +}; |
| 51 | + |
| 52 | +const dfs2 = (x, y, color) => { |
| 53 | + if (changedColorVisitedArr[x][y] || x <= 0 || x > N || y <= 0 || y > N) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + changedColorVisitedArr[x][y] = true; |
| 57 | + |
| 58 | + if (x - 1 >= 1 && !changedColorVisitedArr[x - 1][y] && changedColorArr[x - 1][y] == color) { |
| 59 | + dfs2(x - 1, y, color); |
| 60 | + } |
| 61 | + if (x + 1 <= N && !changedColorVisitedArr[x + 1][y] && changedColorArr[x + 1][y] == color) { |
| 62 | + dfs2(x + 1, y, color); |
| 63 | + } |
| 64 | + if (y - 1 >= 1 && !changedColorVisitedArr[x][y - 1] && changedColorArr[x][y - 1] == color) { |
| 65 | + dfs2(x, y - 1, color); |
| 66 | + } |
| 67 | + if (y + 1 <= N && !changedColorVisitedArr[x][y + 1] && changedColorArr[x][y + 1] == color) { |
| 68 | + dfs2(x, y + 1, color); |
| 69 | + } |
| 70 | + return true; |
| 71 | +}; |
| 72 | + |
| 73 | +for (let i = 1; i <= N; i++) { |
| 74 | + for (let j = 1; j <= N; j++) { |
| 75 | + let color = colorArr[i][j]; |
| 76 | + if (dfs(i, j, color)) { |
| 77 | + count++; |
| 78 | + } |
| 79 | + let color2 = changedColorArr[i][j]; |
| 80 | + if (dfs2(i, j, color2)) { |
| 81 | + count2++; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +console.log(count, count2); |
| 87 | + |
| 88 | +// 빨, 파, 초 |
| 89 | +// 빨초, 파 |
| 90 | +// 모든 좌표에 대해 DFS 실행 |
| 91 | +// 상하좌우에 같은 문자열이 있는지 확인하는 로직 |
| 92 | +// visited |
0 commit comments