[BOJ]24460 특별상이라도 받고 싶어

강동현·2023년 12월 23일
0

코딩테스트

목록 보기
41/111
  • 재귀 문제가 풀리기 시작했다.
  1. Base Condition 정의
  2. f(BaseCondition)>f(BaseCondition+1)f(Base Condition) -> f(Base Condition + 1)으로 가는 규칙 발견
  3. 위 코드를 활용한 문제 해결
  • sol1
    -n = 1칸이 될 경우 그 칸을 반환
    -사각형4분면으로 나눠 재귀 호출
    -두번쨰로 작은 값을 호출하기 위한 vector<int>vector<int> 사용
#include <bits/stdc++.h>
using namespace std;
int N, min = INT_MAX;
vector<vector<int>> board(1025, vector<int>(1025, 0));
int recursive(int x, int y, int n){
    vector<int> ans(4);
    if(n == 1) {
        return board[x][y];
    }
    int lu = recursive(x, y, n / 2);
    int ld = recursive(x + n / 2, y, n / 2);
    int ru = recursive(x, y + n / 2, n / 2);
    int rd = recursive(x + n / 2, y + n / 2, n / 2);
    ans[0] = lu;
    ans[1] = ld;
    ans[2] = ru;
    ans[3] = rd;
    sort(ans.begin(), ans.end());
    //두번쨰로 작은 값을 리턴
    return ans[1];
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> N;
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < N; ++j){
            cin >> board[i][j];
        }
    }
    cout << recursive(0, 0, N);
    return 0;
}
profile
GAME DESIGN & CLIENT PROGRAMMING

0개의 댓글