dakyommii/AlgorithmReview

[Week 1] BOJ #9184 self review-hanjuuuuuu

Opened this issue · 0 comments

self review

1. 해결 시도 과정

피보나치 함수처럼 재귀는 똑같고 시간문제라면 당연히 메모이제이션을 사용해봐야겠다고 생각했다.

2. 아이디어

문제에서 제시한 것과 같이 재귀함수 w(a,b,c)를 if문을 통하여 코드를 작성하였다.

3. 코드 설명

#include <iostream>
#include <cstring>
using namespace std;

int a, b, c;
int memo[21][21][21];

int w(int a, int b, int c) {
	if (memo[a][b][c] != 0)
		return memo[a][b][c];

	if (a <= 0 || b <= 0 || c <= 0) 
		return 1;

	if (a > 20 || b > 20 || c > 20) 
		return memo[a][b][c] = w(20, 20, 20);
	
	if (a < b && b < c) 
		return memo[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);

	else 
		return memo[a][b][c] = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);
	
}

	int main(){
		memset(memo, 0, sizeof(memo));
		while (true) {
			cin >> a >> b >> c;

			if (a == -1 && b == -1 && c == -1) {
				break;
			}
			cout << "w(" << a << ", " << b << ", " << c << ") = " << w(a, b, c) << endl;
		}

		return 0;
	}

4. 막힌 점 및 개선 사항

계속 런타임에러 (outofbounds)가 난다...