알고리즘
[프로그래머스, 자바] 캐릭터의 좌표
hminor
2024. 3. 15. 16:47
반응형
풀이
- 간단하게 board의 길이에 따른 최대 크기를 mx에 담아둔 다음
- 방향에 따라 answer의 값을 추가 여기서 차감할 때는 abs 메서드를 사용해서 확인하기
class Solution {
public int[] solution(String[] keyinput, int[] board) {
int[] mx = new int[]{board[0]/2,board[1]/2};
int[] answer = new int[]{0,0};
for (String key: keyinput) {
if (key.equals("right") && mx[0] >= answer[0]+1) answer[0] += 1;
else if (key.equals("left") && mx[0] >= Math.abs(answer[0]-1)) answer[0] -= 1;
else if (key.equals("up") && mx[1] >= answer[1]+1) answer[1] += 1;
else if (key.equals("down") && mx[1] >= Math.abs(answer[1]-1)) answer[1] -= 1;
}
return answer;
}
}