
static int[] dx = {1, -1, 0, 0, 1, -1, 1, -1}; static int[] dy = {0, 0, -1, 1, 1, 1, -1, -1}; {"R", "L", "B", "T", "RT", "LT", "RB", "LB"};각 커맨드의 좌표를 문제에 나와있는 순서대로 나타낸 것이다. move 함수를 통해 킹의 위치와 돌의 위치를 옮긴다.
시간복잡도: O(N), 공간복잡도: O(1)
import java.util.*;
import java.io.*;
class Main {
static int[] dx = {1, -1, 0, 0, 1, -1, 1, -1};
static int[] dy = {0, 0, -1, 1, 1, 1, -1, -1};
static String[] moves = {"R", "L", "B", "T", "RT", "LT", "RB", "LB"};
static int kingX,kingY,stoneX,stoneY;
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
String king = st.nextToken();
String stone = st.nextToken();
int n = Integer.parseInt(st.nextToken());
kingX = king.charAt(0) - 'A';
kingY = king.charAt(1) - '1';
stoneX = stone.charAt(0) - 'A';
stoneY = stone.charAt(1) - '1';
for(int i=0;i<n;i++){
String command = br.readLine();
move(command);
}
System.out.println((char) ('A' + kingX) + "" + (kingY + 1));
System.out.println((char) ('A' + stoneX) + "" + (stoneY + 1));
}
public static void move(String command){
for(int d=0;d<8;d++){
if(moves[d].equals(command)){
int nx = kingX + dx[d];
int ny = kingY + dy[d];
if(nx < 0 || nx >= 8 || ny < 0 || ny >= 8) continue;
if(nx == stoneX && ny == stoneY){
int sx = stoneX + dx[d];
int sy = stoneY + dy[d];
if(sx < 0 || sx >= 8 || sy < 0 || sy >= 8)continue;
stoneX = sx;
stoneY = sy;
}
kingX = nx;
kingY = ny;
break;
}
}
}
}
