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

설명
바이러스가 퍼질때 인덱스에 따라 우선적으로 퍼지는 바이러스가 정해저 있기 때문에 우선순위큐를 사용했다.
또 고려해야 할 사항은 S가 0초일때까지 고려해야 한다는 점이다.
코드
This file contains hidden or 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.PriorityQueue; | |
import java.util.StringTokenizer; | |
public class BOJ18405 { | |
static int map[][]; | |
static int dxy[][] = {{-1,0},{1,0},{0,-1},{0,1}}; | |
static int x, y, s, n; | |
static boolean visited[][]; | |
static PriorityQueue<coordinate> q = new PriorityQueue<>(); | |
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()); | |
int k = Integer.parseInt(st.nextToken()); | |
map = new int[n][n]; | |
visited = new boolean[n][n]; | |
for(int i = 0; i < n; i++) { | |
st = new StringTokenizer(br.readLine()); | |
for(int j = 0; j < n; j++) { | |
map[i][j] = Integer.parseInt(st.nextToken()); | |
if(map[i][j] != 0) q.add(new coordinate(map[i][j], i, j)); | |
} | |
} | |
st = new StringTokenizer(br.readLine()); | |
s = Integer.parseInt(st.nextToken()); | |
x = Integer.parseInt(st.nextToken())-1; | |
y = Integer.parseInt(st.nextToken())-1; | |
while(s-- >= 0) { | |
bfs(); | |
if(map[x][y] != 0) break; | |
} | |
System.out.println(map[x][y]); | |
} | |
static class coordinate implements Comparable<coordinate>{ | |
int idx; | |
int x; | |
int y; | |
coordinate(int idx, int x, int y){ | |
this.idx = idx; | |
this.x = x; | |
this.y = y; | |
} | |
@Override | |
public int compareTo(coordinate o) { | |
return this.idx - o.idx; | |
} | |
} | |
static void bfs() { | |
PriorityQueue<coordinate> tq = new PriorityQueue<>(); | |
while(!q.isEmpty()) { | |
coordinate cur = q.poll(); | |
if(visited[cur.x][cur.y]) continue; | |
visited[cur.x][cur.y] = true; | |
map[cur.x][cur.y] = cur.idx; | |
for(int i = 0; i < 4; i++) { | |
int tx = cur.x + dxy[i][0]; | |
int ty = cur.y + dxy[i][1]; | |
if(!isRange(tx, ty)) continue; | |
if(visited[tx][ty]) continue; | |
tq.add(new coordinate(cur.idx, tx, ty)); | |
} | |
} | |
q = tq; | |
} | |
static boolean isRange(int x, int y) { | |
return 0 <= x && x < n && 0 <= y && y < n; | |
} | |
} |
반응형
'프로그래밍 문제 > 백준' 카테고리의 다른 글
[백준/자바] 16920 확장 게임 (0) | 2021.10.10 |
---|---|
[백준/자바] 17609 회문 (0) | 2021.10.10 |
[백준/자바] 20061 모노미노도미노2 (0) | 2021.10.09 |
[백준/자바] 2096 내려가기 (0) | 2021.10.05 |
[백준/자바] 21611 마법사 상어와 블리자드 (0) | 2021.10.04 |
Comments