알고리즘 문제/BOJ
15684번 사다리 조작
parkit
2018. 8. 23. 18:15
728x90
반응형
역시나 삼성 SW 역량 테스트 기출 문제이다.
백트래킹과 브루트 포스 둘 다 적용된 문제이다.
다른 블로거 분 코드를 보면서 공부를 했고, 참고를 했다.
https://www.acmicpc.net/problem/15684
이 문제를 풀면서 기억해야 할 것
1. 실제 사다리에서는 오른쪽으로 갔다가 바로 내려가는데,
코드 상에서는 35, 40 번째 줄인 continue;가 그 역할을 한다.
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 87 88 89 90 91 92 93 94 95 96 97 | #include <iostream> #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; int N = 0, M = 0, H = 0; int map[31][31] = { 0, }; int result = -1; void BackTracking(int m, int cnt) { if (m == cnt) { for (int column = 1; column <= N; column++) { int start = column; for (int row = 1; row <= H; row++) { if (map[row][start] == 0) continue; if (map[row][start] > 0) { if (map[row][start] == 1) { ++start; continue; } else if (map[row][start] == 2) { --start; continue; } } } if (column != start) return; } result = cnt; return; } for (int i = 1; i <= H; i++) { for (int j = 1; j <= N-1; j++) { if (map[i][j] == 0 && map[i][j + 1] == 0) { map[i][j] = 1; map[i][j + 1] = 2; BackTracking(m, cnt + 1); map[i][j] = 0; map[i][j + 1] = 0; } } } } int main(void) { int row = 0, column = 0; memset(map, 0, sizeof(map)); scanf("%d %d %d", &N, &M, &H); // H : 행, N : 열 for(int i = 0; i < M; i++) { scanf("%d %d", &row, &column); map[row][column] = 1; map[row][column + 1] = 2; } for (int i = 0; i < 4; i++) { BackTracking(i, 0); if (result != -1) break; } printf("%d\n", result); return 0; } | cs |
728x90
반응형