|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class DH_자율주행_자동차 { |
| 5 | + // 자동차의 위치, 바라보는 방향을 저장하는 클래스 |
| 6 | + static class Car { |
| 7 | + int r, c, d; |
| 8 | + public Car(int r, int c, int d) { |
| 9 | + this.r = r; |
| 10 | + this.c = c; |
| 11 | + this.d = d; |
| 12 | + } |
| 13 | + } |
| 14 | + static Car car; |
| 15 | + static int dr[] = {-1, 0, 1, 0}; |
| 16 | + static int dc[] = {0, 1, 0, -1}; |
| 17 | + // map: 도로와 자동차가 움직인 정보를 가지고 있음 |
| 18 | + // - 0: 갈 수 있는 도로, 1: 인도, 2: 이미 지나간 도로 |
| 19 | + // result: 자동차가 움직인 도롤의 총 면적 |
| 20 | + static int map[][], result = 1; |
| 21 | + |
| 22 | + static void solution() { |
| 23 | + map[car.r][car.c] = 2; |
| 24 | + |
| 25 | + while(true) { |
| 26 | + // 네 방향을 기준으로 왼쪽으로 갈 수 있는지 확인하하기 위한 변수 |
| 27 | + int cnt = 0; |
| 28 | + // 왼쪽으로 갈 수 있는지 확인 |
| 29 | + if(!canMoveLeft()) { |
| 30 | + // 자동차를 회전시키면서 왼쪽으로 갈 수 있는지 확인 |
| 31 | + while(cnt++ < 3) if(canMoveLeft()) break; |
| 32 | + |
| 33 | + // cnt가 4라면 모든 방향에 대해 확인해봤지만 갈 수 없는 상황 |
| 34 | + if(cnt == 4) { |
| 35 | + int nr = car.r - dr[car.d]; |
| 36 | + int nc = car.c - dc[car.d]; |
| 37 | + |
| 38 | + updateCarInfo(nr, nc, car.d); |
| 39 | + if(!check(nr, nc) || map[nr][nc] == 1) break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + System.out.println(result); |
| 45 | + } |
| 46 | + |
| 47 | + // 왼쪽으로 갈 수 있는지 확인 |
| 48 | + static boolean canMoveLeft() { |
| 49 | + int d = (car.d - 1 + 4) % 4; |
| 50 | + int nr = car.r + dr[d]; |
| 51 | + int nc = car.c + dc[d]; |
| 52 | + |
| 53 | + car.d = d; |
| 54 | + if(!check(nr, nc) || map[nr][nc] != 0) return false; |
| 55 | + updateCarInfo(nr, nc, d); |
| 56 | + map[nr][nc] = 2; |
| 57 | + result++; |
| 58 | + |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + // 자동차의 정보 업데이트 |
| 63 | + static void updateCarInfo(int r, int c, int d) { |
| 64 | + car.r = r; |
| 65 | + car.c = c; |
| 66 | + car.d = d; |
| 67 | + } |
| 68 | + static boolean check(int r, int c) { |
| 69 | + return r >= 0 && r < map.length && c >= 0 && c < map[0].length; |
| 70 | + } |
| 71 | + public static void main(String[] args) throws Exception { |
| 72 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 73 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 74 | + |
| 75 | + int n = Integer.parseInt(st.nextToken()); |
| 76 | + int m = Integer.parseInt(st.nextToken()); |
| 77 | + map = new int[n][m]; |
| 78 | + |
| 79 | + st = new StringTokenizer(br.readLine()); |
| 80 | + int cr = Integer.parseInt(st.nextToken()); |
| 81 | + int cc = Integer.parseInt(st.nextToken()); |
| 82 | + int cd = Integer.parseInt(st.nextToken()); |
| 83 | + |
| 84 | + car = new Car(cr, cc, cd); |
| 85 | + for(int r = 0; r < map.length; r++) { |
| 86 | + String s = br.readLine(); |
| 87 | + for(int c = 0; c < map[0].length; c++) { |
| 88 | + map[r][c] = s.charAt(c * 2) - '0'; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + solution(); |
| 93 | + } |
| 94 | +} |
0 commit comments