https://www.acmicpc.net/problem/21922
๋ฌธ์ ์ค๋ช
๋ฏผ์์ ์ฐ๊ตฌ์ค์์ ์์ด์ปจ ๋ฐ๋์ด ์๋๊ฐ๋ ๊ณณ ์ค์ ์์ผ๋ ค๊ณ ํ๋ค.
๋ค์ํ ๋ฌผ๊ฑด๋ค์ด ๋ฐ๋์ ๋ฐฉํฅ์ผ๋ก ๋ฐ๊พผ๋ค.
์ด 4๊ฐ์ง์ ๋ฌผ๊ฑด
์์ด์ปจ์ด ์์นํ ์๋ฆฌ์ ๋ฌผ๊ฑด์ด ์์นํ ์๋ฆฌ๋ฅผ ์ ์ธํ ๋๋จธ์ง ์๋ฆฌ ์ค์์ ๋ฏผ์์ด๊ฐ ์์ ์ ์๋ ์๋ฆฌ์ ๊ฐ์๋ฅผ ๊ตฌํ์!
๋ฌธ์ ํ์ด
boolean isWindy[x][y][d]
๐ (x,y)์นธ์ d๋ฐฉํฅ์ผ๋ก ๋ฐ๋์ด ๋ถ๋์ง ์ฌ๋ถ์ ๋ํ ๋ฐฐ์ด
๊ฐ์ ์นธ์ด๋ผ๋ ๋ฐ๋์ ๋ฐฉํฅ์ ๋ฐ๋ผ ๋ค์์ด ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ 3์ค ๋ฐฐ์ด!!!!! ์ฌ์ฉ!!!!!
์์ด์ปจ ์๋ฆฌ๋ถํฐ ํ์ push
โ
๋ค ๋ฐฉํฅ ๋ชจ๋ ๋ฃ์ด์ฃผ๊ธฐ q.add(new int[]{x,y,d});
BFS()
๋๊ฐ์ด ์งํํ์ง๋ง, ์ฅ์ ๋ฌผ์ ์ข ๋ฅ์ ๋ฐ๋ผ ๋ฐ๋์ ๋ฐฉํฅ๋ง ๋ฐ๊ฟ์ addํด์ฃผ๋ฉด ๋จโผ๏ธ
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_BOJ_21922_ํ๋ถ_์ฐ๊ตฌ์_๋ฏผ์ {
static int N, M;
static int[][] lab;
static boolean[][][] isWindy;
static Queue<int[]> queue = new LinkedList<>();
static int[] dx = {-1, 0, 0, 1}; // ์, ์ผ, ์ค, ์๋
static int[] dy = {0, -1, 1, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
N = Integer.parseInt(stringTokenizer.nextToken());
M = Integer.parseInt(stringTokenizer.nextToken());
lab = new int[N][M];
isWindy = new boolean[N][M][4];
for (int i = 0; i < N; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
lab[i][j] = Integer.parseInt(stringTokenizer.nextToken());
if (lab[i][j] == 9) {
for (int d = 0; d < 4; d++) {
queue.add(new int[]{i, j, d});
isWindy[i][j][d] = true;
}
}
}
}
BFS();
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int d = 0; d < 4; d++) {
if(isWindy[i][j][d]){
ans += 1;
break;
}
}
}
}
System.out.println(ans);
}
private static void BFS() {
while (!queue.isEmpty()) {
int[] now = queue.poll();
int dir = now[2];
int nx = now[0] + dx[dir];
int ny = now[1] + dy[dir];
if(nx < 0 || ny < 0 || N <= nx || M <= ny) continue;
if(isWindy[nx][ny][dir]) continue;
isWindy[nx][ny][dir] = true;
switch (lab[nx][ny]) {
case 1:
if(dir == 1 || dir == 2) continue;
break;
case 2:
if(dir == 0 || dir == 3) continue;
break;
case 3:
if(dir == 0) dir = 2;
else if(dir == 1) dir = 3;
else if(dir == 2) dir = 0;
else dir = 1;
break;
case 4:
if(dir == 0) dir = 1;
else if(dir == 1) dir = 0;
else if(dir == 2) dir = 3;
else dir = 2;
break;
}
queue.add(new int[]{nx, ny, dir});
}
}
}
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ๋ฐฑ์ค / BOJ 22861 ] ํด๋ ์ ๋ฆฌ - large ( JAVA / ์๋ฐ ) (0) | 2022.02.22 |
---|---|
[ BOJ / ๋ฐฑ์ค 17837 ] ์๋ก์ด ๊ฒ์ 2 (0) | 2022.02.22 |
[ BOJ / ๋ฐฑ์ค 20165 ] ์ธ๋ด์ ๋๋ฏธ๋ ธ ์ฅ์ธ ํธ์ ( ์๋ฐ / JAVA ) (0) | 2022.02.21 |
[ BOJ / ๋ฐฑ์ค 5212 ] ์ง๊ตฌ ์จ๋ํ ( ์๋ฐ / JAVA ) (0) | 2022.02.15 |
[ ๋ฐฑ์ค / BOJ 20436 ] ZOAC 3 ( JAVA / ์๋ฐ ) (0) | 2022.02.15 |