Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Customer {
  2.     constructor() {
  3.         this.x = width / 2;
  4.         this.y = height - 1;
  5.     }
  6.  
  7.     goShortest(_x, _y) {
  8.  
  9.         var q = new Queue();
  10.         q.enqueue({x: this.x, y: this.y});
  11.  
  12.         var used = [];
  13.         var p = [];
  14.         for (var x = 0; x < width; ++x) {
  15.             used[x] = [];
  16.             p[x] = [];
  17.             for (var y = 0; y < height; ++y) {
  18.                 used[x][y] = false;
  19.                 p[x][y] = {x: -1, y: -1};
  20.             }
  21.         }
  22.  
  23.         used[this.x][this.y] = true;
  24.  
  25.         while (!q.isEmpty()) {
  26.             var u = q.dequeue();
  27.  
  28.             if (u.x > 0 && !used[u.x - 1][u.y] && get(u.x - 1, u.y)[0] == 0) {
  29.                 used[u.x - 1][u.y] = true;
  30.                 p[u.x - 1][u.y] = u;
  31.                 q.enqueue({x: u.x - 1, y: u.y});
  32.             }
  33.  
  34.             if (u.y > 0 && !used[u.x][u.y - 1] && get(u.x, u.y - 1)[0] == 0) {
  35.                 used[u.x][u.y - 1] = true;
  36.                 p[u.x][u.y - 1] = u;
  37.                 q.enqueue({x: u.x, y: u.y - 1});
  38.             }
  39.  
  40.             if (u.x < width - 1 && !used[u.x + 1][u.y] && get(u.x + 1, u.y)[0] == 0) {
  41.                 used[u.x + 1][u.y] = true;
  42.                 p[u.x + 1][u.y] = u;
  43.                 q.enqueue({x: u.x + 1, y: u.y});
  44.             }
  45.  
  46.             if (u.y < height - 1 && !used[u.x][u.y + 1] && get(u.x, u.y + 1)[0] == 0) {
  47.                 used[u.x][u.y + 1] = true;
  48.                 p[u.x][u.y + 1] = u;
  49.                 q.enqueue({x: u.x, y: u.y + 1});
  50.             }
  51.  
  52.             if (u.x > 0 && u.y > 0 && !used[u.x - 1][u.y - 1] && get(u.x - 1, u.y - 1)[0] == 0) {
  53.                 used[u.x - 1][u.y - 1] = true;
  54.                 p[u.x - 1][u.y - 1] = u;
  55.                 q.enqueue({x: u.x - 1, y: u.y - 1});
  56.             }
  57.  
  58.             if (u.x > 0 && u.y < height - 1 && !used[u.x - 1][u.y + 1] && get(u.x - 1, u.y + 1)[0] == 0) {
  59.                 used[u.x - 1][u.y + 1] = true;
  60.                 p[u.x - 1][u.y + 1] = u;
  61.                 q.enqueue({x: u.x - 1, y: u.y + 1});
  62.             }
  63.  
  64.             if (u.x < width - 1 && u.y > 0 && !used[u.x + 1][u.y - 1] && get(u.x + 1, u.y - 1)[0] == 0) {
  65.                 used[u.x + 1][u.y - 1] = true;
  66.                 p[u.x + 1][u.y - 1] = u;
  67.                 q.enqueue({x: u.x + 1, y: u.y - 1});
  68.             }
  69.  
  70.             if (u.x < width - 1 && u.y < height - 1 && !used[u.x + 1][u.y + 1] && get(u.x + 1, u.y + 1)[0] == 0) {
  71.                 used[u.x + 1][u.y + 1] = true;
  72.                 p[u.x + 1][u.y + 1] = u;
  73.                 q.enqueue({x: u.x + 1, y: u.y + 1});
  74.             }
  75.         }
  76.  
  77.         var path = [];
  78.         for (var u = {x: _x, y: _y}; u.x != -1; u = p[u.x][u.y]) {
  79.             path.push(u);
  80.         }
  81.         reverse(path);
  82.  
  83.         console.log(path);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement