๐ก Info
๋ด์ฉ
NxN ํฌ๊ธฐ์ ์ํ๊ด์ด ์๋ค. ์ํ๊ด์ 1x1 ํฌ๊ธฐ์ ์นธ์ผ๋ก ๋๋์ด์ง๋ฉฐ, ํน์ ํ ์์น์๋ ๋ฐ์ด๋ฌ์ค๊ฐ ์กด์ฌํ ์ ์๋ค. ๋ชจ๋ ๋ฐ์ด๋ฌ์ค๋ 1๋ฒ๋ถํฐ K๋ฒ๊น์ง์ ๋ฐ์ด๋ฌ์ค ์ข ๋ฅ ์ค ํ๋์ ์ํ๋ค.
์ํ๊ด์ ์กด์ฌํ๋ ๋ชจ๋ ๋ฐ์ด๋ฌ์ค๋ 1์ด๋ง๋ค ์, ํ, ์ข, ์ฐ์ ๋ฐฉํฅ์ผ๋ก ์ฆ์ํด ๋๊ฐ๋ค. ๋จ, ๋งค ์ด๋ง๋ค ๋ฒํธ๊ฐ ๋ฎ์ ์ข ๋ฅ์ ๋ฐ์ด๋ฌ์ค๋ถํฐ ๋จผ์ ์ฆ์ํ๋ค. ๋ํ ์ฆ์ ๊ณผ์ ์์ ํน์ ํ ์นธ์ ์ด๋ฏธ ์ด๋ ํ ๋ฐ์ด๋ฌ์ค๊ฐ ์กด์ฌํ๋ค๋ฉด, ๊ทธ ๊ณณ์๋ ๋ค๋ฅธ ๋ฐ์ด๋ฌ์ค๊ฐ ๋ค์ด๊ฐ ์ ์๋ค.
์ํ๊ด์ ํฌ๊ธฐ์ ๋ฐ์ด๋ฌ์ค์ ์์น ์ ๋ณด๊ฐ ์ฃผ์ด์ก์ ๋, S์ด๊ฐ ์ง๋ ํ์ (X,Y)์ ์กด์ฌํ๋ ๋ฐ์ด๋ฌ์ค์ ์ข ๋ฅ๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ๋ง์ฝ S์ด๊ฐ ์ง๋ ํ์ ํด๋น ์์น์ ๋ฐ์ด๋ฌ์ค๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด, 0์ ์ถ๋ ฅํ๋ค. ์ด ๋ X์ Y๋ ๊ฐ๊ฐ ํ๊ณผ ์ด์ ์์น๋ฅผ ์๋ฏธํ๋ฉฐ, ์ํ๊ด์ ๊ฐ์ฅ ์ผ์ชฝ ์์ ํด๋นํ๋ ๊ณณ์ (1,1)์ ํด๋นํ๋ค.
์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ด 3x3 ํฌ๊ธฐ์ ์ํ๊ด์ด ์๋ค๊ณ ํ์. ์๋ก ๋ค๋ฅธ 1๋ฒ, 2๋ฒ, 3๋ฒ ๋ฐ์ด๋ฌ์ค๊ฐ ๊ฐ๊ฐ (1,1), (1,3), (3,1)์ ์์นํด ์๋ค. ์ด ๋ 2์ด๊ฐ ์ง๋ ๋ค์ (3,2)์ ์กด์ฌํ๋ ๋ฐ์ด๋ฌ์ค์ ์ข
๋ฅ๋ฅผ ๊ณ์ฐํด๋ณด์.
1์ด๊ฐ ์ง๋ ํ์ ์ํ๊ด์ ์ํ๋ ๋ค์๊ณผ ๊ฐ๋ค.
2์ด๊ฐ ์ง๋ ํ์ ์ํ๊ด์ ์ํ๋ ๋ค์๊ณผ ๊ฐ๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก 2์ด๊ฐ ์ง๋ ๋ค์ (3,2)์ ์กด์ฌํ๋ ๋ฐ์ด๋ฌ์ค์ ์ข ๋ฅ๋ 3๋ฒ ๋ฐ์ด๋ฌ์ค๋ค. ๋ฐ๋ผ์ 3์ ์ถ๋ ฅํ๋ฉด ์ ๋ต์ด๋ค.
๐ฅ์ ๋ ฅ ์กฐ๊ฑด
//์1
3 3
1 0 2
0 0 0
3 0 0
2 3 2
//์2
3 3
1 0 2
0 0 0
3 0 0
1 2 2
๐ค์ถ๋ ฅ ์กฐ๊ฑด
//์1
3
//์2
0
์ค์ ํ์ด ์๊ฐ : 25๋ถ
์ ๋ ฅ
๊ณ์ฐ
์ถ๋ ฅ
๋ณ๋ ๋ฉ์๋
BFS
import java.util.*;
public class Main {
static int N;
static int K;
static int[][] arr;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int result = 0;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
arr = new int[N][K];
for(int i=0; i<N; i++){
for(int j=0; j<K; j++){
arr[i][j] = sc.nextInt();
}
}
bfs(0,0);
Arrays.sort(arr);
System.out.println(result);
}
public static int bfs(int x, int y){
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{x,y});
while(!queue.isEmpty()){
int[] current = queue.poll();
x = current[0];
y = current[1];
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= N || ny >= K){
continue;
}
if(arr[nx][ny] == 0){
continue;
}
if(arr[nx][ny] == 0){
arr[nx][ny] = arr[x][y] + 1;
queue.offer(new int[]{nx, ny});
}
}
}
return arr[N-1][K-1];
}
}
...
static class Virus implements Comparable<Virus> {
int type;
int x;
int y;
int time;
public Virus(int type, int x, int y, int time) {
this.type = type;
this.x = x;
this.y = y;
this.time = time;
}
@Override
public int compareTo(Virus o) {
return Integer.compare(this.type, o.type);
}
}
...
List<Virus> list = new ArrayList<>();
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
arr[i][j] = sc.nextInt();
if(arr[i][j] != 0){
list.add(new Virus(arr[i][j], i , j, 0));
}
}
}
Collections.sort(list);
tT = sc.nextInt();
tX = sc.nextInt()-1;
tY = sc.nextInt()-1;
bfs(list, tT, tX, tY);
...
public static void bfs(List<Virus> list, int tT, int tX, int tY) {
Queue<Virus> queue = new LinkedList<>(list);
while(!queue.isEmpty()){
Virus current = queue.poll();
int x = current.x;
int y = current.y;
int time = current.time;
if(time == tT) {
System.out.println(arr[tX][tY]);
return;
}
for(int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= N || ny >= N) {
continue;
}
if(arr[nx][ny] == 0){
arr[nx][ny] = current.type;
queue.offer(new Virus(current.type, nx, ny, time + 1));
}
}
}
System.out.println(arr[tX][tY]);
}
์ค์ ํ์ด ์๊ฐ : 1์๊ฐ 15๋ถ(์ค์ ํ์ด ์๊ฐ ํฌํจ)
์ ๋ ฅ
๋ณ๋ ํด๋์ค
- Virus ํด๋์ค ์ ์ (๋ฐ์ด๋ฌ์ค ์ ๋ณด๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ํด๋์ค)
๊ณ์ฐ
์ถ๋ ฅ
๋ณ๋ ๋ฉ์๋
import java.util.*;
public class Main {
static int N;
static int K;
static int[][] arr;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int result;
static int tT;
static int tX;
static int tY;
static class Virus implements Comparable<Virus> {
int type;
int x;
int y;
int time;
public Virus(int type, int x, int y, int time) {
this.type = type;
this.x = x;
this.y = y;
this.time = time;
}
@Override
public int compareTo(Virus o) {
return Integer.compare(this.type, o.type);
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
arr = new int[N][N];
List<Virus> list = new ArrayList<>();
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
arr[i][j] = sc.nextInt();
if(arr[i][j] != 0){
list.add(new Virus(arr[i][j], i , j, 0));
}
}
}
Collections.sort(list);
tT = sc.nextInt();
tX = sc.nextInt()-1;
tY = sc.nextInt()-1;
bfs(list, tT, tX, tY);
}
public static void bfs(List<Virus> list, int tT, int tX, int tY) {
Queue<Virus> queue = new LinkedList<>(list);
while(!queue.isEmpty()){
Virus current = queue.poll();
int x = current.x;
int y = current.y;
int time = current.time;
if(time == tT) {
System.out.println(arr[tX][tY]);
return;
}
for(int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= N || ny >= N) {
continue;
}
if(arr[nx][ny] == 0){
arr[nx][ny] = current.type;
queue.offer(new Virus(current.type, nx, ny, time + 1));
}
}
}
System.out.println(arr[tX][tY]);
}
}