๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/CodeUp

2641 : ์ˆ๋‹ค๋ฆฌ์˜ ๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ (Small)

by dkswnkk 2022. 1. 14.

๋ฌธ์ œ

 

์ˆ๋‹ค๋ฆฌ์˜ ๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ (Small)

* 4๊ฐœ์˜ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ•์€ - 1 1 1 1 - 1 1 2 - 1 2 1 - 2 1 1 - 2 2 - 1 3 - 3 1

codeup.kr

 

์ฝ”๋“œ

#include <iostream>

using namespace std;

int dp[16];
int N,ans;
void dfs(int current,int before_step,int before_before_step){
    if(current==N){
        ans++;
        return;
    }
    else if(current>N) return;
    else{
        if(before_step==3||before_before_step==3){
            dfs(current+1,1,before_step);
            dfs(current+2,2,before_step);
        }
        else{
            dfs(current+1,1,before_step);
            dfs(current+2,2,before_step);
            dfs(current+3,3,before_step);
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin>>N;
    dfs(1,1,0);   //์ฒซ ์‹œ์ž‘์„ ํ•œ์นธ
    dfs(2,2,0);   //์ฒซ ์‹œ์ž‘์„ ๋‘์นธ
    dfs(3,3,0);   //์ฒซ ์‹œ์ž‘์„ ์„ธ์นธ
    cout<<ans;
    
}

 

ํ’€์ด

๋ฌธ์ œ๋งŒ ์ œ๋ชฉ๋งŒ ๋ณด๊ณ  ๋‹น์—ฐํžˆ DP์ธ ์ค„ ์•Œ์•˜๋”๋‹ˆ, ์นธ์„ ์˜ค๋ฅด๋Š” ์ตœ์†Œ ํšŸ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ผ์„œ ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.

์„ธ ์นธ์„ ์˜ค๋ฅด๋ฉด ๋‹ค์Œ๋ฒˆ ์ด๋™๊ณผ ๋‹ค์Œ ๋‹ค์Œ ๋ฒˆ ์ด๋™๊นŒ์ง€ ํ•œ๋ฒˆ์— ์„ธ ์นธ์„ ์˜ค๋ฅด์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

 

์ฑ„์ 

๋Œ“๊ธ€