Coding Question
프로그래머스 : 공원산책
코딩하는쿼카
2023. 3. 29. 23:22
https://school.programmers.co.kr/learn/courses/30/lessons/172928
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
게임프로그래머를 희망하는 친구들에게 추천한다.
이것 역시 유형만 안다면 어렵지는 않다.
첫 위치(S)만 따주고 그 좌표를 바탕으로 이동하면 된다.
여기서 난이도를 결정한 조건이 나오는데,
벽을 만나거나, 장애물을 만나면 이동 전 위치로 돌아가는 것이다.
해당위치에 정지하는 것이였으면 대략 2레벨쯤 됬을 것 같다.
class Solution {
public int[] solution(String[] park, String[] routes) {
String[][] round = new String[park.length][park[0].length()];
for(int i=0; i<park.length; i++){
for(int j=0; j<park[0].length(); j++){
round[i][j] = String.valueOf(park[i].charAt(j));
}
}
int x = 0;
int y = 0;
for(int i = 0; i<round.length; i++){
for(int j=0; j<round[i].length; j++){
if(round[i][j].equals("S")){
x = i;
y = j;
break;
}
}
}
for(int a=0; a<routes.length; a++){
char dir = routes[a].charAt(0); // 방향 동서남북
int len = Integer.parseInt(String.valueOf(routes[a].charAt(2))); // 방향에 따른 길이 변화.
if(dir == 'E'){
for(int i=0; i<len; i++){
try {
if (!round[x][y + 1].equals("X")) {
y += 1;
} else {
y = y - i;
break;
}
}
catch (Exception e){
y = y - i;
break;
}
}
}
else if(dir == 'S'){
for(int i=0; i<len; i++){
try {
if (!round[x+1][y].equals("X")) {
x += 1;
} else {
x = x - i;
break;
}
}
catch (Exception e){
x = x - i;
break;
}
}
}
else if(dir == 'N'){
for(int i=0; i<len; i++){
try {
if (!round[x-1][y].equals("X")) {
x -= 1;
} else {
x = x + i;
break;
}
}
catch (Exception e){
x = x +i;
break;
}
}
}
else {
for(int i=0; i<len; i++){
try {
if (!round[x][y - 1].equals("X")) {
y -= 1;
} else {
y = y + i;
break;
}
}
catch (Exception e){
y = y + i;
break;
}
}
}
}
int[] arr = {x,y};
return arr;
}
}
한 30분 정도 걸린 것 같다