백준 32932번 드론 조작 JAVA

YB·2025년 3월 13일

링크텍스트

설명

x, y의 범위가 -500부터 500이므로 이를 배열 인덱스로 활용하기 위해 배열의 크기를 1001로 선언하였다. 이후 출력 시에는 원래 좌표값을 복원하기 위해 500을 빼주었다.
시간복잡도: O(N+K), 공간복잡도: O(1)

회독

PASS

코드

import java.util.*;
import java.io.*;

class Main {
	public static void main (String[] args) throws IOException{
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

		int n = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());

		int [][] arr = new int[1001][1001];
		char [] cmd = new char[k];

		for(int i=0;i<n;i++){
			st = new StringTokenizer(br.readLine());
			arr[Integer.parseInt(st.nextToken())+500][Integer.parseInt(st.nextToken())+500] = 1;
		}

		int x=500, y=500;

		String s = br.readLine();
		for(int i=0;i<k;i++){
			cmd[i]=s.charAt(i);

			if(cmd[i]=='U' && y<=1000 && arr[x][y+1]!=1){
				y++;
			}else if(cmd[i]=='D' && y>=0 && arr[x][y-1]!=1){
				y--;
			}else if(cmd[i]=='R' && x<=1000 && arr[x+1][y]!=1){
				x++;
			}else if(cmd[i]=='L' && x>=0 && arr[x-1][y]!=1){
				x--;
			}
		}

		System.out.println((x-500)+" "+(y-500));
		
	}
}

profile
안녕하세요

0개의 댓글