728x90
package Implementation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class robot {
static int M,N;
static int[]dx = {1,0,-1,0}; //동서남북
static int[]dy = {0,-1,0,1};
// static int[][]map;
// static boolean[][][]visited;
public static boolean canGo(int x, int y, int n)
{
return x >= 0 && y >= 0 && x < n && y < n ;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
int dir=0;
int x =0; int y =0;
for(int i=0; i<N; i++){
String order;
int num;
StringTokenizer stoken = new StringTokenizer(br.readLine());
order = stoken.nextToken();
num = Integer.parseInt(stoken.nextToken());
int nx,ny;
if(order.equals( "MOVE")){
nx = x+dx[dir]*num;
ny = y+dy[dir]*num;
if(canGo(nx,ny,M)){x=nx; y=ny;}
else{
System.out.println(-1);
return;
}
}
else if(order.equals("TURN")){
if(num==0){ //왼쪽으로 90도 회전
dir = (dir+4-1)%4;
}
else{ //오른쪽으로 90도회전
dir = (dir+1)%4;
}
}
}
System.out.println(x+" "+y);
}
}