Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 엘라스틱서치
- DFS
- 취득후기
- 시뮬레이션
- 완전탐색
- 우선순위큐
- COSPROJAVA1급
- 구현
- 다이나믹프로그래밍
- java
- COSPRO
- 세그먼트트리
- 재귀함수
- 이젠 골드구현도 어렵네..
- QUICKSTARTGUIDE
- 백준
- GatherTown
- 01BFS
- 백준코딩테스트
- deque
- 게더타운시작
- dp
- PS
- 다익스트라
- BFS
- spring
- 네트워크플로우
- YBMCOS
- 알고리즘
- 자바PS
Archives
- Today
- Total
공부공간
BOJ - 3190 ) 뱀 본문
https://www.acmicpc.net/problem/3190
3190번: 뱀
문제 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임은 NxN 정사각 보드위에서 진행되고, 몇몇 칸에는 사과가 놓여져 있다. 보드의 상하좌우 끝에 벽이 있다. 게임이 시작할때 뱀은 맨위 맨좌측에 위치하고 뱀의 길이는 1 이다. 뱀은 처음에 오른쪽을 향한다. 뱀은 매 초마다 이동을 하는데 다음과 같은 규칙을 따
www.acmicpc.net
Dummy라는 게임은 뱀 이동하면서 벽이나 자기의 몸에 닿으면 끝나느 게임이다.
중간중간에 사과를 먹으면 몸의 길이가 늘어난다.
또한, 시간에 따라서 방향이 바뀌기때문에 덱을 이용하여서 사과를 먹으면 앞쪽에 좌표를 추가하고,
사과를 먹지않았다면, 앞쪽에 추가하고 맨뒤의 값을 poll해주면서 진행하면된다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.StringTokenizer;
public class Main {
static class node{
int time; char dir;
public node(int time , char dir) {
this.time = time ;
this.dir = dir;
}
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N =Integer.parseInt(st.nextToken());
int map[][] = new int[N+1][N+1];
st = new StringTokenizer(br.readLine());
int apple = Integer.parseInt(st.nextToken());
for(int index = 1 ; index < apple+1 ; index++) {
st = new StringTokenizer(br.readLine());
map[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = 1;
}
st = new StringTokenizer(br.readLine());
int move_time = Integer.parseInt(st.nextToken());
node move_coordinate[]= new node[move_time+1];
for(int index = 1; index < move_time+1 ; index++) {
st = new StringTokenizer(br.readLine());
move_coordinate[index] = new node(Integer.parseInt(st.nextToken()), st.nextToken().charAt(0));
}
ArrayDeque<int []> snake = new ArrayDeque<>();
snake.add(new int [] {1,1});
int dir = 3 ; // 상하좌우
int diry[] = {-1,1,0,0};
int dirx[] = {0,0,-1,1};
int nowy = 0 , nowx =0;
int nexty = 0 , nextx = 0;
map[1][1] = 2;
int time = 0;
int index =1;
while(true) {
if(index <= move_time) {
if(move_coordinate[index].time == time) {
if(move_coordinate[index].dir=='D') {
if(dir == 3) {dir =1;}
else if(dir ==2) {dir =0;}
else if(dir ==1) {dir =2;}
else if(dir ==0) {dir =3;}
}
else if(move_coordinate[index].dir=='L') {
if(dir == 3) {dir=0;}
else if(dir ==2) {dir=1;}
else if(dir ==1) {dir=3;}
else if(dir ==0) {dir=2;}
}
index++;
}
}
int now[] = snake.peek();
nowy = now[0] ; nowx = now[1];
nexty = nowy + diry[dir] ; nextx = nowx+ dirx[dir];
if(nexty >=1 && nextx >=1 && nexty <=N && nextx <=N) {
if(map[nexty][nextx] == 1) {
snake.addFirst(new int[] {nexty, nextx});
map[nexty][nextx] = 2;
}
else if(map[nexty][nextx] == 0) {
int last[] =snake.pollLast();
snake.addFirst(new int[] {nexty, nextx});
map[last[0]][last[1]] = 0 ; map[nexty][nextx] =2;
}
else if(map[nexty][nextx] == 2) {
break;
}
}
else {
break;
}
time++;
}
System.out.println(time+1);
}
}
'알고리즘 > 구현,시뮬' 카테고리의 다른 글
BOJ - 1717 ) 집합의 표현 (0) | 2020.03.22 |
---|---|
BOJ - 14891 ) 톱니바퀴 (0) | 2020.03.08 |
BOJ - 13458 ) 시험감독 (0) | 2020.03.07 |
BOJ - 16235 ) 나무 재테크 (0) | 2020.02.28 |
BOJ - 17144 ) 미세먼지 안녕! (0) | 2020.02.26 |
Comments