Skip to content

Commit dfda1b4

Browse files
committed
[level 0] Title: 캐릭터의 좌표, Time: 0.02 ms, Memory: 64.2 MB -BaekjoonHub
1 parent 5ac4b2d commit dfda1b4

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [level 0] 캐릭터의 좌표 - 120861
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/120861)
4+
5+
### 성능 요약
6+
7+
메모리: 64.2 MB, 시간: 0.02 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 코딩테스트 입문
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 05월 22일 08:08:07
20+
21+
### 문제 설명
22+
23+
<p>머쓱이는 RPG게임을 하고 있습니다. 게임에는 <code>up</code>, <code>down</code>, <code>left</code>, <code>right</code> 방향키가 있으며 각 키를 누르면 위, 아래, 왼쪽, 오른쪽으로 한 칸씩 이동합니다. 예를 들어 [0,0]에서 <code>up</code>을 누른다면 캐릭터의 좌표는 [0, 1], <code>down</code>을 누른다면 [0, -1], <code>left</code>를 누른다면 [-1, 0], <code>right</code>를 누른다면 [1, 0]입니다. 머쓱이가 입력한 방향키의 배열 <code>keyinput</code>와 맵의 크기 <code>board</code>이 매개변수로 주어집니다. 캐릭터는 항상 [0,0]에서 시작할 때 키 입력이 모두 끝난 뒤에 캐릭터의 좌표 [x, y]를 return하도록 solution 함수를 완성해주세요.</p>
24+
25+
<ul>
26+
<li>[0, 0]은 <code>board</code>의 정 중앙에 위치합니다. 예를 들어 <code>board</code>의 가로 크기가 9라면 캐릭터는 왼쪽으로 최대 [-4, 0]까지 오른쪽으로 최대 [4, 0]까지 이동할 수 있습니다.</li>
27+
</ul>
28+
29+
<hr>
30+
31+
<h5>제한사항</h5>
32+
33+
<ul>
34+
<li><code>board</code>은 [가로 크기, 세로 크기] 형태로 주어집니다.</li>
35+
<li><code>board</code>의 가로 크기와 세로 크기는 홀수입니다.</li>
36+
<li><code>board</code>의 크기를 벗어난 방향키 입력은 무시합니다.</li>
37+
<li>0 ≤ <code>keyinput</code>의 길이 ≤ 50</li>
38+
<li>1 ≤ <code>board</code>[0]&nbsp;≤ 99</li>
39+
<li>1 ≤ <code>board</code>[1] ≤ 99</li>
40+
<li><code>keyinput</code>은 항상 <code>up</code>, <code>down</code>, <code>left</code>, <code>right</code>만 주어집니다.</li>
41+
</ul>
42+
43+
<hr>
44+
45+
<h5>입출력 예</h5>
46+
<table class="table">
47+
<thead><tr>
48+
<th>keyinput</th>
49+
<th>board</th>
50+
<th>result</th>
51+
</tr>
52+
</thead>
53+
<tbody><tr>
54+
<td>["left", "right", "up", "right", "right"]</td>
55+
<td>[11, 11]</td>
56+
<td>[2, 1]</td>
57+
</tr>
58+
<tr>
59+
<td>["down", "down", "down", "down", "down"]</td>
60+
<td>[7, 9]</td>
61+
<td>[0, -4]</td>
62+
</tr>
63+
</tbody>
64+
</table>
65+
<hr>
66+
67+
<h5>입출력 예 설명</h5>
68+
69+
<p>입출력 예 설명 #1</p>
70+
71+
<ul>
72+
<li>[0, 0]에서 왼쪽으로 한 칸 오른쪽으로 한 칸 위로 한 칸 오른쪽으로 두 칸 이동한 좌표는 [2, 1]입니다.</li>
73+
</ul>
74+
75+
<p>입출력 예 설명 #2</p>
76+
77+
<ul>
78+
<li>[0, 0]에서 아래로 다섯 칸 이동한 좌표는 [0, -5]이지만 맵의 세로 크기가 9이므로 아래로는 네 칸을 넘어서 이동할 수 없습니다. 따라서 [0, -4]를 return합니다.</li>
79+
</ul>
80+
81+
82+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int[] solution(String[] keyinput, int[] board) {
3+
int[] answer = new int[2];
4+
5+
for(String input : keyinput){
6+
int x = board[0];
7+
int y = board[1];
8+
int curX = answer[0];
9+
int curY = answer[1];
10+
11+
if(input.equals("left") && curX-1>=x/2*(-1)){
12+
answer[0] = curX-1;
13+
}
14+
if(input.equals("right") && curX+1 <= x/2){
15+
answer[0] = curX+1;
16+
}
17+
if(input.equals("up") && curY+1 <= y/2){
18+
answer[1] = curY+1;
19+
}
20+
if(input.equals("down") && curY-1 >= y/2*(-1)){
21+
answer[1] = curY-1;
22+
}
23+
}
24+
25+
26+
return answer;
27+
}
28+
}

0 commit comments

Comments
 (0)