-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path방문 길이.cpp
53 lines (51 loc) · 1.07 KB
/
방문 길이.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
#include <string>
using namespace std;
int solution(string dirs)
{
int x = 5; int y = 5; //시작 위치는 배열 기준 (5,5)로 지정
int count = 0;
int check[11][11][11][11]; //길 방문 여부(전 x좌표, 전 y좌표, 후 x좌표, 후 y좌표)
for (int i = 0; i < dirs.length(); i++) {
if (dirs[i] == 'U') {
if (y < 10) {
if (check[x][y][x][y + 1] != 1) {
check[x][y][x][y + 1] = 1; //방문한 것으로 체크
check[x][y + 1][x][y] = 1;
count++;
}
y++;
}
}
else if (dirs[i] == 'D') {
if (y > 0) {
if (check[x][y][x][y -1] != 1) {
check[x][y][x][y - 1] = 1;
check[x][y - 1][x][y] = 1;
count++;
}
y--;
}
}
else if (dirs[i] == 'L') {
if (x > 0) {
if (check[x][y][x - 1][y] != 1) {
check[x][y][x - 1][y] = 1;
check[x - 1][y][x][y] = 1;
count++;
}
x--;
}
}
else if (dirs[i] == 'R') {
if (x < 10) {
if (check[x][y][x + 1][y] != 1) {
check[x][y][x + 1][y] = 1;
check[x + 1][y][x][y] = 1;
count++;
}
x++;
}
}
}
return count;
}