Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <queue>
  6. #include <tuple>
  7.  
  8. using namespace std;
  9.  
  10. int dist[5][5][5];
  11.  
  12. int dx[6] = { 1, -1, 0, 0, 0, 0 };
  13. int dy[6] = { 0, 0, 1, -1, 0, 0 };
  14. int dz[6] = { 0, 0, 0, 0, 1, -1 };
  15.  
  16. void rotate(vector<vector<int>> &v) {
  17. vector<vector<int>> tmp(v);
  18.  
  19. for (int i = 0; i < 5; i++) {
  20. for (int j = 0; j < 5; j++) {
  21. v[i][j] = tmp[j][5 - i - 1];
  22. }
  23. }
  24. }
  25.  
  26. int main(void) {
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29.  
  30. vector<vector<vector<int>>> groups;
  31. for (int k = 0; k < 5; k++) {
  32. vector<vector<int>> group;
  33. for (int i = 0; i < 5; i++) {
  34. vector<int> row;
  35. for (int j = 0; j < 5; j++) {
  36. int x;
  37. cin >> x;
  38. row.push_back(x);
  39. }
  40. group.push_back(row);
  41. }
  42. groups.push_back(group);
  43. }
  44.  
  45. int ans = -1;
  46. vector<int> idx = { 0, 1, 2, 3, 4 };
  47. do {
  48. vector<vector<vector<int>>> vtmp;
  49. for (int i = 0; i < 5; i++) {
  50. vtmp.push_back(groups[idx[i]]);
  51. }
  52.  
  53. for (int r1 = 0; r1 < 4; r1++) {
  54. rotate(vtmp[0]);
  55. for (int r2 = 0; r2 < 4; r2++) {
  56. rotate(vtmp[1]);
  57. for (int r3 = 0; r3 < 4; r3++) {
  58. rotate(vtmp[2]);
  59. for (int r4 = 0; r4 < 4; r4++) {
  60. rotate(vtmp[3]);
  61. for (int r5 = 0; r5 < 4; r5++) {
  62. rotate(vtmp[4]);
  63.  
  64. memset(dist, -1, sizeof(dist));
  65. queue<tuple<int, int, int>> q;
  66. if (vtmp[0][0][0] == 0) continue;
  67. q.push(make_tuple(0, 0, 0));
  68. dist[0][0][0] = 0;
  69.  
  70. while (!q.empty()) {
  71. int z, x, y;
  72. tie(z, x, y) = q.front();
  73. q.pop();
  74.  
  75. for (int k = 0; k < 6; k++) {
  76. int tz = z + dz[k];
  77. int tx = x + dx[k];
  78. int ty = y + dy[k];
  79. if (tx < 0 || ty < 0 || tz < 0 || tx > 4 || ty > 4 || tz > 4) continue;
  80. if (dist[tz][tx][ty] != -1 || vtmp[tz][tx][ty] == 0) continue;
  81. dist[tz][tx][ty] = dist[z][x][y] + 1;
  82. q.push(make_tuple(tz, tx, ty));
  83. }
  84. }
  85.  
  86. if (dist[4][4][4] != -1) {
  87. if (ans == -1 || ans > dist[4][4][4]) {
  88. ans = dist[4][4][4];
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. } while (next_permutation(idx.begin(), idx.end()));
  97.  
  98. cout << ans << '\n';
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement