Advertisement
pdaogu

MAZE

Sep 29th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <queue>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAX 910
  6.  
  7. using namespace std;
  8.  
  9. int n, m, x0, y0;
  10. int result;
  11. typedef struct {
  12.     int x;
  13.     int y;
  14. } POINT;
  15. queue<POINT> q;     // queue for cell
  16.  
  17. bool maze[MAX][MAX];
  18. bool visited[MAX][MAX];         // visited[i][j] = 1 -> cell (i, j) visited
  19. int level[MAX][MAX];        // level[i][j] shortest distance from state (0, 0) to (i, j)
  20.  
  21. void output () {
  22.     printf("%d\n", result);
  23. }
  24.  
  25. void dfs () {
  26.    
  27.     while (!q.empty()) {
  28.         POINT cur = q.front();
  29.         q.pop();
  30.         int xnextf = cur.x + 1;
  31.         int ynextf = cur.y + 1;
  32.         int xnextb = cur.x - 1;
  33.         int ynextb = cur.y - 1;
  34.         if (xnextf > n || xnextb < 1 || ynextf > m || ynextb < 1) {
  35.             result = level[cur.x][cur.y] + 1;
  36.             return;
  37.         }
  38.         POINT tmp;
  39.         if (!maze[xnextf][cur.y] && !visited[xnextf][cur.y]) {
  40.             tmp.x = xnextf;
  41.             tmp.y = cur.y;
  42.             q.push(tmp);
  43.             level[xnextf][cur.y] = level[cur.x][cur.y] + 1;
  44.             visited[tmp.x][tmp.y] = 1;
  45.         }
  46.         if (!maze[xnextb][cur.y] && !visited[xnextb][cur.y]) {
  47.             tmp.x = xnextb;
  48.             tmp.y = cur.y;
  49.             q.push(tmp);
  50.             level[xnextb][cur.y] = level[cur.x][cur.y] + 1;
  51.             visited[tmp.x][tmp.y] = 1;
  52.         }
  53.         if (!maze[cur.x][ynextf] && !visited[cur.x][ynextf]) {
  54.             tmp.x = cur.x;
  55.             tmp.y = ynextf;
  56.             q.push(tmp);
  57.             level[cur.x][ynextf] = level[cur.x][cur.y] + 1;
  58.             visited[tmp.x][tmp.y] = 1;
  59.         }
  60.         if (!maze[cur.x][ynextb] && !visited[cur.x][ynextb]) {
  61.             tmp.x = cur.x;
  62.             tmp.y = ynextb;
  63.             q.push(tmp);
  64.             level[cur.x][ynextb] = level[cur.x][cur.y] + 1;
  65.             visited[tmp.x][tmp.y] = 1;
  66.         }
  67.     }
  68.     result = -1;
  69. }
  70.  
  71. void solve () {
  72.     POINT tmp = {x0, y0};
  73.     q.push(tmp);
  74.     level[x0][y0] = 0;
  75.     visited[x0][y0] = 1;
  76.     dfs();
  77. }
  78.  
  79. void input () {
  80.     scanf("%d%d%d%d", &n, &m, &x0, &y0);
  81.     for (int i = 1; i <= n; ++i) {
  82.         for (int j = 1; j <= m; ++j)
  83.             scanf("%d", &maze[i][j]);
  84.     }
  85.     memset(visited, 0, sizeof(visited));
  86.     memset(level, 0, sizeof(level));
  87. }
  88.  
  89. int main () {
  90.     // freopen("inp", "r", stdin);
  91.     // freopen("out", "w", stdout);
  92.     input();
  93.     solve();
  94.     output();
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement