์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ๋ค์ต์คํธ๋ผ
- ์นด์นด์ค
- Floyd
- ํธ๋ผ์ด
- PriorityQueue
- ์๋ฃ๊ตฌ์กฐ
- ์๋ฎฌ๋ ์ด์
- Dijkstra
- ๋ฐฑํธ๋ํน
- ๋นํธ๋ง์คํน
- Union Find
- ๋ถ๋ถ์งํฉ
- upperbound
- ํ์๊ฐ์
- ๊ทธ๋ฆฌ๋
- dp
- ์ธ๊ทธ๋จผํธํธ๋ฆฌ
- LowerBound
- ์์ด
- BFS
- ์ฌ๊ท
- ์กฐํฉ
- ์ด์งํธ๋ฆฌ
- ํฌํฌ์ธํฐ
- ๊ตฌํ
- dfs
- ํ๋ก์ด๋์์
- ์ฐ์ ์์ํ
- Django
- ์ด๋ถํ์
- Today
- Total
J
[JAVA] BOJ ๋ฐฑ์ค 3190 ๋ฑ ๋ณธ๋ฌธ
[JAVA] BOJ ๋ฐฑ์ค 3190 ๋ฑ
snowball๐ฐ 2023. 7. 7. 10:57๐ ์ด ๋ฌธ์ ์ ์ ์์ ์ ๋ฐ๋์ ์ ํด์ง ์์๋๋ก ์ฝ๋๋ฅผ ์ง์ผ ํ๋ ๊ฒ์ด๋ค.
๋จธ๋ฆฌ์ ๊ผฌ๋ฆฌ๊ฐ ๋์์ ์์ง์์ ๊ฒฐ์ ํ๋ ๊ฒ์ด ์๋๋ผ ๋จธ๋ฆฌ๊ฐ ์์ง์ด๊ณ ! ํ์ ๊ผฌ๋ฆฌ๋ฅผ ๊ทธ๋๋ก ๋์ง ์ด๋ํ ์ง ๊ฒฐ์ ํด์ผ ํ๋ค
์ ์ฒด์ฝ๋
import java.io.*;
import java.util.*;
public class Main {
static class timer{
char dir;
int time;
timer(char dir, int time){
this.dir = dir;
this.time = time;
}
}
static boolean[][] map;
static boolean[][] snake;
static int[] dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bf.readLine());
map = new boolean[N][N];
snake = new boolean[N][N];
int K = Integer.parseInt(bf.readLine());
for(int i=0; i<K; i++){
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
map[x-1][y-1] = true;
}
ArrayDeque<timer> q = new ArrayDeque<>();
int L = Integer.parseInt(bf.readLine());
for(int i=0; i<L; i++){
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
char c = st.nextToken().charAt(0);
q.offer(new timer(c, x));
}
int time = 1;
int hx = 0; int hy = 0;
int tx = 0; int ty = 0;
int d = 0;
ArrayDeque<int[]> road = new ArrayDeque<>();
while(true){
if(!q.isEmpty() && q.peek().time < time){
timer t = q.poll();
if(t.dir == 'L')
d = (d+3) % 4;
else if(t.dir == 'D')
d = (d+1) % 4;
}
hx += dx[d]; hy += dy[d];
if(hx < 0 || hx >= N || hy < 0 || hy >= N) break; // ๋ฒฝ์ ๋ถ๋ชํ๊ฑฐ๋
if(snake[hx][hy]) break; // ์๊ธฐ์์ ์ ๋ชธ๊ณผ ๋ถ๋ชํ
snake[hx][hy] = true;
road.offer(new int[]{hx, hy});
if(map[hx][hy]) map[hx][hy] = false;
else {
snake[tx][ty] = false;
tx = road.peek()[0];
ty = road.poll()[1];
}
// System.out.println(hx+", " +hy)
time++;
}
System.out.println(time);
}
}
ํ ์ค ์ฉ ๋ณด์!
static class timer{
char dir;
int time;
timer(char dir, int time){
this.dir = dir;
this.time = time;
}
}
static boolean[][] map;
static boolean[][] snake;
static int[] dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
timer ํด๋์ค๋ ์ ๋ ฅ๋ฐ์ ์์น ๋ณํ ์ ๋ณด๋ฅผ ๋ฐ๋ ํด๋์ค์ด๋ค.
map์ ์ฌ๊ณผ์ ์ ๋ณด๋ฅผ ํ์ํ๋ค(true์ผ ๋, ํด๋น ์์น์ ์ฌ๊ณผ๊ฐ ์กด์ฌํ๋ค๋ ๋ป)
snake๋ ๋ฑ์ ์์น๋ค์ ์ ์ฅํ๋ค(true์ด๋ฉด ํด๋น ์์น์ ๋ฑ์ด ์กด์ฌํ๋ค๋ ๋ป)
dx, dy๋ก ๋ค์ ์์น๋ฅผ ์ง์ ํ๋ค.
int N = Integer.parseInt(bf.readLine());
map = new boolean[N][N];
snake = new boolean[N][N];
int K = Integer.parseInt(bf.readLine());
for(int i=0; i<K; i++){
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
map[x-1][y-1] = true;
}
ArrayDeque<timer> q = new ArrayDeque<>();
int L = Integer.parseInt(bf.readLine());
for(int i=0; i<L; i++){
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
char c = st.nextToken().charAt(0);
q.offer(new timer(c, x));
}
์ฌ๊ณผ์ ์์น๋ฅผ ๋ฐ์์ map์ ์ฌ๊ณผ ์ ๋ณด๋ฅผ ๊ฐฑ์ ํ๋ค.
์์น ๋ณ๊ฒฝ ์ ๋ณด๋ฅผ ๋ฐ์์ Queue์ ์ ์ฅํด์ค๋ค.
int time = 1;
int hx = 0; int hy = 0;
int tx = 0; int ty = 0;
int d = 0;
ArrayDeque<int[]> road = new ArrayDeque<>();
while(true){
if(!q.isEmpty() && q.peek().time < time){
timer t = q.poll();
if(t.dir == 'L')
d = (d+3) % 4;
else if(t.dir == 'D')
d = (d+1) % 4;
}
hx += dx[d]; hy += dy[d];
if(hx < 0 || hx >= N || hy < 0 || hy >= N) break; // ๋ฒฝ์ ๋ถ๋ชํ๊ฑฐ๋
if(snake[hx][hy]) break; // ์๊ธฐ์์ ์ ๋ชธ๊ณผ ๋ถ๋ชํ
snake[hx][hy] = true;
road.offer(new int[]{hx, hy});
if(map[hx][hy]) map[hx][hy] = false;
else {
snake[tx][ty] = false;
tx = road.peek()[0];
ty = road.poll()[1];
}
time++;
}
System.out.println(time);
}
time ๋ณ์์ ์ง๋ ์๊ฐ์ ์ ์ฅํ๋ค. hx, hy๋ ๋ฑ์ ๋จธ๋ฆฌ ์์น๋ฅผ tx, ty๋ ๋ฑ์ ๊ผฌ๋ฆฌ ์์น๋ฅผ ์ ์ฅํ๋ค.
d์๋ ์ด๋ ๋ฐฉํฅ์ ์ ์ฅํ๋ค.
Queue์ ๋จธ๋ฆฌ์ ์ด๋ ์์น๋ฅผ ์ ์ฅํ์ฌ ๊ผฌ๋ฆฌ๊ฐ ๋ฐ๋ผ์ ์ด๋ํ ์ ์๊ฒ ํ๋ค.
๋จผ์ ์ด๋ํด์ผ ๋๋ ํ์์ ์ด๋ ๋ฐฉํฅ์ ๋ณ๊ฒฝํด์ค๋ค.
์ด๋ ๋จธ๋ฆฌ๊ฐ ๋ฒฝ์ ๋ถ๋ชํ๊ฑฐ๋ ๋ชธํต๊ณผ ๋ง๋๊ฒ ๋๋ฉด ๊ฒ์์ด ์ข ๋ฃ๋๋ฏ๋ก break ์ฒ๋ฆฌ๋ฅผ ํ๋ค.
๊ทธ ๋ค ๊ผฌ๋ฆฌ์ ์ด๋์ ์ค์ ํด์ค๋ค.
๋ง์ง๋ง์ผ๋ก ์๊ฐ์ ์ฆ๊ฐ์ํจ๋ค.
'๐ Problem Solving > ๐ป Samsung SW Expert' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] BOJ ๋ฐฑ์ค 19236 ์ฒญ์๋ ์์ด (0) | 2023.03.30 |
---|---|
[JAVA] BOJ ๋ฐฑ์ค 17070 ํ์ดํ์ฎ๊ธฐ๊ธฐ1 (0) | 2023.03.29 |
[JAVA] BOJ ๋ฐฑ์ค 17825 ์ฃผ์ฌ์ ์ท๋์ด (0) | 2023.03.06 |
[JAVA] BOJ ๋ฐฑ์ค 19237 ์ด๋ฅธ ์์ด (0) | 2023.03.06 |
[JAVA] BOJ ๋ฐฑ์ค 20061 ๋ชจ๋ ธ๋ฏธ๋ ธ๋๋ฏธ๋ ธ 2 (1) | 2023.03.05 |