반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- 트리의 지름 자바
- 코드업 1020 자바
- codeup 1020 java
- 빅데이터분석기사
- 프로그래머스 가운데 글자 가져오기 자바
- docker 삭제
- 빅분기실기
- 최소 스패닝 트리 자바
- 청년 AI Big Data 아카데미 13기
- 가운데 글자 가져오기 python
- codeup 1020 자바
- 프로그래머스 가운데 글자 가져오기 파이썬
- 프로그래머스 가운데 글자 가져오기 python
- 청년 Ai Big Data 아카데미
- 최소 스패닝 트리
- 나누어 떨어지는 숫자 배열 python
- 최단 경로 알고리즘
- 나누어 떨어지는 숫자 배열 java
- 트리의 지름 java
- 프로그래머스 나누어 떨어지는 숫자 배열 자바
- docker remove
- m1 docker
- 핸즈온 머신러닝
- 코드업 1020 java
- 가운데 글자 가져오기 java
- 가운데 글자 가져오기 자바
- 가운데 글자 가져오기 파이썬
- m1 docker install
- 프로그래머스 나누어 떨어지는 숫자 배열 파이썬
- docker 완전 삭제
Archives
- Today
- Total
NineTwo meet you
[백준/자바] 17086 아기 상어 2 본문
반응형

문제
N×M 크기의 공간에 아기 상어 여러 마리가 있다.
공간은 1×1 크기의 정사각형 칸으로 나누어져 있다. 한 칸에는 아기 상어가 최대 1마리 존재한다.
어떤 칸의 안전 거리는 그 칸과 가장 거리가 가까운 아기 상어와의 거리이다.
두 칸의 거리는 하나의 칸에서 다른 칸으로 가기 위해서 지나야 하는 칸의 수이고, 이동은 인접한 8방향(대각선 포함)이 가능하다.
안전 거리가 가장 큰 칸을 구해보자.
입력
첫째 줄에 공간의 크기 N과 M(2 ≤ N, M ≤ 50)이 주어진다.
둘째 줄부터 N개의 줄에 공간의 상태가 주어지며, 0은 빈 칸, 1은 아기 상어가 있는 칸이다.
빈 칸의 개수가 한 개 이상인 입력만 주어진다.
출력
첫째 줄에 안전 거리의 최댓값을 출력한다.
예제 입력 1
5 4
0 0 1 0
0 0 0 0
1 0 0 0
0 0 0 0
0 0 0 1
예제 출력 1
2
예제 입력 2
7 4
0 0 0 1
0 1 0 0
0 0 0 0
0 0 0 1
0 0 0 0
0 1 0 0
0 0 0 1
예제 출력 2
2
설명
bfs문제다.
상어 위치에서 8방향으로 돌리고 거리를 측정하면 된다.
다 측정을 마치고 거리가 가장 최대가 되는 값을 찾으면 된다.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
import java.util.StringTokenizer; | |
public class BOJ17086 { | |
static int safeZone[][]; | |
static int n; | |
static int m; | |
static ArrayList<String> shark = new ArrayList<>(); | |
static int dxy[][] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,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()); | |
safeZone = new int[n][m]; | |
int result = 0; | |
for(int i = 0; i < n; i++) { | |
st = new StringTokenizer(br.readLine()); | |
for(int j = 0; j < m; j++) { | |
if(Integer.parseInt(st.nextToken()) == 1) { | |
shark.add(i+" "+j); | |
} | |
} | |
} | |
for(int i = 0; i < n; i++) { | |
for(int j = 0; j < m; j++) { | |
safeZone[i][j] = Integer.MAX_VALUE; | |
} | |
} | |
for(String s:shark) { | |
move(s); | |
} | |
for(int i = 0; i < n; i++) { | |
for(int j = 0; j < m; j++) { | |
result = Math.max(result, safeZone[i][j]); | |
} | |
} | |
System.out.println(result); | |
} | |
static void move(String baby) { | |
String str[] = baby.split(" "); | |
int x = Integer.parseInt(str[0]); | |
int y = Integer.parseInt(str[1]); | |
safeZone[x][y] = 0; | |
Queue<String> q = new LinkedList<>(); | |
q.add(baby); | |
boolean visited[][] = new boolean[n][m]; | |
while(!q.isEmpty()) { | |
String cur[] = q.poll().split(" "); | |
int cx = Integer.parseInt(cur[0]); | |
int cy = Integer.parseInt(cur[1]); | |
visited[cx][cy] = true; | |
for(int i = 0; i < 8; i++) { | |
int tx = cx + dxy[i][0]; | |
int ty = cy + dxy[i][1]; | |
if(!isRange(tx, ty)) continue; | |
if(visited[tx][ty]) continue; | |
if(safeZone[cx][cy] + 1 <= safeZone[tx][ty]) { | |
safeZone[tx][ty] = safeZone[cx][cy] + 1; | |
q.add(tx+" "+ty); | |
} | |
} | |
} | |
} | |
static boolean isRange(int x, int y) { | |
return -1 < x && x < n && -1 < y && y < m; | |
} | |
} |
반응형
'프로그래밍 문제 > 백준' 카테고리의 다른 글
[백준/자바] 2138 전구와 스위치 (0) | 2021.08.10 |
---|---|
[백준/자바] 9465 스티커 (0) | 2021.08.10 |
[백준/자바] 15591 MooTube (Silver) (2) | 2021.08.03 |
[백준/자바] 20057 마법사 상어와 토네이도 (2) | 2021.07.30 |
[백준/자바] 17142 연구소 3 (0) | 2021.07.23 |