백준 14503 로봇 청소기 (Java,자바)

jonghyukLee·2022년 2월 7일
0

이번에 풀어본 문제는
백준 14503번 로봇 청소기 입니다.

📕 문제 링크

❗️코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

class Robot
{
    int x,y,dir;

    public Robot(int x, int y, int dir) {
        this.x = x;
        this.y = y;
        this.dir = dir;
    }
}
public class Main {
    static int N,M;
    static Robot robot;
    static int [][] map;
    static boolean [][] cleaned;
    static int answer = 1;
    static int [] dx = {-1,0,1,0};
    static int [] dy = {0,1,0,-1};
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

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

        st = new StringTokenizer(br.readLine());
        robot = new Robot(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),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());
            }
        }

        run();
        System.out.println(answer);
    }
    static void run()
    {
        Queue<Robot> q = new LinkedList<>();
        q.add(robot);
        cleaned[robot.x][robot.y] = true;
        while(!q.isEmpty())
        {
            Robot cur = q.poll();
            int next_dir = find(cur);
            int mx = cur.x;
            int my = cur.y;
            if(next_dir < 0) // 청소할 곳이 없을경우 후진 , 방향은 유지
            {
                next_dir = (cur.dir + 2) % 4;
                mx += dx[next_dir];
                my += dy[next_dir];
                if(!isValid(mx,my) || map[mx][my] > 0) break; // 종료조건
                q.add(new Robot(mx,my,cur.dir));
            }
            else
            {
                mx += dx[next_dir];
                my += dy[next_dir];

                cleaned[mx][my] = true;
                answer++;
                q.add(new Robot(mx,my,next_dir));
            }
        }
    }
    static int find(Robot cur)
    {
        int next_dir = cur.dir;
        for(int i = 0; i < 4; ++i)
        {
            next_dir = (next_dir + 3) % 4;
            int mx = cur.x + dx[next_dir];
            int my = cur.y + dy[next_dir];

            if(!isValid(mx,my) || cleaned[mx][my] || map[mx][my] > 0) continue;
            return next_dir;
        }
        return -1;
    }
    static boolean isValid(int x, int y)
    {
        return x >= 0 && y >= 0 && x < N && y < M;
    }
}

📝 풀이

문제에서 주어진 조건으로 로봇이 이동할 때, 종료 후 청소된 칸의 갯수를 구하는 문제입니다.
먼저 입력받은 로봇의 초기 위치를 큐에 담아주고, 주어진 1번 조건부터 시작합니다. find함수에서는 로봇이 다음 이동할 위치를 탐색합니다. 왼쪽 방향으로 계속 회전하면서, 청소가 안된 칸이 존재한다면 해당 방향을 리턴해줍니다. 이동할 곳을 찾지 못했다면 -1을 리턴합니다.
find함수의 결과를 담은 next_dir 변수를 통해, 다음 행동을 결정합니다.
0보다 작은 값, 즉 다음 청소할 곳이 존재하지 않는다면, 현재 방향을 유지한 상태로 후진을 해주고, 다시 앞선 조건으로 돌아갑니다. 이동할 곳이 있다면 해당 위치로 이동시켜주면 됩니다.

📜 후기

주어진 그대로 구현하는 문제라서, 문제의 조건을 꼼꼼하게 읽는것이 가장 중요했던 것 같습니다!

profile
머무르지 않기!

0개의 댓글