강의

멘토링

커뮤니티

Inflearn Community Q&A

dpffpsk9076484's profile image
dpffpsk9076484

asked

10-Week C++ Coding Test | Algorithm Coding Test

7-M

7-M 질문입니다.

Written on

·

300

0

선생님 안녕하세요!

테스트케이스 3번의 답이 왜 2인지 모르겠습니다. 테스트케이스부터 이해를 못 했기 때문에 코드는 없습니다..

봄/여름 로직에서 첫 번째 나무 (2, 1, 3)은 양분[2][1]의 값이 2이기 때문에 죽고 두 번째 나무 (3, 2, 3)은 양분[3][2]의 값이 3이기 때문에 죽지 않습니다.

가을 로직에서 첫 번째 나무는 이미 죽었으니 고려하지 않고 두 번째 나무는 나이가 4가 되는데 5의 배수가 아니기 때문에 새로운 나무가 생겨나지 않는다고 생각했습니다.

제가 어디를 잘못 생각하고 있는지 궁금합니다.

C++코테 준비 같이 해요!

Answer 2

1

kundol님의 프로필 이미지
kundol
Instructor

  1. 일단 문제 지문을 보시면 "모든 칸에 대해서 조사를 한다. 가장 처음에 양분은 모든 칸에 5만큼 들어있다." 이런 지문이 있습니다. 이거 보시고 다시 생각해보시겠어요?

  2. 또한, 제 코드에 디버깅 코드를 추가한 코드를 드립니다. 예제3을 하면 이런식으로 디버깅 됩니다. 이거 참고해서 다시 생각해보시겠어요?

#include<bits/stdc++.h> 
using namespace std;
int n, m, k, A[14][14], yangbun[14][14], ret; 
vector<int> _map[14][14];
const int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
void springSummer(){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(_map[i][j].size() == 0) continue; 
            int die_tree = 0; 
            vector<int> temp; 
            sort(_map[i][j].begin(), _map[i][j].end());
            for(int tree : _map[i][j]){
                if(yangbun[i][j] >= tree){
                    yangbun[i][j] -= tree; 
                    temp.push_back(tree + 1); 
                }else{
                    //만약 그렇지 않다면 죽어버려!!
                    die_tree += tree / 2; 
                } 
            } 
            _map[i][j].clear();
            _map[i][j] = temp;  
            yangbun[i][j] += die_tree; 
        }
    }
} 
void fall(){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(_map[i][j].size() == 0) continue;  
            for(int tree : _map[i][j]){
                if(tree % 5 == 0){
                    for(int d = 0; d < 8; d++){
                        int ny = i + dy[d]; 
                        int nx = j + dx[d]; 
                        if(ny < 0 || ny >= n || nx < 0 || nx >= n) continue; 
                        _map[ny][nx].push_back(1);
                    }
                } 
            }  
        }
    }
}
void winter(){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            yangbun[i][j] += A[i][j];
        }
    }
}
void print(){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cout <<  yangbun[i][j] << " "; 
        }
        cout << '\n';
    }
    cout << "---턴종료\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL); 
    cin >> n >> m >> k; 
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> A[i][j]; 
            yangbun[i][j] = 5; 
        }
    }
    for(int i = 0; i < m; i++){
        int _a, _b, _c; cin >> _a >> _b >> _c; _a--; _b--;
        _map[_a][_b].push_back(_c);
    }
    for(int i = 0; i < k; i++){
        springSummer();fall(); winter(); 
        print();
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            ret += _map[i][j].size(); 
        }
    }
    cout << ret << "\n";
     
    return 0;   
}
/*
예제3입력
5 2 1
2 3 2 3 2
2 3 2 3 2
2 3 2 3 2
2 3 2 3 2
2 3 2 3 2
2 1 3
3 2 3

출력 :
7 8 7 8 7 
4 8 7 8 7 
7 5 7 8 7 
7 8 7 8 7 
7 8 7 8 7 
---턴종료
2

*/

0

dpffpsk9076484님의 프로필 이미지
dpffpsk9076484
Questioner

헉 바로 이해했습니다. 처음에 양분이 5 들어가는 걸 놓쳤군요... 그러면 첫 번째 나무가 죽지 않기 때문에 정답이 2가 되는 게 맞네요. 디버깅 코드를 보니 더 잘 이해가 됩니다. 답변 감사합니다!!

dpffpsk9076484's profile image
dpffpsk9076484

asked

Ask a question