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] 16973. 직사각형 탈출 본문

acmicpc

[BAEKJOON] 16973. 직사각형 탈출

코드와이 2021. 4. 15. 22:38

 

BFS

문제링크

www.acmicpc.net/problem/16973

 

16973번: 직사각형 탈출

크기가 N×M인 격자판에 크기가 H×W인 직사각형이 놓여 있다. 격자판은 크기가 1×1인 칸으로 나누어져 있다. 격자판의 가장 왼쪽 위 칸은 (1, 1), 가장 오른쪽 아래 칸은 (N, M)이다. 직사각형의 가장

www.acmicpc.net

 

package acmicpc.Gold5;

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

public class 직사각형_탈출 {

	static int n, m, arr[][], h, w, sR, sC, fR, fC, ans;
	static class Point{
		int r;
		int c;
		int cnt;
		public Point(int r, int c, int cnt) {
			this.r = r;
			this.c = c;
			this.cnt = cnt;
		}
	}
	public static void main(String[] args) throws NumberFormatException, 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());
		
		arr = new int[n+1][m+1];
		for(int i = 1 ; i <= n ; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j = 1 ; j <= m ; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
			}
		}

		st = new StringTokenizer(br.readLine());
		h = Integer.parseInt(st.nextToken());
		w = Integer.parseInt(st.nextToken());
		sR = Integer.parseInt(st.nextToken());
		sC = Integer.parseInt(st.nextToken());
		fR = Integer.parseInt(st.nextToken());
		fC = Integer.parseInt(st.nextToken());
		
		ans = -1;

		go();
		System.out.println(ans);
	}
	
	static int[] dr = {-1,1,0,0};
	static int[] dc = {0,0,1,-1};
	private static void go() {
		
		if(sR == fR && sC == fC) {
			ans = 0;
			return;
		}
		
		Queue<Point> queue = new LinkedList<Point>();
		queue.offer(new Point(sR, sC, 0));
		boolean[][] visited = new boolean[n+1][m+1];
		visited[sR][sC] = true;
		
		while(!queue.isEmpty()) {
			
			Point cur = queue.poll();
			
			if(cur.r == fR && cur.c == fC) {
				ans = cur.cnt;
				return;
			}
			
			out :for(int d = 0 ; d < 4 ; d++) {
				int nr = cur.r + dr[d];
				int nc = cur.c + dc[d];
                
				if(nr <= 0 || nr + h - 1 > n || nc <= 0 || nc + w - 1 > m || visited[nr][nc]) continue;
				
				for(int i = nr ; i < nr + h ; i++) {
					if(arr[i][nc] == 1 || arr[i][nc + w - 1] == 1) continue out;
				}
				for(int j = nc ; j < nc + w ; j++) { 
					if(arr[nr][j] == 1 || arr[nr + h - 1][j] == 1) continue out;
				}
				visited[nr][nc] = true;
				queue.offer(new Point(nr, nc, cur.cnt+1));
			}
		}
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 16235. 나무 재테크  (0) 2021.04.16
[BAEKJOON] 7569. 토마토  (0) 2021.04.15
[BAEKJOON] 11052. 카드 구매하기  (0) 2021.04.15
[BAEKJOON] 1194. 달이 차오른다, 가자.  (0) 2021.04.14
[BAEKJOON] 17471. 게리맨더링  (0) 2021.04.13