Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Customer {
- constructor() {
- this.x = width / 2;
- this.y = height - 1;
- }
- goShortest(_x, _y) {
- var q = new Queue();
- q.enqueue({x: this.x, y: this.y});
- var used = [];
- var p = [];
- for (var x = 0; x < width; ++x) {
- used[x] = [];
- p[x] = [];
- for (var y = 0; y < height; ++y) {
- used[x][y] = false;
- p[x][y] = {x: -1, y: -1};
- }
- }
- used[this.x][this.y] = true;
- while (!q.isEmpty()) {
- var u = q.dequeue();
- if (u.x > 0 && !used[u.x - 1][u.y] && get(u.x - 1, u.y)[0] == 0) {
- used[u.x - 1][u.y] = true;
- p[u.x - 1][u.y] = u;
- q.enqueue({x: u.x - 1, y: u.y});
- }
- if (u.y > 0 && !used[u.x][u.y - 1] && get(u.x, u.y - 1)[0] == 0) {
- used[u.x][u.y - 1] = true;
- p[u.x][u.y - 1] = u;
- q.enqueue({x: u.x, y: u.y - 1});
- }
- if (u.x < width - 1 && !used[u.x + 1][u.y] && get(u.x + 1, u.y)[0] == 0) {
- used[u.x + 1][u.y] = true;
- p[u.x + 1][u.y] = u;
- q.enqueue({x: u.x + 1, y: u.y});
- }
- if (u.y < height - 1 && !used[u.x][u.y + 1] && get(u.x, u.y + 1)[0] == 0) {
- used[u.x][u.y + 1] = true;
- p[u.x][u.y + 1] = u;
- q.enqueue({x: u.x, y: u.y + 1});
- }
- if (u.x > 0 && u.y > 0 && !used[u.x - 1][u.y - 1] && get(u.x - 1, u.y - 1)[0] == 0) {
- used[u.x - 1][u.y - 1] = true;
- p[u.x - 1][u.y - 1] = u;
- q.enqueue({x: u.x - 1, y: u.y - 1});
- }
- if (u.x > 0 && u.y < height - 1 && !used[u.x - 1][u.y + 1] && get(u.x - 1, u.y + 1)[0] == 0) {
- used[u.x - 1][u.y + 1] = true;
- p[u.x - 1][u.y + 1] = u;
- q.enqueue({x: u.x - 1, y: u.y + 1});
- }
- if (u.x < width - 1 && u.y > 0 && !used[u.x + 1][u.y - 1] && get(u.x + 1, u.y - 1)[0] == 0) {
- used[u.x + 1][u.y - 1] = true;
- p[u.x + 1][u.y - 1] = u;
- q.enqueue({x: u.x + 1, y: u.y - 1});
- }
- 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) {
- used[u.x + 1][u.y + 1] = true;
- p[u.x + 1][u.y + 1] = u;
- q.enqueue({x: u.x + 1, y: u.y + 1});
- }
- }
- var path = [];
- for (var u = {x: _x, y: _y}; u.x != -1; u = p[u.x][u.y]) {
- path.push(u);
- }
- reverse(path);
- console.log(path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement