알고리즘 문제/BOJ
17069번 파이프 옮기기 2
parkit
2019. 3. 13. 23:15
728x90
반응형
https://www.acmicpc.net/problem/17069
17070번을 푼 BFS 코드로 배열 크기만 바꿔서
제출하였더니, 메모리 초과가 떴다.
생각해보니 BFS로는 풀 수 없는 문제였다.
그래서 다이나믹 프로그래밍으로 푼 코드 그대로 제출하였다.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; // pipe[행y][열x][파이프 번호] // 0 : ㅡ // 1 : 역 / // 2 : ㅣ long long pipe[40][40][3] = { 0, }; int input[40][40] = { 0, }; int N = 0; void zero(int y, int x, int s) { if (x + 1 <= N && input[y][x + 1] != 1) { pipe[y][x + 1][0] += pipe[y][x][s]; } } void one(int y, int x, int s) { if (y + 1 <= N && x + 1 <= N && input[y + 1][x] != 1 && input[y][x + 1] != 1 && input[y + 1][x + 1] != 1) { pipe[y + 1][x + 1][1] += pipe[y][x][s]; } } void two(int y, int x, int s) { if (y + 1 <= N && input[y + 1][x] != 1) { pipe[y + 1][x][2] += pipe[y][x][s]; } } int main(void) { scanf("%d", &N); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) scanf("%d", &input[i][j]); pipe[1][2][0] = 1; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (input[i][j]) continue; if (pipe[i][j][0] != 0) { zero(i, j, 0); one(i, j, 0); } if (pipe[i][j][1] != 0) { zero(i, j, 1); one(i, j, 1); two(i, j, 1); } if (pipe[i][j][2] != 0) { one(i, j, 2); two(i, j, 2); } } } printf("%lld\n", pipe[N][N][0] + pipe[N][N][1] + pipe[N][N][2]); return 0; } | cs |
728x90
반응형