Advertisement
Zennoma

Dz2.2

May 10th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. bool** labyrinth;
  7. int count;
  8. int path[100][100];
  9. int c = 0;
  10. int way = 0;
  11. void labgen(int n)
  12. {
  13. labyrinth = new bool* [n + 2];
  14. for (int i = 0; i < n + 2; i++)
  15. {
  16. labyrinth[i] = new bool[n + 2];
  17. }
  18. srand(time(NULL));
  19. for (int i = 0; i < n+2; i++)
  20. for (int j = 0; j < n+2; j++)
  21. labyrinth[i][j] = 0;
  22. for (int i = 1; i < n + 1; i++)
  23. for (int j = 1; j < n + 1; j++)
  24. labyrinth[i][j] = rand()%3;
  25. labyrinth[n][n] = 1;
  26. labyrinth[1][1] = 1;
  27. }
  28. void printlab(int n)
  29. {
  30. for (int i = 0; i < n; i++)
  31. {
  32. for (int j = 0; j < n; j++)
  33. {
  34. cout.width(5);
  35. cout << labyrinth[i][j];
  36. }
  37. cout << endl;
  38. }
  39. }
  40.  
  41. void preprintlab(int n)
  42. {
  43. for (int i = 1; i < n+1; i++)
  44. {
  45. for (int j = 1; j < n+1; j++)
  46. {
  47. cout.width(5);
  48. cout << labyrinth[i][j];
  49. }
  50. cout << endl;
  51. }
  52. }
  53. int search(int x,int y, int n)
  54. {
  55. c++;
  56. path[c][1] = x;
  57. path[c][2] = y;
  58.  
  59. if (x == n && y == n)//базисное высказывание
  60. {
  61. for (int i = 1; i < c + 1; i++) cout << path[i][1]<<"," << path[i][2]<<" ";
  62. c--;
  63. cout << endl;
  64. way++;
  65. return way;
  66. }
  67.  
  68. labyrinth[x][y] = 0;
  69. if (labyrinth[x + 1][y] == 1)
  70. {
  71. search(x + 1, y, n); ::count++;
  72. }
  73. if (labyrinth[x - 1][y] == 1)
  74. {
  75. search(x - 1, y, n);
  76. ::count++;
  77. }
  78. if (labyrinth[x][y+1] == 1)
  79. {
  80. search(x, y+1, n);
  81. ::count++;
  82. }
  83. if (labyrinth[x][y-1] == 1)
  84. {
  85. search(x, y-1, n);
  86. ::count++;
  87. }
  88. labyrinth[x][y] = 1;
  89. c--;
  90. return way;
  91. }
  92.  
  93. int main()
  94. {
  95. int n;
  96. cin >> n;
  97. labgen(n);
  98. preprintlab(n);
  99. cout << "'walled' labyrinth"<<endl;
  100. printlab(n+2);
  101. ::count = 1;
  102. int way=search(1, 1, n);
  103. if (way >=1)
  104. cout << "There is " << way << " way(s)" << endl;
  105. else cout << "There is no way out" << endl;
  106. cout << ::count << " Count of reccursions fuctions called";
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement