알고리즘

백준 14503번 로봇 청소기 (JAVA)

박카스마시며코딩 2023. 11. 24. 21:02

https://www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$  둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽

www.acmicpc.net

 

저는 구현을 통해 문제를 해결하였습니다.

먼저 현재칸을 청소하고, 4방향으로 청소하지 않은 곳을 찾고 없다면 바로 끝내고 있다면 그 방향까지 방향을 돌리고 한칸 전진하였습니다. 위의 방식을 끝날 때 까지 하여 문제를 해결하였습니다.

 

package BOJ.simulation;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_14503_2 {
        private static final int[] DY = {-1,0,1,0};
        private static final int[] DX = {0,1,0,-1};
        private static final int BLOCK = 1;
        private static final int EMPTY = 0;
        public static void main(String[] args) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer(br.readLine());
            int n = Integer.parseInt(st.nextToken());
            int m = Integer.parseInt(st.nextToken());
            int[][] map = new int[n][m];
            st = new StringTokenizer(br.readLine());
            int startY = Integer.parseInt(st.nextToken());
            int startX = Integer.parseInt(st.nextToken());
            int dir = Integer.parseInt(st.nextToken());
            for(int i = 0 ; i < n; i++){
                st = new StringTokenizer(br.readLine());
                for(int j = 0 ;j < m ; j++){
                    map[i][j] = Integer.parseInt(st.nextToken());
                }
            }
            int result = cal(startY,startX,dir,map,n,m);
            System.out.println(result);
        }

        private static int cal(int startY, int startX, int dir, int[][] map, int n, int m) {
            int cnt = 0;
            boolean[][] isClean = new boolean[n][m];
            int y = startY;
            int x = startX;
            while(true){
                if(!isClean[y][x]){
                    isClean[y][x] = true;
                    cnt++;
                }
                if(haveNearNoClean(y,x,isClean,map,n,m)){ // 4칸 중 청소되지 않은 칸이 있다면
                    dir = (dir + 3) % 4; // 90도 회전
                    int ny = y + DY[dir];
                    int nx = x + DX[dir];
                    while(map[ny][nx] == BLOCK || isClean[ny][nx]){
                        dir = (dir + 3) % 4;
                        ny = y + DY[dir];
                        nx = x + DX[dir];
                    }
                    // 한칸 전진
                    y = ny;
                    x = nx;
                    continue;
                }

                int ny = y - DY[dir];
                int nx = x - DX[dir];
                if(ny < 0 || ny >= n || nx < 0 || nx >= m || map[ny][nx] == BLOCK){ // 뒤가 벽이거나 밖으로 나가지게 된다면
                    break;
                }
                // 후진
                y = ny;
                x = nx;
            }
            return cnt;
        }

        private static boolean haveNearNoClean(int y, int x, boolean[][]isClean ,int[][] map, int n, int m) {
            for(int i = 0 ; i < 4 ; i++){
                int ny = y + DY[i];
                int nx = x + DX[i];
                if(ny >= 0 && ny < n && nx >= 0 && nx < m && map[ny][nx] != BLOCK && !isClean[ny][nx]){
                    return true;
                }
            }
            return false;
        }
    }

'알고리즘' 카테고리의 다른 글

백준 1141번 접두사 (JAVA)  (0) 2023.11.26
백준 12100번 2048 (JAVA)  (1) 2023.11.25
백준 3190번 뱀 (JAVA)  (0) 2023.11.23
백준 2512번 예산 (JAVA)  (0) 2023.11.22
백준 2003번 수들의 합2 (JAVA)  (1) 2023.11.21