Recent Posts
Notice
No Rules Rules
Pascal's Travels (feat. 백준, 4620번) 본문
728x90
반응형
Pascal's Travels
https://www.acmicpc.net/problem/4620
4620번: Pascal's Travels
The input contains data for one to thirty boards, followed by a final line containing only the integer -1. The data for a board starts with a line containing a single positive integer n, 4 ≤ n ≤ 34, which is the number of rows in this board. This is fo
www.acmicpc.net
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
register int N;
char arr[35][35];
long long dp[35][35];
while (1) {
cin >> N;
if (N == -1)
break;
for (register int i = 1, j; i <= N; ++i)
for (j = 1; j <= N; ++j)
cin >> arr[i][j], arr[i][j] -= '0', dp[i][j] = 0;
dp[1][1] = 1;
for (register int x = 1, y; x <= N; ++x)
for (y = 1; y <= N; ++y) {
if ((x == N && y == N) || (dp[x][y] == 0))
continue;
register int dist = arr[x][y];
register int down = x + dist;
register int right = y + dist;
if (down <= N)
dp[down][y] = dp[down][y] + dp[x][y];
if (right <= N)
dp[x][right] = dp[x][right] + dp[x][y];
}
cout << dp[N][N] << "\n";
}
return 0;
}
// *&)*@*
- 문제는 dp를 이용하여 풀이하는 문제입니다.
- (1,1)에서 (N,N)까지의 모든 경로를 구할때, (x,y)는 (1,1)에서의 방문 횟수를 저장하고 이후 다시 (x,y)를 거친다면 이전 저장된 방문 횟수를 그대로 사용하게 됩니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
보드 점프 (feat. 백준, 3372번) (2) | 2022.09.06 |
---|---|
점프 (feat. 백준, 1890번) (0) | 2022.09.06 |
세 부분 (feat. 백준, 2993번) (0) | 2022.09.06 |
단어 나누기 (feat. 백준, 1251번) (0) | 2022.09.06 |
약수들의 합 (feat. 백준, 9506번) (0) | 2022.09.06 |
Comments