SW Expert Academy
SW ํ๋ก๊ทธ๋๋ฐ ์ญ๋ ๊ฐํ์ ๋์์ด ๋๋ ๋ค์ํ ํ์ต ์ปจํ ์ธ ๋ฅผ ํ์ธํ์ธ์!
swexpertacademy.com
1. ๋ฌธ์ ์์ฝ
N*Nํฌ๊ธฐ ์ง๋์์ ๊ฒฌ์ฐ(0,0)๊ฐ ์ง๋ (N-1,N-1)์๊ฒ ๊ฐ ์ ์๋ ์ต์์๊ฐ์ ๊ตฌํ๋ผ.
์ง๋์ ๊ฐ์ด
0์ด๋ฉด ์ ๋ฒฝ, 1์ด๋ฉด ๊ธธ, 1๋ณด๋ค ํฌ๋ฉด ๋ค๋ฆฌ์ ์ฌ๋์๊ฐ ์ฃผ๊ธฐ์ด๋ค. ๋ค๋ฆฌ๋ ์ฌ๋์๊ฐ์ด ๋๋๋ฉด ๊ฑด๋ ์ ์๋ค.
๋ฑ ํ ๋ฒ๋ง ๋ค๋ฆฌ ํ๋๋ฅผ ์ถ๊ฐ๋ก ๋ ์์ฑํ ์ ์๋ค. ์ด ๋ค๋ฆฌ์ ์ฌ๋์๊ฐ ์ฃผ๊ธฐ๋ M์ด๋ค.
2. ํ์ด
์ฒ์์ DFS๋ก ์ ๊ทผํ์๋ค. ์๋ก์ด ๋ค๋ฆฌ๋ฅผ ๋์๋ณด๊ณ ์ง๋ ๋ฅผ ๋ง๋๋ฌ ๊ฐ๋๋ฐ ์๋๋ฉด ๋ค์ ๋์์์ ๋ค๋ฅธ ์๋ฆฌ์ ๋ค๋ฆฌ๋ฅผ ๋์๋ณด๊ณ ํ ์๊ฐ์ผ๋ก ๋ฐฑํธ๋ํน์ด ์ ์ ํ๋ค๊ณ ์๊ฐํ๋ค.
๊ทผ๋ฐ ์ด๋ ๊ฒ ํ๋๊น ์๊ฐ์ด๊ณผ๊ฐ ๋จ๋๋ผ. 20๊ฐ ์ค์ 12๊ฐ ๋ง์๋ค.
์ต๋จ ์๊ฐ์ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ฏ๋ก DFS๋ณด๋ค๋ BFS๊ฐ ์ ์ ํ๋ค.
ํ์ง๋ง ๋ค๋ฆฌ๋ฅผ ๋์๋ณด๋ ์์ ์ ๋ฐฑํธ๋ํน์ด ์๋๊ฐ? -> ์ฒ์์ ์ ๋ ฅ๋ฐ์ ๋ 0์ธ ์ขํ๋ฅผ ์ ์ฅํด๋๋ค๊ฐ
for๋ฌธ์ ๋๋ ค์ ์ขํ๋ง๋ค ๋ค๋ฆฌ๋ฅผ ๋์๋ค๊ณ ์น๊ณ bfs๋ฅผ ๋๋ฆฌ๋ ๊ฒ์ด๋ค.
์ด๋ ๊ฒ ํ๋ ํต๊ณผํ๋ค.
๋ค๋ฆฌ๋ฅผ ๋์๋ค๊ฐ ์น์ ๋ค๊ฐ๋ฉด ๋ฐฑํธ๋ํน์ธ๋ฐ ์ต๋จ๊ฑฐ๋ฆฌ๋ฉด BFS?? ์ด๋ฐ ํ์ ๊ฐํ ์๊ฐ ๋๋ฌธ์ ๊ฝค๋ ํค๋งธ๋ค.
๊ทธ๋ฆฌ๊ณ ์๋ก์ด ๋ค๋ฆฌ๋ ๊ฒน์น๋ ๋ถ๋ถ์ ๋ ์ ์๋ค๋ ์กฐ๊ฑด์ด ์๋๋ฐ,
์ด ์กฐ๊ฑด ๋๋ฌธ์ 4๋ฐฉํ์์ ํ๋ฒ ๋ ํด์ฃผ๋ฉด ๊ทธ๊ฑด ๋์ธ ๊ฒ์ด๋ค.
์ด์ฐจํผ visited true๋ฉด ๋ฐฉ๋ฌธ์ ์ํ ํ ๊ณ , map[ny][nx]==0์ด๋ฉด continueํ ๊ฒ์ด๋๊น ๋ฌด์ํด๋ ๋๋ ์กฐ๊ฑด์ด๋ค.
3. ์ฝ๋
import java.io.*;
import java.util.*;
public class SW_4727_๊ฒฌ์ฐ์์ง๋
{
static int N,M, ans;
static int[][] map;
static int[] dy= {-1,1,0,0};
static int[] dx= {0,0,-1,1};
static List<Pos> blank;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
int T = Integer.parseInt(br.readLine());
for (int tc=1; tc<=T; tc++) {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][N];
blank = new ArrayList<> ();
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) {
blank.add(new Pos(i, j)); //์ ๋ฒฝ
}
}
}//์
๋ ฅ ๋
ans = Integer.MAX_VALUE;
for (Pos node : blank) {
map[node.y][node.x] = M;
int res = bfs();
map[node.y][node.x] = 0;
//System.out.println(node.y+", "+node.x+" res:" + res);
if (res==0) continue;
else ans = Math.min(ans, res);
}
System.out.println("#"+tc+" "+ans);
}
}
static int bfs() {
int res = 0;
Queue<Pos> queue = new ArrayDeque<> ();
boolean[][] visited = new boolean[N][N];
queue.offer(new Pos(0,0,0));
visited[0][0] = true;
while (!queue.isEmpty()) {
Pos cur = queue.poll();
int y=cur.y;
int x=cur.x;
int sec=cur.sec;
if (y==N-1 && x==N-1) {
res = sec;
break;
}
for (int d=0; d<4; d++) {
int ny = y+dy[d];
int nx = x+dx[d];
if (ny<0 || ny>=N || nx<0||nx>=N || visited[ny][nx] == true) continue;
if (map[ny][nx] == 0) continue;
else if (map[ny][nx] == 1) {
queue.offer(new Pos(ny,nx,sec+1));
visited[ny][nx] = true;
}
else if (map[ny][nx] > 1) {
if (map[y][x] == 1) {
if ((sec+1) % map[ny][nx] == 0) {
queue.offer(new Pos(ny,nx,sec+1));
visited[ny][nx] = true;
} else {
queue.offer(new Pos(y,x,sec+1));
}
}
}
}
}
return res;
}
static class Pos {
int y,x,sec;
boolean used;
Pos(int y, int x) {
this.y = y;
this.x = x;
}
Pos(int y, int x, int sec) {
this.y = y;
this.x = x;
this.sec = sec;
}
}
}
4. ์ฌ๋ด
์ํ๊ณ ์๋๊ฑฐ์์ผ๋ฉด ์ข๊ฒ ๋ค ์ธ์ ์ฏค ์ค๋ ฅ์ด ๋๊น
'Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SWEA] 5208.์ ๊ธฐ๋ฒ์ค2 - Java (0) | 2022.11.20 |
---|---|
[SWEA] 7733. ์น์ฆ ๋๋ - Java (0) | 2022.11.20 |
[SWEA] 2819. ๊ฒฉ์ํ์ ์ซ์ ์ด์ด ๋ถ์ด๊ธฐ (1) | 2022.11.20 |
[SWEA] 8382. ๋ฐฉํฅ ์ ํ - Java (0) | 2022.11.14 |
[SWEA] 1824. ํ์ง์ด์ ํ๋ก๊ทธ๋จ ๊ฒ์ฆ - Java (0) | 2022.11.14 |