[백준] 로봇 청소기 - Java

JeongYong·2023년 6월 21일
0

Algorithm

목록 보기
172/263

문제 링크

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

문제

문제 링크 참조

입력

문제 링크 참조

출력

로봇 청소기가 작동을 시작한 후 작동을 멈출 때까지 청소하는 칸의 개수를 출력한다.

알고리즘: 구현, 시뮬레이션

풀이

기본 구현 문제이다.

이 문제에서 요구하는 핵심 기능을 정의하자면, 다음과 같이 3가지로 정의할 수 있다.

  1. 로봇 주변 검사하는 메소드

  2. 후진 가능한지 검사하는 메소드

  3. 로봇을 반시계 방향으로 회전하는 메소드

1 -> 현재 로봇 위치에서 북, 동, 남, 서를 검사하면 된다.

2 -> 후진은 로봇의 현재 진행 방향에 반대 방향을 검사하면 된다.
북, 동, 남, 서는 차례대로 0, 1, 2, 3값을 가지기 때문에 반대 방향은 (현재 방향+2) % 4 다.

3 -> 반시계 90도 회전은 현재 진행 방향에 -1를 해준다. (북 0, 동 1, 남 2, 서 3)
(진행 방향 -1) 값이 -1이면 3을 넣어주고 그 외는 그 값 그대로 사용하면 된다.

소스 코드

import java.util.*;
import java.io.*;
class Point {
    int x, y, d;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    Point(int x, int y, int d) {
        this.x = x;
        this.y = y;
        this.d = d;
    }
}
public class Main {
    static final int[] dx = {0, 1, 0, -1}; //북, 동, 남, 서
    static final int[] dy = {-1, 0, 1, 0};
    static int H, W;
    static Point robot;
    static int[][] room;
    static int answer = 0;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        H = sc.nextInt();
        W = sc.nextInt();
        int sy = sc.nextInt();
        int sx = sc.nextInt();
        robot = new Point(sx, sy, sc.nextInt());
        room = new int[H][W];
        for(int i=0; i<H; i++) {
            for(int j=0; j<W; j++) {
                room[i][j] = sc.nextInt();
            }
        }
        while(true) {
            if(room[robot.y][robot.x] == 0) {
                answer += 1;
                room[robot.y][robot.x] = -1; //청소함 표시
            }
            if(check_around()) {
                for(int i=0; i<4; i++) {
                    rotation_robot(); //회전
                    int nx = robot.x + dx[robot.d];
                    int ny = robot.y + dy[robot.d];
                    if(check_range(new Point(nx, ny)) && room[ny][nx] == 0) {
                        robot = new Point(nx, ny, robot.d);
                        break;
                    }
                }
            } else {
                if(check_R()) {
                    robot = new Point(robot.x + dx[(robot.d + 2) % 4], robot.y + dy[(robot.d + 2) % 4], robot.d);
                } else break;
            }
        }
        System.out.println(answer);
    }
    
    static boolean check_around() {
        for(int i=0; i<4; i++) {
            int nx = robot.x + dx[i];
            int ny = robot.y + dy[i];
            if(check_range(new Point(nx, ny)) && room[ny][nx] == 0) return true;
        }
        return false;
    }
    
    static boolean check_range(Point p) {
        if((0 <= p.x && p.x <= W-1) && (0 <= p.y && p.y <= H-1)) return true;
        return false;
    }
    
    static boolean check_R() {
        int nx = robot.x + dx[(robot.d + 2) % 4];
        int ny = robot.y + dy[(robot.d + 2) % 4];
        if(check_range(new Point(nx, ny)) && room[ny][nx] != 1) return true;
        return false;
    }
    
    static void rotation_robot() {
        //반시계 방향으로 회전
        int rd = robot.d - 1;
        if((robot.d - 1) == -1) rd = 3;
        robot = new Point(robot.x, robot.y, rd);
    }
}

0개의 댓글