1. 문제에 대한 이해
- 우리가 풀어야 할 문제는 무엇인가?
- 주어진 자료는 무엇인가?
- 조건은 무엇인가?
- 우리가 문제를 풀기 위해 주어진 자료가 충분한가?
- 숨겨진 조건이나 자료가 있는가? 그렇다면 그 것을 다른 방법으로 해석해보라.
- 문제는 무엇인가?
-> 어떻게 나무 재테크를 할 것인가?
->
2. 계획
- 전에 비슷한 문제를 알고 있는가?
- 이 문제를 푸는데 있어서 유용하게 쓸 수 있는 지식은 무엇인가?
- 비슷한 문제를 풀어본 적이 있다면 그 것을 활용할 수 있는가?
- 만약 문제를 풀 수 없다면 문제를 더 단순하게 하기 위해서 주어진 조건을 버려보아라
- 주어진 자료로부터 유용한 것을 이끌어 낼 수 있는가?
- 자료는 모두 사용했는가?
- 조건을 모두 사용했는가?
- 문제에 포함된 핵심적인 개념은 모두 고려했는가?
3. 실행
- 풀이 계획을 실행하고, 각 단계가 올바른지 점검하라.
[내 코드]
import java.util.*;
public class Main {
public static int N;
public static int M;
public static int K;
public static int[][] A;
public static int[][] nutritions;
public static int[][] addedNutritions;
public static ArrayList<Integer> [][] trees;
public static int answer;
public static int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
public static int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};
public boolean isInside(int x, int y){
if(1<=x && x<=N && 1<=y && y<=N){
return true;
}else{
return false;
}
}
public void winter(){
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
nutritions[i][j] += A[i][j];
}
}
}
public void fall(){
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
int len = trees[i][j].size();
for(int k=0; k<len; k++){
if(trees[i][j].get(k) % 5 == 0){
breed(i,j);
}
}
}
}
}
public void breed(int x, int y){
for(int i=0; i<8; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(isInside(nx,ny)){
trees[nx][ny].add(0, 1);
}
}
}
public void summer(){
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
nutritions[i][j] += addedNutritions[i][j];
}
}
}
public void spring(){
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
if(trees[i][j].size() != 0){
int addedNutrition = 0;
int isDead = -1;
int len = trees[i][j].size();
ArrayList<Integer> tmp = new ArrayList<>();
for(int k=0; k<len; k++){
int age = trees[i][j].get(k);
if(nutritions[i][j] - age >= 0){
nutritions[i][j] -= age;
trees[i][j].set(k, age+1);
}else{
isDead = k;
break;
}
}
for(int k=0; k<isDead; k++){
tmp.add(trees[i][j].get(k));
}
if(isDead != -1){
for(int k=isDead; k<len; k++){
addedNutrition += (trees[i][j].get(k)/2);
}
}
addedNutritions[i][j] = addedNutrition;
if(isDead != -1){
trees[i][j].clear();
for(int k=0; k<isDead; k++){
trees[i][j].add(tmp.get(k));
}
}
}
}
}
}
public void sort(){
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
Collections.sort(trees[i][j]);
}
}
}
public void treeFinance(){
int year = 0;
while(true){
if(year == K){
break;
}
addedNutritions = new int[N+10][N+10];
spring();
summer();
fall();
winter();
year++;
}
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
answer += trees[i][j].size();
}
}
}
public static void main(String args[]) {
Main T = new Main();
Scanner sc = new Scanner(System.in);
answer = 0;
N = sc.nextInt();
M = sc.nextInt();
K = sc.nextInt();
A = new int[N+10][N+10];
trees = new ArrayList[N+10][N+10];
nutritions = new int[N+10][N+10];
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
trees[i][j] = new ArrayList<>();
}
}
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
A[i][j] = sc.nextInt();
}
}
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
nutritions[i][j] = 5;
}
}
for(int i=1; i<=M; i++){
int x = sc.nextInt();
int y = sc.nextInt();
int age = sc.nextInt();
trees[x][y].add(age);
}
T.sort();
T.treeFinance();
System.out.println(answer);
}
}
4. 반성
- 문제를 다른 방식으로 해결할 수 있는가?
- 결과나 방법을 어떤 다른 문제에 활용할 수 있는가?
- 어떻게 하면 더 효율적으로 문제를 해결할 수 있는가?
- 어떻게 하면 더 효과적으로 문제를 해결할 수 있는가?
- 어떻게 하면 더 효율적으로 문제를 해결할 수 있는가?
-> 1시간 반 정도 걸렸다
-> 더 효율적으로 해결할 수 있지 않았을까?
-> 우선 sort 같은 경우는 매번 할 필요 없이, 처음에만 해주면 된다
- ArrayList<Integer> [][] trees와 같이 2차원 ArrayList 배열을 선언할 수 있다
->
- 코드가 너무 길다
-> 줄일 수 있지 않을까?
-> 어떤 점에서 줄일 수 있을까?
-> 질문을 해결해서 다시 한 번 코드를 작성해보자
'PS' 카테고리의 다른 글
| 트리의 부모 찾기 [BOJ 11725] (0) | 2022.10.06 |
|---|---|
| 한국이 그리울 땐 서버에 접속하지 [BOJ 9996] (0) | 2022.10.01 |
| 행렬 테두리 회전하기 [프로그래머스] (0) | 2022.09.30 |
| 다단계 칫솔 판매 [프로그래머스] (1) | 2022.09.30 |
| 괄호 추가하기 [BOJ 16637] (0) | 2022.09.27 |