All Articles

μž¬κ·€μ‹œ λ³€μˆ˜ 선언에 유의

λ°±μ€€ 1890번. κ·Έλƒ₯ DFS + DP 문제둜 μ•„λž˜ μ½”λ“œλ‘œλŠ” λ¬΄λ‚œνžˆ λŒμ•„κ°€μ§€λ§Œ

#include <iostream>

using namespace std;

int N;

int tile[100][100];
long long memo[100][100];

long long sol(int i, int j) {
	if (i == N - 1 && j == N - 1) {
		return 1;
	}
	if (memo[i][j] != -1) {
		return memo[i][j];
	}

	long long &ret = memo[i][j];
	ret = 0;
	if (i + tile[i][j] < N){
		ret += sol(i + tile[i][j], j);
	}
	if (j + tile[i][j] < N) {
		ret += sol(i, j + tile[i][j]);
	}

	return ret;
}

int main() {
	cin >> N;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> tile[i][j];
			memo[i][j] = -1;
		}
	}

	cout << sol(0, 0);
	return 0;
}#include <iostream>

using namespace std;

int N;

int tile[100][100];
long long memo[100][100];

long long sol(int i, int j) {
	if (i == N - 1 && j == N - 1) {
		return 1;
	}
	if (memo[i][j] != -1) {
		return memo[i][j];
	}


	long long ret = 0;
	if (i + tile[i][j] < N){
		ret += sol(i + tile[i][j], j);
	}
	if (j + tile[i][j] < N) {
		ret += sol(i, j + tile[i][j]);
	}
	memo[i][j] = ret;

	return ret;
}

int main() {
	cin >> N;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> tile[i][j];
			memo[i][j] = -1;
		}
	}

	cout << sol(0, 0);
	return 0;
}

쀑간에 sol λΆ€λΆ„μ—μ„œ λ³€μˆ˜λ₯Ό ν•˜λ‚˜ μ„ μ–Έν•΄μ„œ μ“°λŠ” κ²ƒμœΌλ‘œ λ°”λ€Œλ©΄ 호좜 μŠ€νƒμ—μ„œ λ©”λͺ¨λ¦¬λ₯Ό 무지 많이 μ¨μ„œ λ°”λ‘œ λ©”λͺ¨λ¦¬ μ΄ˆκ³Όκ°€ λœ¬λ‹€

long long sol(int i, int j) {
	if (i == N - 1 && j == N - 1) {
		return 1;
	}
	if (memo[i][j] != -1) {
		return memo[i][j];
	}


	long long ret = 0;
	if (i + tile[i][j] < N){
		ret += sol(i + tile[i][j], j);
	}
	if (j + tile[i][j] < N) {
		ret += sol(i, j + tile[i][j]);
	}
	memo[i][j] = ret;

	return ret;
}

DFSλ₯Ό μž¬κ·€λ‘œ 지 λ•ŒλŠ” λ³€μˆ˜ 선언에 μœ μ˜ν•˜μž.

Published Oct 6, 2018

If I keep marking the dots, someday they will πŸ”—πŸ”—