반응형
https://www.acmicpc.net/problem/2174
로직 짜는 것 보다 인풋받기랑 배열 가로세로 바꿔놓고 함정이 많은 문제
어렵다기보다 치사한 시뮬레이션 문제다.
import java.io.*;
import java.util.*;
class Robot{
int row, col, dir;
public Robot(int row, int col, int dir) {
this.row = row;
this.col = col;
this.dir = dir;
}
}
public class 로봇시뮬레이션 {
public static final int[] dx = {-1,0,1,0};
public static final int[] dy = {0,1,0,-1};
public static Queue<Robot> q;
public static Queue<Robot> roboQ;
public static int map[][];
public static int A,B;
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
map = new int[B][A];
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int number = 1;
q = new LinkedList<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int col = Integer.parseInt(st.nextToken()) - 1;
int row = B - Integer.parseInt(st.nextToken());
int dir = st.nextToken().charAt(0);
dir = SetDir(dir);
q.add(new Robot(row, col, dir));
map[row][col] = number++;
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int kind =Integer.parseInt(st.nextToken());
char order = st.nextToken().charAt(0);
int time =Integer.parseInt(st.nextToken());
int size= q.size();
for (int j = 0; j < size; j++) {
Robot robo = q.poll();
if(map[robo.row][robo.col] == kind) {
move(robo, order, time);
}
q.add(robo);
}
}
System.out.println("OK");
}
private static int SetDir(int dir) {
if(dir == 'N') dir= 0;
else if(dir == 'E') dir= 1;
else if(dir == 'S') dir= 2;
else if(dir == 'W') dir= 3;
return dir;
}
public static void move(Robot robo, char order, int time) {
for (int i = 0; i < time; i++) {
switch(order) {
case 'L':{
robo.dir = (robo.dir+3)%4;
break;
}
case 'R': {
robo.dir = (robo.dir+1)%4;
break;
}
case 'F':{
int nx = robo.row + dx[robo.dir];
int ny = robo.col + dy[robo.dir];
if(nx < 0 || nx >= B || ny < 0 || ny >= A) {
System.out.println("Robot " + map[robo.row][robo.col]+ " crashes into the wall");
System.exit(0);
}
if(map[nx][ny] > 0) {
System.out.println("Robot " + map[robo.row][robo.col]+
" crashes into robot " + map[nx][ny]);
System.exit(0);
}
map[nx][ny] = map[robo.row][robo.col];
map[robo.row][robo.col] = 0;
robo.row = nx;
robo.col = ny;
break;
}
}
}
}
}
반응형
'2. 알고리즘사이트 > 1. 백준' 카테고리의 다른 글
줄 세우기 [백준 2252][골드2][Java] (0) | 2020.03.04 |
---|---|
공주님을 구해라! [백준 17836][골드5][Java] (0) | 2020.03.03 |
프린터 큐 [백준 1966][실버3][Java] (1) | 2020.03.03 |
괄호의 값 [백준 2504][실버2][Java] (0) | 2020.03.03 |
Puyo Puyo [백준 11559][골드5][Java] (0) | 2020.03.02 |