Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- bool** labyrinth;
- int count;
- int path[100][100];
- int c = 0;
- int way = 0;
- void labgen(int n)
- {
- labyrinth = new bool* [n + 2];
- for (int i = 0; i < n + 2; i++)
- {
- labyrinth[i] = new bool[n + 2];
- }
- srand(time(NULL));
- for (int i = 0; i < n+2; i++)
- for (int j = 0; j < n+2; j++)
- labyrinth[i][j] = 0;
- for (int i = 1; i < n + 1; i++)
- for (int j = 1; j < n + 1; j++)
- labyrinth[i][j] = rand()%3;
- labyrinth[n][n] = 1;
- labyrinth[1][1] = 1;
- }
- void printlab(int n)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cout.width(5);
- cout << labyrinth[i][j];
- }
- cout << endl;
- }
- }
- void preprintlab(int n)
- {
- for (int i = 1; i < n+1; i++)
- {
- for (int j = 1; j < n+1; j++)
- {
- cout.width(5);
- cout << labyrinth[i][j];
- }
- cout << endl;
- }
- }
- int search(int x,int y, int n)
- {
- c++;
- path[c][1] = x;
- path[c][2] = y;
- if (x == n && y == n)//базисное высказывание
- {
- for (int i = 1; i < c + 1; i++) cout << path[i][1]<<"," << path[i][2]<<" ";
- c--;
- cout << endl;
- way++;
- return way;
- }
- labyrinth[x][y] = 0;
- if (labyrinth[x + 1][y] == 1)
- {
- search(x + 1, y, n); ::count++;
- }
- if (labyrinth[x - 1][y] == 1)
- {
- search(x - 1, y, n);
- ::count++;
- }
- if (labyrinth[x][y+1] == 1)
- {
- search(x, y+1, n);
- ::count++;
- }
- if (labyrinth[x][y-1] == 1)
- {
- search(x, y-1, n);
- ::count++;
- }
- labyrinth[x][y] = 1;
- c--;
- return way;
- }
- int main()
- {
- int n;
- cin >> n;
- labgen(n);
- preprintlab(n);
- cout << "'walled' labyrinth"<<endl;
- printlab(n+2);
- ::count = 1;
- int way=search(1, 1, n);
- if (way >=1)
- cout << "There is " << way << " way(s)" << endl;
- else cout << "There is no way out" << endl;
- cout << ::count << " Count of reccursions fuctions called";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement