1. ๋ฌธ์ ๋ถ์
[3190 ๋ฑ ๋ฌธ์ ๋งํฌ](https://www.acmicpc.net/problem/3190)
์ง๋ ์ด ๊ฒ์์ ์๋ฆฌ๋๋ก ์์ง์ด๋ ๋ฌธ์ ๋น ๊ณต๊ฐ์ 0, ์ฌ๊ณผ๋ 1, ๋ฑ์ด ์กด์ฌํ๋ ์๋ฆฌ๋ 2๋ก ํ์ํ๋ค.
- ๋ฑ์ด 2์ฐจ์ ๋ฐฐ์ด ๋ด๋ฅผ ์์ง์ -> ์์ง์ด๋๊น, ์ง๋๊ฐ ์ฅ์๋ 2๊ฐ ์๋๋ผ ๋ค์ 0์ผ๋ก ์์ผํจ.
- ์ฌ๊ณผ๋ฅผ ๋จน์ผ๋ฉด ๋ฑ์ ๋ชธ์ง์ด 1 ์ปค์ง
- ๋ฑ์ด ๋ฒฝ์ ๋ถ๋ชํ๋ฉด ๊ฒ์์ค๋ฒ
- ๋ฑ์ด ์์ ์ ๋ชธ์ ๋ถ๋ชํ๋ฉด ๊ฒ์์ค๋ฒ
- ๋ฑ์ ๋ฐฉํฅ ์ ํ์ด ๊ฐ๋ฅ -> ๋ช ์ด์ ๋ฐ์๊ณ, ์๊ณ ๋ฐฉํฅ ์ค ์ด๋๋ก ์์ง์ผ์ง๋ ์ฃผ์ด์ง๋ค.
- ๊ฒ์์ด ๋ช ์ด๊ฐ ๊ฑธ๋ฆฌ๋์ง ๊ตฌํ๊ธฐ (๋ฑ์ ํ ๋ฒ ์์ง์ผ ๋, 2์ฐจ์ ๋ฐฐ์ด์์ ํ ์นธ ์์ง์ด๊ณ , ์ด๋ ์๊ฐ์ด 1 ๋ ๋ค.)
2. ํธ๋ ์๋ฆฌ
์ ๊ธฐ ๋์ค๋ 1~5๋ฅผ ํจ์๋ก ์์ฑํด์, ํจ์๊ฐ ๋์๊ฐ๋๋ก ํ๋ฉด ๋๋ค.
์ฒ์์๋ Stream์ผ๋ก ํ๋ ค๊ณ ํ๋๋ฐ, ๋ฐฑ์ค์์๋ List์ getFirst()๋ผ๋ ๊ฐ removeLast() ๋ผ๋ ๊ฐ๋ถํฐ Stream ์ ๋ฐ์ ์ฌ์ฉํ์ง ๋ชปํด์ Legacyํ๊ฒ ๋ฐ๊พธ๋ ๊ฒ์ด ์ผ์ด์๋ค.
3. ์ฝ๋ ๋ถ์
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
/*
* 3190๋ฒ ๋ฑ
* */
public class Main {
// 0. ์ ๋ณด N = ๋ฐฐ์ด์ ๊ธธ์ด, apples = ์ฌ๊ณผ์ ๊ฐ์,
// map = 2์ฐจ์ ๋ฐฐ์ด, idx,idy = ๋์๊ฐ ๋ฐฉํฅ
// snakes = ๋ฑ์ด ์ฐจ์งํ๊ณ ์๋ ๊ณต๊ฐ
// nowD = ๋ฑ์ ํ์ฌ ๋ฐฉํฅ
// snakesMovement = [index]๊ฐ ์ด, ๊ฐ์ด ๋ฐฉํฅ
// -------- ๋น์นธ: 0, ์ฌ๊ณผ: 1, ๋ฑ: 2
static int N, apples;
static int [][] map;
static int [] idx = {-1,0,1,0};
static int [] idy = {0,1,0,-1};
static int nowD = 1;
static boolean isFinished = false;
static ArrayList<int []> snakes = new ArrayList<>();
static char [] snakesMovement = new char [10001];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 1. ๊ฐ ์
๋ ฅ --------------------------------------------------------------------
N = Integer.parseInt(br.readLine());
apples = Integer.parseInt(br.readLine());
map = new int [N][N];
for (int i = 0; i < N; i++) {
Arrays.fill(map[i], 0);
}
// 1-2. ์ฌ๊ณผ ์
๋ ฅ
for (int i = 0; i < apples; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
map[Integer.parseInt(st.nextToken())-1][Integer.parseInt(st.nextToken())-1] = 1;
}
int moveCnt = Integer.parseInt(br.readLine());
// 1-3. ๋ฑ์ ๋ฐฉํฅ์ ํ ์
๋ ฅ
for (int i = 0; i < moveCnt; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
snakesMovement[Integer.parseInt(st.nextToken())] = st.nextToken().charAt(0);
}
// 1-4. ๋ฑ์ ์ด๊ธฐ๊ฐ ์
๋ ฅ
snakes.add(new int[]{0, 0});
// -------------------------------------------------------------------------------
// ์คํ----------------------------------------------------------------------------
for (int i = 0; i < snakesMovement.length; i++) {
if(snakesMovement[i] == '0') {
moving();
if(isFinished){
System.out.println(i+1);
return;
}
continue;
}
snakeTurnAbout(snakesMovement[i]);
moving();
if(isFinished){
System.out.println(i+1);
return;
}
}
}
// 2. ๋ฐฉํฅ ์ ํ------------------------------------------------------------------------
public static void snakeTurnAbout (char direction) {
switch (direction) {
case 'D': {
nowD = nowD == 3? 0 : nowD+1;
break;
}
case 'L': {
nowD = nowD == 0? 3 : nowD-1;
break;
}
}
}
// 3. ์์ ์ ๋ชธ๊ณผ ๋ถ๋ชํ๋์ง ํ์ธ
public static boolean isCrashedWithBody () {
// ์ ๋จธ๋ฆฌ๊ฐ ์์ ์ ๋ชธ๊ณผ ๋ถ๋ชํ๋์ง ํ์ธ
int x = snakes.get(0)[0];
int y = snakes.get(0)[1];
for (int i = 1; i < snakes.size(); i++) {
if(snakes.get(i)[0] == x && snakes.get(i)[1] == y){
return true;
}
}
return false;
// ๋ฐฑ์ค Stream ์ฌ์ฉ์ด ์๋จ.
// return snakes.stream().skip(1).anyMatch(snakes -> snakes[0] == x && snakes[1] == y);
}
// 4. ๋ฒฝ๊ณผ ๋ถ๋ชํ๋์ง ํ์ธ
public static boolean isCrashedWithWall () {
// ์ ๋จธ๋ฆฌ๊ฐ ์์ ์ ๋ชธ๊ณผ ๋ถ๋ชํ๋์ง ํ์ธ
int x = snakes.get(0)[0];
int y = snakes.get(0)[1];
return x < 0 || y < 0 || x >= N || y >= N;
}
// 5. ์์ง์ด๊ธฐ
public static void moving () {
int nx = snakes.get(0)[0] + idx[nowD];
int ny = snakes.get(0)[1] + idy[nowD];
if(nx>=0 && ny>=0 && nx < N && ny < N && map[nx][ny] == 1 ){
ateApple(nx,ny);
return;
}
snakes.add(0,new int[]{nx,ny});
if(isCrashedWithWall() || isCrashedWithBody()){
isFinished = true;
return;
}
snakes.remove(snakes.size()-1);
}
// 6. ์ฌ๊ณผ ๋จน๊ธฐ
public static void ateApple(int x, int y) {
map[x][y] = 0;
snakes.add(0,new int[]{x,y});
}
}
0