-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
92 lines (82 loc) · 2.39 KB
/
test.js
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
const fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = Number(input[0]);
let colorArr = [];
let count = 0;
let count2 = 0;
let visitedArr = [];
let changedColorVisitedArr = [];
let changedColorArr = [];
for (let i = 1; i <= N; i++) {
colorArr[i] = [,];
changedColorArr[i] = [,];
colorArr[i].push(...input[i].split(''));
changedColorArr[i].push(...input[i].split(''));
visitedArr[i] = new Array(N + 1).fill(false);
changedColorVisitedArr[i] = new Array(N + 1).fill(false);
}
// 적록색약 색 변환
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= N; j++) {
let color = changedColorArr[i][j];
if (color == 'G') {
changedColorArr[i][j] = 'R';
}
}
}
const dfs = (x, y, color) => {
if (visitedArr[x][y] || x <= 0 || x > N || y <= 0 || y > N) {
return false;
}
visitedArr[x][y] = true;
if (x - 1 >= 1 && !visitedArr[x - 1][y] && colorArr[x - 1][y] == color) {
dfs(x - 1, y, color);
}
if (x + 1 <= N && !visitedArr[x + 1][y] && colorArr[x + 1][y] == color) {
dfs(x + 1, y, color);
}
if (y - 1 >= 1 && !visitedArr[x][y - 1] && colorArr[x][y - 1] == color) {
dfs(x, y - 1, color);
}
if (y + 1 <= N && !visitedArr[x][y + 1] && colorArr[x][y + 1] == color) {
dfs(x, y + 1, color);
}
return true;
};
const dfs2 = (x, y, color) => {
if (changedColorVisitedArr[x][y] || x <= 0 || x > N || y <= 0 || y > N) {
return false;
}
changedColorVisitedArr[x][y] = true;
if (x - 1 >= 1 && !changedColorVisitedArr[x - 1][y] && changedColorArr[x - 1][y] == color) {
dfs2(x - 1, y, color);
}
if (x + 1 <= N && !changedColorVisitedArr[x + 1][y] && changedColorArr[x + 1][y] == color) {
dfs2(x + 1, y, color);
}
if (y - 1 >= 1 && !changedColorVisitedArr[x][y - 1] && changedColorArr[x][y - 1] == color) {
dfs2(x, y - 1, color);
}
if (y + 1 <= N && !changedColorVisitedArr[x][y + 1] && changedColorArr[x][y + 1] == color) {
dfs2(x, y + 1, color);
}
return true;
};
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= N; j++) {
let color = colorArr[i][j];
if (dfs(i, j, color)) {
count++;
}
let color2 = changedColorArr[i][j];
if (dfs2(i, j, color2)) {
count2++;
}
}
}
console.log(count, count2);
// 빨, 파, 초
// 빨초, 파
// 모든 좌표에 대해 DFS 실행
// 상하좌우에 같은 문자열이 있는지 확인하는 로직
// visited