Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef struct point {
- int x, y;
- } point;
- typedef struct neighbours {
- int n;
- point elems[4];
- } neighbours;
- // x - vertical axis, y - horizontal axis WTF
- neighbours get_neighbours(int** labyrinth, int n, int m, point* p) {
- neighbours neighs;
- neighs.n = 0;
- if(p->x>0 && labyrinth[p->x-1][p->y] == 1) {
- point pnt = {p->x-1, p->y};
- neighs.elems[neighs.n++] = pnt;
- }
- if(p->x<n-1 && labyrinth[p->x+1][p->y] == 1) {
- point pnt = {p->x+1, p->y};
- neighs.elems[neighs.n++] = pnt;
- }
- if(p->y>0 && labyrinth[p->x][p->y-1] == 1) {
- point pnt = {p->x, p->y-1};
- neighs.elems[neighs.n++] = pnt;
- }
- if(p->y<m-1 && labyrinth[p->x][p->y+1] == 1) {
- point pnt = {p->x, p->y+1};
- neighs.elems[neighs.n++] = pnt;
- }
- return neighs;
- }
- // n - rows, m - columns
- bool dfs(int** graph, int n, int m, bool** visited, point p, point end) {
- if(p.x == end.x && p.y == end.y) return true;
- visited[p.x][p.y] = true;
- neighbours neighs = get_neighbours(graph, n, m, &p);
- for(int i=0;i<neighs.n;i++) {
- point p1 = neighs.elems[i];
- if(!visited[p1.x][p1.y] && dfs(graph, n, m, visited, p1, end)) return true;
- }
- return false;
- }
- int main() {
- int n, m;
- cin >> n;
- cin >> m;
- int **labyrinth = new int*[n];
- for(int i=0; i<n; i++) labyrinth[i] = new int[m];
- bool** visited = new bool*[n];
- for(int i=0; i<n; i++) {
- visited[i] = new bool[m];
- for(int j=0; j<m; j++) visited[i][j] = false;
- }
- for(int i=0; i<n; i++) {
- for(int j=0; j<m; j++) {
- cin >> labyrinth[i][j];
- }
- }
- point start, end;
- cin >> start.x;
- cin >> start.y;
- cin >> end.x;
- cin >> end.y;
- cout << (dfs(labyrinth, n, m, visited, start, end) ? "TAK" : "NIE") << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement