알고리즘 문제/BOJ
10164번 격자상의 경로
parkit
2020. 1. 7. 22:42
728x90
반응형
https://www.acmicpc.net/problem/10164
아래와 비슷한 문제 풀이법이다.(프로그래머스의 '등굣길' 문제)
따라서, 아래의 블로그 글만 보면 되니, 풀이는 생략.
굳이 언급하자면,
거쳐가려면, 두 경우의 수를 곱해야 한다.(곱사건)
https://hsdevelopment.tistory.com/350?category=1048260
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <bits/stdc++.h> using namespace std; #define Max 18 int dp[Max][Max]; int Count(int sy, int sx, int ey, int ex) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < Max; i++) { dp[1][i] = dp[i][1] = 1; } ey = ey - sy + 1; ex = ex - sx + 1; for (int i = 2; i <= ey; i++) { for (int j = 2; j <= ex; j++) { dp[i][j] += dp[i - 1][j] + dp[i][j - 1]; } } //printf("ey = %d, ex = %d, dp[%d][%d] = %d\n", ey, ex, ey, ex, dp[ey][ex]); return dp[ey][ex]; } int main() { cin.tie(0); int r, c, k; scanf("%d %d %d", &r, &c, &k); if (k) { int num = 0, y = r, x = c; bool stop = false; for (int i = 1; i <= r; i++) { for (int j = 1; j <= c; j++) { if (++num == k) { y = i; x = j; stop = true; break; } } if (stop) break; } printf("%d\n", Count(1, 1, y, x) * Count(y, x, r, c)); } else { printf("%d\n", Count(1, 1, r, c)); } return 0; } | cs |
728x90
반응형