백준_14503_로봇청소기

덤벨로퍼·2024년 8월 8일
0

코테

목록 보기
36/37

로봇 청소기

첫 풀이

import java.io.*;
import java.util.*;

public class Main {
	static int N, M, r, c, d, ans;
	static int[] dx = {-1, 0, 1, 0};
	static int[] dy = {0, 1, 0, -1};
	static int[][] map;
	static boolean[][] cleaned;
	static class Robot{
		int x,y,d;
		public Robot(int x, int y, int d){
			this.x = x;
			this.y = y;
			this.d = d;
		}
		private void turnLeft(){
			this.d = (this.d + 3) % 4;
		}
	}

	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());
		r = Integer.parseInt(st.nextToken());
		c = Integer.parseInt(st.nextToken());
		d = Integer.parseInt(st.nextToken());
		Robot robot = new Robot(r, c, d);

		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());
			}
		}

		while(true){
			if(!cleaned[robot.x][robot.y]){
				cleaned[robot.x][robot.y] = true;
				ans++;
			}

			boolean moved = false;

			for(int i = 0; i < 4; i++){
				int nx = robot.x + dx[i];
				int ny = robot.y + dy[i];

				if(nx >= 0 && nx < N && ny >= 0 && ny < M && !cleaned[nx][ny] && map[nx][ny] == 0){
					moved = true;
					break;
				}
			}

			if(moved){
				robot.turnLeft();
				int nx = robot.x + dx[robot.d];
				int ny = robot.y + dy[robot.d];
				if(nx >= 0 && nx < N && ny >= 0 && ny < M && !cleaned[nx][ny] && map[nx][ny] == 0){
					robot.x = nx;
					robot.y = ny;
				}
			}
			if(!moved){
				int backDir = (robot.d + 2) % 4;
				int nx = robot.x + dx[backDir];
				int ny = robot.y + dy[backDir];

				if(nx >= 0 && nx < N && ny >= 0 && ny < M && map[nx][ny] != 1){
					robot.x = nx;
					robot.y = ny;
					continue;
				}
				break;
			}

		}

		System.out.print(ans);
	}
}

움직임 가능 처리 조건문의 오류

if(nx >= 0 && nx < N && ny >= 0 && ny < M && !cleaned[nx][ny])

벽도 청소가 안된 상태라는 것을 깜빡하고 조건문을 잘못 짰었다!
청소가 되었어도 후진은 할수 있다!

개선

어짜피 주변의 청소되지 않은 칸으로 이동하는 것이라면 🤔
for(int i = 0; i < 4; i++)으로 순으로 dx[i], dy[i] 를 찾는 것이 아닌 turnLeft 메소드로 이동가능 여부를 판단해도 될듯하다!
👉 어짜피 움직일 칸이 없으면 다시 원래 방향을 바라보게 된다! (4번 반시계 회전)

import java.io.*;
import java.util.*;

public class Main {
	static int N, M, r, c, d, ans;
	static int[] dx = {-1, 0, 1, 0};
	static int[] dy = {0, 1, 0, -1};
	static int[][] map;
	static boolean[][] cleaned;
	static class Robot{
		int x,y,d;
		public Robot(int x, int y, int d){
			this.x = x;
			this.y = y;
			this.d = d;
		}
		private void turnLeft(){
			this.d = (this.d + 3) % 4;
		}
	}

	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());
		r = Integer.parseInt(st.nextToken());
		c = Integer.parseInt(st.nextToken());
		d = Integer.parseInt(st.nextToken());
		Robot robot = new Robot(r, c, d);

		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());
			}
		}

		while(true){
			if(!cleaned[robot.x][robot.y]){
				cleaned[robot.x][robot.y] = true;
				ans++;
			}

			boolean moved = false;

			for(int i = 0; i < 4; i++){
				robot.turnLeft();
				int nx = robot.x + dx[robot.d];
				int ny = robot.y + dy[robot.d];

				if(nx >= 0 && nx < N && ny >= 0 && ny < M && !cleaned[nx][ny] && map[nx][ny] == 0){
					moved = true;
					robot.x = nx;
					robot.y = ny;
					break;
				}
			}

			if(!moved){
				int backDir = (robot.d + 2) % 4;
				int nx = robot.x + dx[backDir];
				int ny = robot.y + dy[backDir];

				if (nx < 0 || ny < 0 || nx >= N || ny >= M || map[nx][ny] == 1) {
					break;
				}
				robot.x = nx;
				robot.y = ny;
			}
		}
		System.out.print(ans);
	}
}
profile
💪 점진적 과부하로 성장하는 개발자

0개의 댓글