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
- QUICKSTARTGUIDE
- YBMCOS
- 완전탐색
- 구현
- 재귀함수
- 자바PS
- DFS
- 게더타운시작
- 다이나믹프로그래밍
- 시뮬레이션
- 취득후기
- spring
- java
- 알고리즘
- BFS
- COSPROJAVA1급
- 네트워크플로우
- 다익스트라
- GatherTown
- 01BFS
- 우선순위큐
- 백준코딩테스트
- 세그먼트트리
- COSPRO
- PS
- dp
- deque
- 이젠 골드구현도 어렵네..
- 엘라스틱서치
- 백준
Archives
- Today
- Total
공부공간
BOJ - 16235 ) 나무 재테크 본문
https://www.acmicpc.net/problem/16235
16235번: 나무 재테크
부동산 투자로 억대의 돈을 번 상도는 최근 N×N 크기의 땅을 구매했다. 상도는 손쉬운 땅 관리를 위해 땅을 1×1 크기의 칸으로 나누어 놓았다. 각각의 칸은 (r, c)로 나타내며, r은 가장 위에서부터 떨어진 칸의 개수, c는 가장 왼쪽으로부터 떨어진 칸의 개수이다. r과 c는 1부터 시작한다. 상도는 전자통신공학과 출신답게 땅의 양분을 조사하는 로봇 S2D2를 만들었다. S2D2는 1×1 크기의 칸에 들어있는 양분을 조사해 상도에게 전송하고, 모든
www.acmicpc.net
할짓없는 친구가 NXN 땅을 사서 나무를 키우는 문제이다. 로봇도 사서 매년 겨울에 양분도 주고,
각 계절마다 설명대로의 작업을 거치게 되는데,
설명을 이해하니 큐2개로 해결될것 같았다. 막상 구현하다보니 죽은 나무를 관리하는 큐를 하나더 썼지만..
우선순위큐로 한번해봤는데 터져서 덱으로 바꾸어 보았더니 통과해버렸다..
그냥 문제 조건대로 코딩해주면 된다.
package 풀문제;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class BOJ_나무재테크 {
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 m = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int Nutri_map[][] = new int[n+1][n+1];
int Robot_map[][] = new int[n+1][n+1];
int dirx[] = {1,1,-1,-1,0,0,-1,1};
int diry[] = {1,-1,1,-1,1,-1,0,0};
ArrayDeque<int [] > dq = new ArrayDeque<>();
ArrayDeque<int [] > dq2 = new ArrayDeque<>();
ArrayDeque<int []> dead = new ArrayDeque<>();
for(int y= 1 ; y <= n ; y ++) {
st = new StringTokenizer(br.readLine());
for(int x= 1 ; x <= n ; x++) {
Robot_map[y][x] = Integer.parseInt(st.nextToken());
Nutri_map[y][x] = 5;
}
}
for(int i = 0 ; i < m ; i ++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int age = Integer.parseInt(st.nextToken());
dq.add(new int[] {age,x,y});
}
for(int year = 1 ; year <= k ; year++) {
// 봄
while(!dq.isEmpty()) {
int now[] = dq.poll();
int age = now[0];
int y = now[1] , x = now[2];
if(Nutri_map[y][x] < age) {
dead.add(new int[] {age, y,x});
continue;
}
Nutri_map[y][x] -= age;
dq2.add(new int[] {age+1,y,x});
}
// 여름
while(!dead.isEmpty()) {
int now[] = dead.poll();
Nutri_map[now[1]][now[2]] += (now[0]/2);
}
// 가을
while(!dq2.isEmpty()) {
int now[] = dq2.poll();
int y = now[1] , x = now[2];
int age = now[0];
if( (age % 5) ==0) {
for(int index = 0 ; index < 8 ; index++) {
int ny= y + diry[index];
int nx= x + dirx[index];
if(ny >= 1 && nx>=1 && ny <= n && nx <= n) {
dq.addFirst(new int[] {1,ny,nx});
}
}
}
dq.add(new int[]{age,y,x});
}
// 겨울
for(int y = 1 ; y <= n ; y++) {
for(int x = 1 ; x<= n ; x++) {
Nutri_map[y][x] += Robot_map[y][x];
}
}
}
System.out.print(dq.size());
}
}
그래서 좀 우선순위 큐를 조금만? 쓰도록 조정했더니
우선순위큐로도 통과가 가능했다..
하지만 문제 시간보가 훨씬 넘었고,, 찾아보니 우선순위 큐는 내부적으로 맥스힙구조이기 때문에
, Searching에 O(1)이 안걸릴수있다고한다..
'알고리즘 > 구현,시뮬' 카테고리의 다른 글
BOJ - 3190 ) 뱀 (0) | 2020.03.07 |
---|---|
BOJ - 13458 ) 시험감독 (0) | 2020.03.07 |
BOJ - 17144 ) 미세먼지 안녕! (0) | 2020.02.26 |
BOJ- 17143 ) 낚시왕 (0) | 2020.02.26 |
SWEA - 5648 ) 원자소멸 시뮬레이션 (4) | 2020.02.23 |
Comments