곽로그

[백준 14503, Java] 로봇청소기 본문

알고리즘/백준

[백준 14503, Java] 로봇청소기

일도이동 2021. 2. 25. 17:26
반응형

문제

www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어

www.acmicpc.net

 

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

class Robot{
    int x, y, direction, clearCount;
    Robot(int x, int y, int direction, int clearCount){
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.clearCount = clearCount;
    }

    @Override
    public String toString() {
        return "Robot{" +
                "x=" + x +
                ", y=" + y +
                ", direction=" + direction +
                ", clearCount=" + clearCount +
                '}';
    }
}
public class Main {
    static int N;
    static int M;
    static int[][] map;
    static boolean[][] isCleared;
    static final int EMPTY = 0;
    static final int WALL = 1;
    static int[] dx = {-1,0,1,0};
    static int[] dy = {0,1,0,-1};

    static Robot robot;


    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;

        st = new StringTokenizer(br.readLine()," ");
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        map = new int[N][M];
        isCleared = new boolean[N][M];


        st = new StringTokenizer(br.readLine()," ");
        int x = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        int direction = Integer.parseInt(st.nextToken());
        robot = new Robot(x, y, direction,0);

        for(int r=0; r<N; r++){
            st = new StringTokenizer(br.readLine()," ");
            for(int c=0; c<M; c++){
                map[r][c]  =Integer.parseInt(st.nextToken());
            }
        }

        int result = getNumberOfTileCleared(robot);
        bw.write(String.valueOf(result));
        bw.flush();

    }
    static int getNumberOfTileCleared(Robot robot){
        while(true){
            //현재 위치 청소
            isCleared[robot.x][robot.y] =true;
            ++robot.clearCount;

            boolean isBlocked = false;
            while(true) {
                //이동방향 정하기
                boolean isMoved = false;

                for (int d = robot.direction + 7; d > robot.direction + 3; d--) {
                    int nx = robot.x + dx[d % 4];
                    int ny = robot.y + dy[d % 4];

                    if (nx >= 0 && ny >= 0 && nx < N && ny < M) {
                        if (map[nx][ny] == EMPTY && !isCleared[nx][ny]) {
                            isMoved = true;
                            robot.x = nx;
                            robot.y = ny;
                            robot.direction = d % 4;
                            break;
                        }
                    }
                }


                //어느방향으로도 이동하지 않았다면 뒤로 후진할 수 있는지 확인
                if(isMoved){
                    break;
                }
                else {
                    int backDirection = (robot.direction + 2) % 4;
                    int bx = robot.x + dx[backDirection];
                    int by = robot.y + dy[backDirection];

                    if (bx < 0 || by < 0 || bx >= N || by >= M || map[bx][by] == WALL) {
                        isBlocked = true;
                        break;
                    } else {
                        robot.x = bx;
                        robot.y = by;
                    }
                }
            }
            if(isBlocked){
                break;
            }
        }
        return robot.clearCount;
    }
}
반응형

'알고리즘 > 백준' 카테고리의 다른 글

[백준 13458, Java] 시험 감독  (0) 2021.03.19
[백준 3190, Java] 뱀  (0) 2021.02.26
[백준 19237, Java] 어른상어  (0) 2021.02.24
[백준 16948, Java] 데스 나이트  (0) 2021.02.18
[백준 16235, Java] 나무 재테크  (0) 2021.02.17
Comments