https://school.programmers.co.kr/learn/courses/30/lessons/172928
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
주어진 방향에 따라서 장애물이 있는지 , 범위를 넘었는지를 확인 한 후 둘 중에 하나라도 해당 하지 않으면 다음 명령을 따른다.
이차원 배열로 길을 만들어 두고 주어진 명령으로 해당 맵을 확인 하는 식으로 코드를 짰다. IDE를 사용하지 않고 짜다 보니 계속 되는 if 문 for문에 헷갈렸다. 주석을 다는 것이 좋은거 같다.
class Solution {
public int[] solution(String[] park, String[] routes) {
int[] answer = {};
boolean flag = true;
String[][] road = new String[park.length][park[0].length()];
int x = 0;
int y = 0;
for (int i = 0; i < park.length; i++) {
String a = park[i];
for (int j = 0; j < park[0].length(); j++) {
road[i][j] = String.valueOf(a.charAt(j));
if (road[i][j].equals("S")) {
x = j;
y = i;
}
System.out.print(road[i][j]);
}
System.out.println();
}
for (int i = 0; i < routes.length; i++) {
String[] route = routes[i].split(" ");
if (route[0].equals("E")) {
if (x + Integer.parseInt(route[1]) >= park[0].length()) {
continue;
} else {
for (int k = x; k <= x + Integer.parseInt(route[1]); k++) {
if (road[y][k].equals("X")) {
flag = false;
break;
}
}
if(flag){
x += Integer.parseInt(route[1]);
}
}
} else if (route[0].equals("S")) {
if (y + Integer.parseInt(route[1]) >= park.length) {
continue;
} else {
for (int k = y; k <= y + Integer.parseInt(route[1]); k++) {
if (road[k][x].equals("X")) {
flag = false;
break;
}
}
if(flag){
y += Integer.parseInt(route[1]);
}
}
} else if (route[0].equals("W")) {
if (x - Integer.parseInt(route[1]) <= -1) {
continue;
} else {
for (int k = x; k >= x - Integer.parseInt(route[1]); k--) {
if (road[y][k].equals("X")) {
flag = false;
break;
}
}
if(flag){
x -= Integer.parseInt(route[1]);
}
}
} else {
if (y - Integer.parseInt(route[1]) <= -1) {
continue;
} else {
for (int k = y; k >= y - Integer.parseInt(route[1]); k--) {
if (road[k][x].equals("X")) {
flag = false;
break;
}
}
if(flag){
y -= Integer.parseInt(route[1]);
}
}
}
flag = true;
}
answer =new int[] {y,x};
return answer;
}
}
'PS' 카테고리의 다른 글
[Programmers] [PCCP 기출문제] 2번 / 퍼즐 게임 챌린지 (1) | 2024.11.19 |
---|---|
[Programmers] [PCCE 기출문제] 9번 / 이웃한 칸 (0) | 2024.11.15 |
프로그래머스 바탕화면 정리 (1) | 2023.11.18 |
프로그래머스 특수문자 출력하기 (0) | 2023.11.18 |
[Lv0] 대소문자 바꿔서 출력하기 (0) | 2023.08.13 |