ALGORITHM/BOJ

[백준] 14503 로봇 청소기

0298 2020. 11. 3. 23:25

www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

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

www.acmicpc.net

2020-11-03


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

public class Main14503 {
	public static int N, M, count, arr[][];
	public static Queue<int[]> q;
	public static int dx[] = {-1, 0, 1, 0};
	public static int dy[] = {0, 1, 0, -1}; // 북 동 남 서
	
	public static void solve() {
		loop:while(!q.isEmpty()) {
			int tmp[] = q.poll();
			int sx = tmp[0];
			int sy = tmp[1];
			int sd = tmp[2];
			int nd = sd;
			int tmpCnt = 0;
			
			for(int i = 0; i < 4; i++) {
				nd = ((nd-1) == -1) ? 3 : (nd-1);
				int nx = sx + dx[nd];
				int ny = sy + dy[nd];
				
				if (nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 2 || arr[nx][ny] == 1) {
					tmpCnt++;
					continue;
				}
				if (arr[nx][ny] == 0) {
					q.add(new int[] {nx, ny, nd});
					arr[nx][ny] = 2;
					count++;
					break;
				} 
			}
			if (tmpCnt == 4) {
				int back = (sd < 2) ? (sd + 2) : (sd - 2);
				int nx = sx + dx[back];
				int ny = sy + dy[back];
				if(nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 1) break loop;
				else {
					q.add(new int[] {nx, ny, sd});
				}
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());

		count = 0;
		arr = new int[N][M];
		q = new LinkedList<int[]>();
		
		st = new StringTokenizer(bf.readLine());
		int x = Integer.parseInt(st.nextToken()); // 시작 위치
		int y = Integer.parseInt(st.nextToken());
		int d = Integer.parseInt(st.nextToken());

		for(int i = 0; i < N; i++) {
			st = new StringTokenizer(bf.readLine());
			for(int j = 0; j < M; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		q.add(new int[] {x, y, d});
		arr[x][y] = 2;
		count++;

		solve();
		System.out.println(count);
	}
}

 

입력 조건 중에 지도의 첫 행, 마지막 행, 첫 열, 마지막 열에 있는 모든 칸은 이다. 벽이라는 조건을 제대로 확인을 안 해서 예제 2번하고 답이 안 맞았었다.

if (nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 2 || arr[nx][ny] == 1) {
		tmpCnt++;
		continue;
}

 

 

 

 

 

2019-10-14


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

public class Main {
	public static int N, M, arr[][], clean;
	public static Queue<int[]> q;
	public static int dx[] = {-1, 0, 1, 0};
	public static int dy[] = {0, 1, 0, -1};
	
	public static void solve() {
		loop:while(!q.isEmpty()) {
			int size = q.size();
			while(size > 0) {
				int tmp[] = q.poll();
				int x = tmp[0];
				int y = tmp[1];
				int dir = tmp[2];
				int d = dir;
				boolean check = false;
				for(int i = 0; i < 4; i++) {
					if(d == 0) d = 3;
					else d-=1;
					int nx = x + dx[d];
					int ny = y + dy[d];
					
					if(nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 1 || arr[nx][ny] == 2) continue;
					else if(arr[nx][ny] == 0 /*&& !vtd[nx][ny]*/) {
						arr[nx][ny] = 2;
						q.add(new int[] {nx, ny, d});
						clean++;
						check = true;
						break;
					}
				}
				if(!check) {
					int change = 0;
					if(dir == 0) change = 2;
					else if(dir == 1) change = 3;
					else if(dir == 2) change = 0;
					else if(dir == 3) change = 1;
					
					int nx = x + dx[change];
					int ny = y + dy[change];
					
					if(nx <= 0 || ny <= 0 || nx >= N-1 || ny >= M-1 || arr[nx][ny] == 1) break loop;
					else {
						q.add(new int[] {nx, ny, dir});
					}
				}
				size--;
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		arr = new int[N][M];
		q = new LinkedList<int[]>();
		st = new StringTokenizer(bf.readLine());
		int r = Integer.parseInt(st.nextToken());
		int c = Integer.parseInt(st.nextToken());
		int d = Integer.parseInt(st.nextToken());
		q.add(new int[] {r, c, d});
		for(int i = 0; i < N; i++) {
			st = new StringTokenizer(bf.readLine());
			for(int j = 0; j < M; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		clean = 0;
		if(arr[r][c] == 0) {
			arr[r][c] = 2;
			clean++;
		}
		solve();
		System.out.println(clean);
	}
}