import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) {
String[] park = {"SOO","OOO","OOO"};
String[] routes = {"E 2","S 2","W 1"};
String[] park_1 = {"SOO","OXX","OOO"};
String[] routes_1 = {"E 2","S 2","W 1"};
String[] park_2 = {"OSO","OOO","OXO","OOO"};
String[] routes_2 = {"E 2","S 3","W 1"};
//int[] a = solution(park, routes);
//System.out.println(a[0]+","+a[1]);
//int[] b = solution(park_1, routes_1);
//System.out.println(b[0]+","+b[1]);
int[] c = solution(park_2, routes_2);
System.out.println(c[0]+","+c[1]);
}
public static int[] solution(String[] park, String[] routes) {
char[][] c_park = java.util.Arrays.stream(park)
.map(String::toCharArray)
.toArray(char[][]::new);
int rows = park.length;
int cols = park[0].length();
int[] current = {-1, -1};
List<int[]> positions_x = new ArrayList<>();
List<int[]> positions_o = new ArrayList<>();
outerLoop:
for (int i = 0; i < rows; i++) { // 행(row) 탐색
for (int j = 0; j < cols; j++) { // 열(col) 탐색
if (c_park[i][j] == 'S') {
current[0] = i;
current[1] = j;
System.out.println("Start: [ "+i+", "+j+" ]");
break outerLoop;
}
}
}
for (String route : routes) {
String op = route.split(" ")[0];
int n = Integer.parseInt(route.split(" ")[1]);
int c = 0;
switch (op) {
case "N":
if (current[0] - n < 0) { System.out.println("[ X ] : You can't go to [ N ]!"); break; }
c = current[0];
for (int i = 0; i < n; i++) {
c--;
if(c_park[c][current[1]] == 'X') { System.out.println("[ X ] : You can't go to [ N ]!"); c = -1; break; }
}
if(c != -1) {
System.out.println("[ O ] You can go to [ "+op+" ]!");
current = new int[]{ current[0] - n, current[1] };
System.out.println("[ "+current[0]+", "+current[1]+" ]");
}
break;
case "S":
if (current[0] + n >= rows) { System.out.println("[ X ] : You can't go to [ S ]!"); break; }
c = current[0];
for (int i = 0; i < n; i++) {
c++;
if(c_park[c][current[1]] == 'X') { System.out.println("[ X ] : You can't go to [ S ]!"); c = -1; break; }
}
if(c != -1) {
System.out.println("[ O ] You can go to [ "+op+" ]!");
current = new int[]{ current[0] + n, current[1] };
System.out.println("[ "+current[0]+", "+current[1]+" ]");
}
break;
case "W":
if (current[1] - n < 0) { System.out.println("[ X ] : You can't go to [ W ]!"); break; }
c = current[1];
for (int i = 0; i < n; i++) {
c--;
if(c_park[current[0]][c] == 'X') { System.out.println("[ X ] : You can't go to [ W ]!"); c = -1; break; }
}
if(c != -1) {
System.out.println("[ O ] You can go to [ "+op+" ]!");
current = new int[]{ current[0], current[1] - n };
System.out.println("[ "+current[0]+", "+current[1]+" ]");
}
break;
case "E":
if (current[1] + n >= cols) { System.out.println("[ X ] : You can't go to [ E ]!"); break; }
c = current[1];
for (int i = 0; i < n; i++) {
c++;
if(c_park[current[0]][c] == 'X') { System.out.println("[ X ] : You can't go to [ E ]!"); c = -1; break; }
}
if(c != -1) {
System.out.println("[ O ] You can go to [ "+op+" ]!");
current = new int[]{ current[0], current[1] + n };
System.out.println("[ "+current[0]+", "+current[1]+" ]");
}
break;
}
}
return current;
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: