Skip to content

Commit eb662a4

Browse files
committed
[level 2] Title: 방문 길이, Time: 0.01 ms, Memory: 4.2 MB -BaekjoonHub
1 parent 3c64723 commit eb662a4

2 files changed

Lines changed: 200 additions & 0 deletions

File tree

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+
메모리: 4.2 MB, 시간: 0.01 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년 05월 01일 23:24:31
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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <string>
2+
using namespace std;
3+
4+
int sero[11][11];
5+
int garo[11][11];
6+
int curX = 0, curY=0;
7+
int answer = 0;
8+
9+
void func(char type){
10+
int destX, destY;
11+
12+
13+
switch(type){
14+
case 'U' :
15+
destX = curX+5;
16+
destY = curY+5;
17+
curY++;
18+
19+
if(curY > 5) {
20+
curY--;
21+
break;
22+
}
23+
24+
if(sero[destX][destY] == 0){
25+
sero[destX][destY]=1;
26+
answer++;
27+
}
28+
break;
29+
case 'D' :
30+
destX = curX+5;
31+
destY = curY+4;
32+
curY--;
33+
34+
if(curY < -5) {
35+
curY++;
36+
break;
37+
}
38+
39+
if(sero[destX][destY] == 0){
40+
sero[destX][destY]=1;
41+
answer++;
42+
}
43+
break;
44+
case 'R' :
45+
destX = curX+5;
46+
destY = curY+5;
47+
curX++;
48+
49+
if(curX > 5) {
50+
curX--;
51+
break;
52+
}
53+
54+
if(garo[destX][destY] == 0){
55+
garo[destX][destY]=1;
56+
answer++;
57+
}
58+
break;
59+
case 'L' :
60+
destX = curX+4;
61+
destY = curY+5;
62+
curX--;
63+
64+
if(curX < -5) {
65+
curX++;
66+
break;
67+
}
68+
69+
if(garo[destX][destY] == 0){
70+
garo[destX][destY]=1;
71+
answer++;
72+
}
73+
break;
74+
75+
default :
76+
break;
77+
}
78+
79+
80+
}
81+
82+
int solution(string dirs) {
83+
84+
int dirs_length = dirs.length();
85+
86+
for(int i=0; i<dirs_length; i++){
87+
char c = dirs[i];
88+
89+
if(c == 'U') func('U');
90+
if(c == 'D') func('D');
91+
if(c == 'R') func('R');
92+
if(c == 'L') func('L');
93+
94+
}
95+
96+
return answer;
97+
}

0 commit comments

Comments
 (0)