Skip to content

Commit a45514b

Browse files
committed
[level 2] Title: 방문 길이, Time: 0.18 ms, Memory: 82.6 MB -BaekjoonHub
1 parent 7e906ce commit a45514b

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [level 2] 방문 길이 - 49994
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/49994)
4+
5+
### 성능 요약
6+
7+
메모리: 82.6 MB, 시간: 0.18 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > Summer/Winter Coding(~2018)
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 03월 01일 22:52:52
20+
21+
### 문제 설명
22+
23+
<p>게임 캐릭터를 4가지 명령어를 통해 움직이려 합니다. 명령어는 다음과 같습니다.</p>
24+
25+
<ul>
26+
<li><p>U: 위쪽으로 한 칸 가기</p></li>
27+
<li><p>D: 아래쪽으로 한 칸 가기</p></li>
28+
<li><p>R: 오른쪽으로 한 칸 가기</p></li>
29+
<li><p>L: 왼쪽으로 한 칸 가기</p></li>
30+
</ul>
31+
32+
<p>캐릭터는 좌표평면의 (0, 0) 위치에서 시작합니다. 좌표평면의 경계는 왼쪽 위(-5, 5), 왼쪽 아래(-5, -5), 오른쪽 위(5, 5), 오른쪽 아래(5, -5)로 이루어져 있습니다.</p>
33+
34+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/ace0e7bc-9092-4b95-9bfb-3a55a2aa780e/%E1%84%87%E1%85%A1%E1%86%BC%E1%84%86%E1%85%AE%E1%86%AB%E1%84%80%E1%85%B5%E1%86%AF%E1%84%8B%E1%85%B51_qpp9l3.png" title="" alt="방문길이1_qpp9l3.png"></p>
35+
36+
<p>예를 들어, "ULURRDLLU"로 명령했다면</p>
37+
38+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/668c7458-e184-472d-9d32-f5d2acca759a/%E1%84%87%E1%85%A1%E1%86%BC%E1%84%86%E1%85%AE%E1%86%AB%E1%84%80%E1%85%B5%E1%86%AF%E1%84%8B%E1%85%B52_lezmdo.png" title="" alt="방문길이2_lezmdo.png"></p>
39+
40+
<ul>
41+
<li>1번 명령어부터 7번 명령어까지 다음과 같이 움직입니다.</li>
42+
</ul>
43+
44+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/08558e36-d667-4160-bfec-b754c78a7d85/%E1%84%87%E1%85%A1%E1%86%BC%E1%84%86%E1%85%AE%E1%86%AB%E1%84%80%E1%85%B5%E1%86%AF%E1%84%8B%E1%85%B53_sootjd.png" title="" alt="방문길이3_sootjd.png"></p>
45+
46+
<ul>
47+
<li>8번 명령어부터 9번 명령어까지 다음과 같이 움직입니다.</li>
48+
</ul>
49+
50+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/a52af28e-5835-438b-9f40-5467ebf9bf03/%E1%84%87%E1%85%A1%E1%86%BC%E1%84%86%E1%85%AE%E1%86%AB%E1%84%80%E1%85%B5%E1%86%AF%E1%84%8B%E1%85%B54_hlpiej.png" title="" alt="방문길이4_hlpiej.png"></p>
51+
52+
<p>이때, 우리는 게임 캐릭터가 지나간 길 중 <strong>캐릭터가 처음 걸어본 길의 길이</strong>를 구하려고 합니다. 예를 들어 위의 예시에서 게임 캐릭터가 움직인 길이는 9이지만, 캐릭터가 처음 걸어본 길의 길이는 7이 됩니다. (8, 9번 명령어에서 움직인 길은 2, 3번 명령어에서 이미 거쳐 간 길입니다)</p>
53+
54+
<p>단, 좌표평면의 경계를 넘어가는 명령어는 무시합니다.</p>
55+
56+
<p>예를 들어, "LULLLLLLU"로 명령했다면</p>
57+
58+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/f631f005-f8de-4392-a76c-a9ef64b6de08/%E1%84%87%E1%85%A1%E1%86%BC%E1%84%86%E1%85%AE%E1%86%AB%E1%84%80%E1%85%B5%E1%86%AF%E1%84%8B%E1%85%B55_nitjwj.png" title="" alt="방문길이5_nitjwj.png"></p>
59+
60+
<ul>
61+
<li>1번 명령어부터 6번 명령어대로 움직인 후, 7, 8번 명령어는 무시합니다. 다시 9번 명령어대로 움직입니다.</li>
62+
</ul>
63+
64+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/35e62f0a-43c6-4142-bec6-6d28fbc57216/%E1%84%87%E1%85%A1%E1%86%BC%E1%84%86%E1%85%AE%E1%86%AB%E1%84%80%E1%85%B5%E1%86%AF%E1%84%8B%E1%85%B56_nzhumd.png" title="" alt="방문길이6_nzhumd.png"></p>
65+
66+
<p>이때 캐릭터가 처음 걸어본 길의 길이는 7이 됩니다.</p>
67+
68+
<p>명령어가 매개변수 dirs로 주어질 때, 게임 캐릭터가 처음 걸어본 길의 길이를 구하여 return 하는 solution 함수를 완성해 주세요.</p>
69+
70+
<h5>제한사항</h5>
71+
72+
<ul>
73+
<li>dirs는 string형으로 주어지며, 'U', 'D', 'R', 'L' 이외에 문자는 주어지지 않습니다.</li>
74+
<li>dirs의 길이는 500 이하의 자연수입니다.</li>
75+
</ul>
76+
77+
<h5>입출력 예</h5>
78+
<table class="table">
79+
<thead><tr>
80+
<th>dirs</th>
81+
<th>answer</th>
82+
</tr>
83+
</thead>
84+
<tbody><tr>
85+
<td>"ULURRDLLU"</td>
86+
<td>7</td>
87+
</tr>
88+
<tr>
89+
<td>"LULLLLLLU"</td>
90+
<td>7</td>
91+
</tr>
92+
</tbody>
93+
</table>
94+
<h5>입출력 예 설명</h5>
95+
96+
<p>입출력 예 #1<br>
97+
문제의 예시와 같습니다.</p>
98+
99+
<p>입출력 예 #2<br>
100+
문제의 예시와 같습니다.</p>
101+
102+
103+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
3+
int currX, currY, cnt;
4+
boolean[][][] visited;
5+
6+
// d: 0=U, 1=D, 2=R, 3=L
7+
int[] dx = {0, 0, 1, -1};
8+
int[] dy = {1, -1, 0, 0};
9+
int[] rev = {1, 0, 3, 2}; // 반대방향 매핑
10+
11+
public int solution(String dirs) {
12+
currX = 5;
13+
currY = 5;
14+
cnt = 0;
15+
visited = new boolean[11][11][4];
16+
17+
for (int i = 0; i < dirs.length(); i++) {
18+
move(dirs.charAt(i));
19+
}
20+
return cnt;
21+
}
22+
23+
void move(char dir) {
24+
int d = 0;
25+
switch (dir) {
26+
case 'U': d = 0; break;
27+
case 'D': d = 1; break;
28+
case 'R': d = 2; break;
29+
case 'L': d = 3; break;
30+
}
31+
32+
int nextX = currX + dx[d];
33+
int nextY = currY + dy[d];
34+
35+
if (nextX < 0 || nextX > 10 || nextY < 0 || nextY > 10) return;
36+
37+
// "출발점에서 d방향으로 간 길"을 체크
38+
if (!visited[currX][currY][d]) {
39+
cnt++;
40+
visited[currX][currY][d] = true;
41+
visited[nextX][nextY][rev[d]] = true; // 반대방향도 같은 길
42+
}
43+
44+
currX = nextX;
45+
currY = nextY;
46+
}
47+
}

0 commit comments

Comments
 (0)