티스토리 뷰

import java.util.*;
import java.io.*;
import java.awt.Point;
public class Main {
static int n = 0;
static int m = 0;
static int h = 0;
static int w = 0;
static int[][] board = new int[1001][1001] ;
static int[][] visit = new int[1001][1001] ;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {1, -1, 0, 0};
static int startx=0;
static int starty=0;
static int lastx=0;
static int lasty=0;
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());
for (int i = 1; i <= n; i++) {
st = new StringTokenizer(bf.readLine()," ");
for (int j = 1; j <= m; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(bf.readLine()," ");
h = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
startx = Integer.parseInt(st.nextToken());
starty = Integer.parseInt(st.nextToken());
lastx = Integer.parseInt(st.nextToken());
lasty = Integer.parseInt(st.nextToken());
if (startx == lastx && starty == lasty)
System.out.println(0);
else {
System.out.println(bfs());
}
bf.close();
}
static int bfs() {
Queue<pair> q = new LinkedList<>();
q.add(new pair(startx,starty));
visit[startx][starty] = 1;
while (!q.isEmpty()) {
pair cur = q.poll();
if(cur.x == lastx && cur.y ==lasty)
return visit[cur.x][cur.y]-1;
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (visit[nx][ny] == 0) {
if (rect(nx, ny))
continue;
visit[nx][ny] = visit[cur.x][cur.y] + 1;
q.add(new pair(nx, ny));
}
}
}
return -1;
}
static boolean rect(int x,int y)
{
int cx = x+h-1;
int cy = y+w-1;
if(x<1||y<1||cx>n||cy>m)
return true;
for(int i=x;i<=cx;i++)
{
for(int j=y;j<=cy;j++)
{
if(board[i][j]==1)
return true;
}
}
return false;
}
}
class pair
{
int x;
int y;
pair(int x, int y)
{
this.x =x;
this.y = y;
}
}
rect 함수에서 탈출 조건을 생각을 잘 해야 한다.
'알고리즘 > 백준' 카테고리의 다른 글
| 백준 2636 (치즈) java (0) | 2022.12.01 |
|---|---|
| 백준 4963 (섬의 개수) - java (1) | 2022.12.01 |
| 백준 7576 (토마토) java (0) | 2022.11.28 |
| 백준 13549 (숨바꼭질3) java (0) | 2022.11.28 |
| 백준 2146 (다리 만들기) java (0) | 2022.11.27 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준 #2636 #치즈
- 백준 #2580 #스도쿠
- 백준 #18405 #경쟁적 전염
- 자바 #JAVA
- 백준 #5721 #사탕 줍기 대회 #java #자바
- 17218
- 올해보다
- 백준 #15686 #치킨 배달
- 17394
- 백준 #25603 #짱해커 이동식 #java #자바
- 백준 #1584 #게임 #java #자바
- 행복합시다.
- 백준 #17940 #주식 #자바 #java
- 백준 #4963 #섬의 개수
- 백준 #13549 #숨바꼭질3
- Java
- 백준 #16973 #직사각형 탈출
- 백준
- 백준 #다리 만들기 #2146
- 백준 #3980 #선발 명단
- 백준 #인구 이동 #16234
- 백준 #14863 #서울에서 경산까지 #java #자바
- 백준 #12014 #주식 #자바 #java
- 백준 #28140 #빨강~ 빨강~ 파랑! 파랑! 달콤한 솜사탕! #java #자바
- 백준 #
- 백준 #1325 #효율적인 해킹
- 백준 #1987 #알파벳 #자바 #java
- 자바
- 백준 #치즈 #2638
- 백준 #1759 #암호 만들기
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함