Advertisement
MegaVerkruzo

sdfsdf

Sep 4th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <utility>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. const int INF = 1e9;
  10.  
  11. random_device rd;
  12. mt19937 eese(rd());
  13.  
  14. int r = rand();
  15.  
  16. int check(vector<vector<int>>& a, int x, int y) {
  17. int k = 0;
  18. if (a[y - 1][x - 2] == 0) {
  19. k++;
  20. }
  21. if (a[y + 1][x - 2] == 0) {
  22. k++;
  23. }
  24. if (a[y + 2][x - 1] == 0) {
  25. k++;
  26. }
  27. if (a[y + 2][x + 1] == 0) {
  28. k++;
  29. }
  30. if (a[y + 1][x + 2] == 0) {
  31. k++;
  32. }
  33. if (a[y - 1][x + 2] == 0) {
  34. k++;
  35. }
  36. if (a[y - 2][x + 1] == 0) {
  37. k++;
  38. }
  39. if (a[y - 2][x - 1] == 0) {
  40. k++;
  41. }
  42. return k;
  43. }
  44.  
  45. void condition(vector<vector<int>>& a, int x, int y, int n, int m, vector<pair<int, int>>& z, int& mi) {
  46. if (x >= 2 && x <= m + 1 && y >= 2 && y <= n + 1 && a[y][x] == 0) {
  47. int ch = check(a, x, y);
  48. if (ch < mi) {
  49. mi = ch;
  50. z.clear();
  51. z.push_back({ y, x });
  52. }
  53. else if (ch == mi) {
  54. z.push_back({ y, x });
  55. }
  56. }
  57. }
  58.  
  59. void passageway(vector<vector<int>>& a, int x, int y, int n, int m, vector<pair<int, int>>& z) {
  60. int mi = 10;
  61. z.clear();
  62. condition(a, x - 1, y - 2, n, m, z, mi);
  63. condition(a, x + 1, y - 2, n, m, z, mi);
  64. condition(a, x + 2, y - 1, n, m, z, mi);
  65. condition(a, x + 2, y + 1, n, m, z, mi);
  66. condition(a, x + 1, y + 2, n, m, z, mi);
  67. condition(a, x - 1, y + 2, n, m, z, mi);
  68. condition(a, x - 2, y + 1, n, m, z, mi);
  69. condition(a, x - 2, y - 1, n, m, z, mi);
  70. if (mi == 10) {
  71. return;
  72. }
  73. int random = rand() % z.size();
  74. a[z[random].first][z[random].second] = a[y][x] + 1;
  75. passageway(a, z[random].second, z[random].first, n, m, z);
  76.  
  77. }
  78.  
  79. int main()
  80. {
  81. ifstream in("input.txt");
  82. int n, m;
  83. in >> n >> m;
  84. int x, y;
  85. for (int i = 1; i <= n; ++i) {
  86. for (int l = 1; l <= m; ++l) {
  87. char c;
  88. in >> c;
  89. if (c == 'K') {
  90. x = l;
  91. y = i;
  92. }
  93. }
  94. }
  95. vector<vector<int>> a(n + 4, vector<int>(m + 4, 0));
  96. a[y + 1][x + 1] = 1;
  97. for (int i = 0; i < n + 4; ++i) {
  98. for (int l = 0; l < m + 4; ++l) {
  99. if (i < 2 || l < 2 || i >= n + 2 || l >= m + 2) {
  100. a[i][l] = INF;
  101. }
  102. }
  103. }
  104. vector<pair<int, int>> z;
  105. passageway(a, x + 1, y + 1, n, m, z);
  106. for (int i = 2; i < n + 2; ++i) {
  107. for (int l = 2; l < m + 2; ++l) {
  108. printf("%6d ", a[i][l]);
  109. }
  110. printf("/n");
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement