티스토리 뷰

알고리즘/백준

[백준] 1563번 개근상

JeongHyeon 2019. 11. 4. 21:55

문제

https://www.acmicpc.net/problem/1563

 

풀이

dynamic programming / recursion 을 통해 풀이 했습니다.

gCache : day 에 출석, 지각, 결석 횟수 저장

recursion 함수를 통해 저장된 값이 있으면 활용하고 아닌 경우 다음날을 3가지 경우로 분기시켜 진행 시켰습니다.

  1. 출석하는 경우 (day + 1, attendance + 1, late, 0)

  2. ㄴ지각하는 경우 (day + 1, attendance, late +1, 0)

  3. 결석하는 경우 (day +1, attendance, late, absent+1)

     

소스코드

#include <iostream>
#include <cstring>

using namespace std;

const int MAX_N = 1000 + 1;
const int MOD = 1000000;

int gCache[MAX_N][MAX_N+1][3][4]; // day, 출석, 지각, 결석
int n;

int solution(int day, int attendance, int late, int absent){

    if(late >= 2 || absent >= 3){
        return 0;
    }

    if(day == n)
        return 1;

    int &ret = gCache[day][attendance][late][absent];
    if(ret >= 0)
        return ret%MOD;

    ret = 0;
    
    ret += solution(day + 1, attendance + 1, late, 0)%MOD + solution(day + 1, attendance, late +1, 0)%MOD + solution(day +1, attendance, late, absent+1)%MOD;

    return ret%MOD;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;

    memset(gCache, -1, sizeof(gCache));

    cout << solution(0,0,0,0) << "\n";

    return 0;
}

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 6569번 몬드리안의 꿈  (1) 2019.11.05
[백준] 11727번 2xn 타일링 2  (0) 2019.11.04
[백준] 15686번 치킨 배달  (0) 2018.05.27
[백준] 13460 구슬 탈출 2  (0) 2018.04.13
[백준] 12100번 2048 (Easy)  (0) 2018.04.12
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
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 29 30
글 보관함