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 | 31 |
Tags
- DBASE&
- 이중우선순위큐 java
- 면접 필수 질문
- oracle ansi sql
- oracle ansi
- Java
- 위상정렬
- 프로그래머스 이중우선순위큐 java
- IT 면접 준비
- CJ DBASE&
- ansi sql 장점
- 그리디
- ansi sql 단점
- 프로그래머스 이중우선순위큐
- Spring Boot
- SQL
- 디베이스앤
- 백트래킹
- 디베이스앤 인턴 후기
- 이분탐색
- 개발자 면접 준비
- 프로그래머스
- BFS
- DP
- DFS
- 이중우선순위큐 자바
- 프로그래머스 이중우선순위큐 자바
- 백준
- JPA
- Gradle
Archives
- Today
- Total
쉬운 프로그래밍
[알고리즘] 백준 7576 토마토 본문
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BOJ_7576 {
static int m;
static int n;
static int[][] map;
static boolean[][] visited;
static int count = 0;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {1, -1, 0, 0};
public static void main(String[] args) throws IOException {
//input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
StringTokenizer st = new StringTokenizer(s, " ");
m = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
map = new int[n + 1][m + 1];
visited = new boolean[n + 1][m + 1];
// init
for (int i = 0; i < n + 1; i++) {
Arrays.fill(map[i], 0);
}
for (int i = 1; i < n + 1; i++) {
s = br.readLine();
st = new StringTokenizer(s, " ");
for (int j = 1; j < m + 1; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
List<Location> list = new ArrayList<Location>();
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < m + 1; j++) {
if (map[i][j] == 1)
list.add(new Location(i, j, 0));
}
}
bfs(list);
}
public static void bfs(List<Location> loc) {
Queue<Location> qu = new LinkedList<Location>();
int day = 0;
for (int i = 0; i < loc.size(); i++) {
Location currentLoc = loc.get(i);
qu.add(currentLoc);
}
while (!qu.isEmpty()) {
Location temp = qu.poll();
day = temp.value;
for (int i = 0; i < 4; i++) {
int nx = temp.x + dx[i];
int ny = temp.y + dy[i];
if (nx > 0 && nx < n + 1 && ny > 0 && ny < m + 1) {
if (map[nx][ny] == 0) {
qu.add(new Location(nx, ny, temp.value + 1));
map[nx][ny] = map[temp.x][temp.y] + 1;
}
}
}
}
if (checkTomato())
System.out.println(day);
else
System.out.println(-1);
}
public static boolean checkTomato() {
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < m + 1; j++) {
if (map[i][j] == 0)
return false;
}
}
return true;
}
static class Location {
int x;
int y;
int value;
public Location(int x, int y, int value) {
this.x = x;
this.y = y;
this.value = value;
}
}
}
익은 토마토의 상,하,좌,우 인덱스값이 하루마다 익어간다.
nx, ny(상하좌우 인덱스)값을 검증하면서 bfs를 돌리면 된다.
map[nx][ny]의 값과 Location.value의 값은 map[temp.x][temp.y] + 1로 만들어 준다.
이를 통해 토마토가 익었을 당시의 날짜를 찾을 수 있다.
또한 마지막으로 익은 토마토 Location의 value값 == map[][]의 최대값 == 토마토가 모두 익은 날짜이다.
또한 신경써줘야 할 것이 있는데, 토마토가 모두 익지 못하게 되는 경우에는 -1을 출력해야한다.
BFS가 완료된 후 map[][]에 0이 한개라도 있으면 토마토가 모두 익지 않은 것이기 때문에 이러한 경우에 -1을 출력하도록 하여 문제를 해결하였다.
'알고리즘 > DFS, BFS' 카테고리의 다른 글
[알고리즘] 백준 2583 영역 구하기 (0) | 2021.01.23 |
---|---|
[알고리즘] 백준 7569 토마토 (3) | 2021.01.05 |
[알고리즘] 백준 2178 미로탐색 (0) | 2021.01.05 |
[알고리즘] 백준 1012 유기농 배추 (0) | 2021.01.05 |
[알고리즘] 백준 2667 단지번호붙이기 (0) | 2021.01.05 |
Comments