-
Notifications
You must be signed in to change notification settings - Fork 0
/
거리두기확인하기.js
64 lines (51 loc) · 1.22 KB
/
거리두기확인하기.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
function distancingByPerson(place, x, y) {
const dx = [-1, 0, 1, 0];
const dy = [0, 1, 0, -1];
for (let i = 0; i < 4; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5) {
if (place[nx][ny] === 'P') {
return false;
}
}
}
return true;
}
function distancingByEmpty(place, x, y) {
const dx = [-1, 0, 1, 0];
const dy = [0, 1, 0, -1];
let count = 0;
for (let i = 0; i < 4; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5) {
if (place[nx][ny] === 'P') {
count++;
}
}
if (count >= 2) {
return false;
}
}
return true;
}
function checkDistancing(place) {
const N = 5;
for (let x = 0; x < N; x++) {
for (let y = 0; y < N; y++) {
if (
(place[x][y] === 'P' && !distancingByPerson(place, x, y)) ||
(place[x][y] === 'O' && !distancingByEmpty(place, x, y))
) {
return false;
}
}
}
return true;
}
function solution(places) {
const _places = places.map((place) => place.map((row) => row.split('')));
const check = _places.map((place) => (checkDistancing(place) ? 1 : 0));
return check;
}