Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 14503. 로봇 청소기 본문

acmicpc

[BAEKJOON] 14503. 로봇 청소기

코드와이 2021. 5. 9. 23:54

 

문제링크

www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

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

www.acmicpc.net

 

package acmicpc.Gold5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class 로봇_청소기 {

	static int n, m, arr[][], ans;
	static boolean visited[][];
	// 북 서 남 동
	static int[] dr = {-1, 0, 1, 0};
	static int[] dc = {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());
		
		st = new StringTokenizer(br.readLine());
		int r = Integer.parseInt(st.nextToken());
		int c = Integer.parseInt(st.nextToken());
		int d = Integer.parseInt(st.nextToken());
		
		arr = new int[n][m];
		for(int i = 0 ; i < n ; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j = 0 ; j < m ; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		visited = new boolean[n][m];
		visited[r][c] = true;
		arr[r][c] = 2;
		ans = 1;
		if(d == 1) d = 3;
		else if(d == 3) d = 1;
  		go(r,c,d);
		
		System.out.println(ans);
	}
	private static void go(int r, int c, int d) {
		
		for(int i = 0 ; i < 4 ; i++) {
			int nd = (d + 1 + i) % 4;
			int nr = r + dr[nd];
			int nc = c + dc[nd];
			
			if(nr < 0 || nc < 0 || nr >= n || nc >= m) continue;
			if(arr[nr][nc] == 1 || visited[nr][nc]) continue;
			arr[nr][nc] = 2;
			visited[nr][nc] = true;
			ans++;
			go(nr, nc, nd);
			
			return;
		}
		
		int nr = r + dr[(d+2) % 4];
		int nc = c + dc[(d+2) % 4];
		if(nr < 0 || nc < 0 || nr >= n || nc >= m || arr[nr][nc] == 1) return;
		go(nr, nc, d);
	}
	
	
}