17136. 색종이 붙이기 본문
문제링크 : https://www.acmicpc.net/problem/17136
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 | #include <iostream> using namespace std; int arr[10][10]; bool visited[10][10]; int result = 25; bool finishFlag = false; int min(int a, int b) { if (a > b) return b; else return a; } int check(int a, int b) { for (int t = 5; t >= 1; t--) { if (a + t > 10 || b + t > 10) continue; bool flag = true; for (int i = a; i < a + t; i++) { for (int j = b; j < b + t; j++) { if (arr[i][j] == 0||visited[i][j]) { flag = false; i = a + t; break; } } } if (flag) { return t; } } return 0; } void dfs(int x,int y,int count[]) { if (x == 10) { dfs(0, y + 1, count); return; } if (x == 9 && y == 9) { if (!visited[x][y] && arr[x][y] == 1) { if (count[1] >= 5) return; count[1]++; } int sum = count[1] + count[2] + count[3] + count[4] + count[5]; result = min(result, sum); finishFlag = true; return; } if (visited[x][y]) { dfs(x + 1, y, count); return; } int next = check(x, y); if (next == 0) { visited[x][y] = true; dfs(x + 1, y, count); visited[x][y] = false; return; } for (int i = next; i >= 1; i--) { if (count[i] >= 5) continue; for (int iNext = 0; iNext < i; iNext++) { for (int jNext = 0; jNext < i; jNext++) { int nextX = x + iNext; int nextY = y + jNext; visited[nextX][nextY] = true; } } count[i]++; dfs(x + 1, y, count); count[i]--; for (int iNext = 0; iNext < i; iNext++) { for (int jNext = 0; jNext < i; jNext++) { int nextX = x + iNext; int nextY = y + jNext; visited[nextX][nextY] = false; } } } } int main() { fill(&arr[0][0], &arr[9][10], 0); fill(&visited[0][0], &visited[9][10], false); int count[6] = { 0 }; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { scanf("%d", &arr[i][j]); } } dfs(0, 0, count); if (finishFlag) printf("%d", result); else printf("-1"); } | cs |
'알고리즘 문제풀기 > 백준(Acmicpc)' 카테고리의 다른 글
17071. 숨바꼭질 5 (4) | 2019.03.26 |
---|---|
7569. 토마토 (0) | 2019.03.06 |
16987. 계란으로 계란치기 (2) | 2019.03.02 |
16986. 인싸들의 가위바위보 (0) | 2019.03.02 |
1322. X와 K (0) | 2019.01.31 |
Comments