Advertisement
tomalikem

LAB

Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef struct point {
  5.     int x, y;
  6. } point;
  7.  
  8. typedef struct neighbours {
  9.     int n;
  10.     point elems[4];
  11. } neighbours;
  12.  
  13. // x - vertical axis, y - horizontal axis WTF
  14. neighbours get_neighbours(int** labyrinth, int n, int m, point* p) {
  15.     neighbours neighs;
  16.     neighs.n = 0;
  17.     if(p->x>0 && labyrinth[p->x-1][p->y] == 1) {
  18.         point pnt = {p->x-1, p->y};
  19.         neighs.elems[neighs.n++] = pnt;
  20.     }
  21.     if(p->x<n-1 && labyrinth[p->x+1][p->y] == 1) {
  22.         point pnt = {p->x+1, p->y};
  23.         neighs.elems[neighs.n++] = pnt;
  24.     }
  25.     if(p->y>0 && labyrinth[p->x][p->y-1] == 1) {
  26.         point pnt = {p->x, p->y-1};
  27.         neighs.elems[neighs.n++] = pnt;
  28.     }
  29.     if(p->y<m-1 && labyrinth[p->x][p->y+1] == 1) {
  30.         point pnt = {p->x, p->y+1};
  31.         neighs.elems[neighs.n++] = pnt;
  32.     }
  33.     return neighs;
  34. }
  35.  
  36. // n - rows, m - columns
  37. bool dfs(int** graph, int n, int m, bool** visited, point p, point end) {
  38.     if(p.x == end.x && p.y == end.y) return true;
  39.     visited[p.x][p.y] = true;
  40.    
  41.     neighbours neighs = get_neighbours(graph, n, m, &p);
  42.     for(int i=0;i<neighs.n;i++) {
  43.         point p1 = neighs.elems[i];
  44.         if(!visited[p1.x][p1.y] && dfs(graph, n, m, visited, p1, end)) return true;
  45.     }
  46.     return false;
  47. }
  48.  
  49.  
  50. int main() {
  51.     int n, m;
  52.     cin >> n;
  53.     cin >> m;
  54.     int **labyrinth = new int*[n];
  55.     for(int i=0; i<n; i++) labyrinth[i] = new int[m];
  56.     bool** visited = new bool*[n];
  57.     for(int i=0; i<n; i++) {
  58.         visited[i] = new bool[m];
  59.         for(int j=0; j<m; j++) visited[i][j] = false;
  60.     }
  61.    
  62.     for(int i=0; i<n; i++) {
  63.         for(int j=0; j<m; j++) {
  64.             cin >> labyrinth[i][j];
  65.         }
  66.     }
  67.     point start, end;
  68.     cin >> start.x;
  69.     cin >> start.y;
  70.     cin >> end.x;
  71.     cin >> end.y;
  72.     cout << (dfs(labyrinth, n, m, visited, start, end) ? "TAK" : "NIE") << endl;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement