[BOJ] 2174. 로봇 시뮬레이션
2021. 9. 19. 23:55ㆍ알고리즘/백준 문제 풀이
https://www.acmicpc.net/problem/2174
접근 방법
먼저 로봇의 index를 꺼냈을 때 로봇의 정보가 담아져있으면 되기 때문에
자료구조형은 HashMap<"Index(Integer)", Robot(정보들)> 을 가져갔다.
또한 좌표에 로봇이 있는지 없는지에 대한 정보와 좌표에 어떤 로봇이 있는지에 대해 2차 행렬로 표시하였다.
static HashMap<Integer, Robot> hashMap = new HashMap<>();
static int[][] map; // index값이 들어오게 된다.
...
class Robot{
int direction, x, y;
public Robot(int direction, int x, int y) {
this.direction = direction;
this.x = x;
this.y = y;
}
}
주의 사항
- 가로는 A, 세로의 크기는 B이다.
- x좌표는 왼쪽부터, y좌표는 아래쪽부터 순서가 매겨진다.
Need Know
- 구현
전체 코드 ( Java )
import java.io.*;
import java.util.HashMap;
import java.util.StringTokenizer;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int N,M;
static int A,B;
static int[][] robots;
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0}; // E N W S
static HashMap<Integer, Robot> hashMap = new HashMap<>();
public static void main(String[] args) throws IOException {
set();
}
static void set() throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
robots = new int[B][A];
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
int index = i+1;
int x = Integer.parseInt(st.nextToken())-1;
int y = Integer.parseInt(st.nextToken())-1;
String dir = st.nextToken();
int direction = 0;
if(dir.equals("E")) direction = 0;
else if( dir.equals("N")) direction = 1;
else if( dir.equals("W")) direction = 2;
else direction = 3;
hashMap.put(index, new Robot(direction,y,x));
robots[y][x] = index;
}
// System.out.println();
// for(int i=0; i<B; i++){
// for(int k=0; k<A; k++){
// System.out.print(robots[i][k]+" ");
// }
// System.out.println();
// }
for(int i=0; i<M; i++){
st = new StringTokenizer(br.readLine());
int index = Integer.parseInt(st.nextToken());
String b = st.nextToken();
int times = Integer.parseInt(st.nextToken());
String result = move(index, b, times);
if(!result.equals("OK")) {
System.out.println(result);
return;
}
}
System.out.println("OK");
}
static String move(int index, String order, int times){
Robot robot = hashMap.get(index);
int newDirection = 0;
int nx = 0;
int ny = 0;
if(order.equals("L")){// 왼쪽으로 90도 회전
newDirection = (robot.direction + times) % 4;
hashMap.put(index, new Robot(newDirection, robot.x, robot.y));
}else if(order.equals("R")){ //오른쪽으로 90도 회전
newDirection = (robot.direction - times) % 4;
if(newDirection < 0){
newDirection = 4 + newDirection;
}
hashMap.put(index, new Robot(newDirection, robot.x, robot.y));
}else { // 앞으로 한칸 전진
for(int i=1; i<=times; i++){
nx = (dx[robot.direction]*i) + robot.x;
ny = (dy[robot.direction]*i) + robot.y;
if(nx < 0 || ny < 0 || nx>=B || ny >= A){
return "Robot "+index+" crashes into the wall";
}
if(robots[nx][ny] != 0){
return "Robot "+index+" crashes into robot "+robots[nx][ny];
}
}
robots[robot.x][robot.y] = 0;
robots[nx][ny] = index;
hashMap.put(index, new Robot(robot.direction, nx, ny));
}
return "OK";
}
}
class Robot{
int direction, x, y;
public Robot(int direction, int x, int y) {
this.direction = direction;
this.x = x;
this.y = y;
}
}
'알고리즘 > 백준 문제 풀이' 카테고리의 다른 글
[BOJ] 1013. Contact (0) | 2021.09.25 |
---|---|
[BOJ] 1107. 리모콘 (0) | 2021.09.25 |
[BOJ] 10800. 컬러볼 (0) | 2021.09.18 |
[BOJ] 20056. 마법사 상어와 파이어볼 (0) | 2021.09.18 |
[BOJ] 1182. 부분수열의 합 (0) | 2021.04.21 |