From 2fd3de0a678b110853401f9257c63b7fda07a007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Ku=C4=87mierz?= Date: Thu, 21 Sep 2017 02:44:28 +0200 Subject: [PATCH] quickfix: callstack limit reached error --- path-finder-number-1-can-you-reach-the-exit/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/path-finder-number-1-can-you-reach-the-exit/index.js b/path-finder-number-1-can-you-reach-the-exit/index.js index f5eb235..bac2de0 100644 --- a/path-finder-number-1-can-you-reach-the-exit/index.js +++ b/path-finder-number-1-can-you-reach-the-exit/index.js @@ -4,6 +4,7 @@ function pathFinder(maze) { let m = maze.split(/\n/).map(row=>row.split``); let h = m.length; let w = m[0].length; + let queue = [[0, 0]]; let mark = (xx, yy) => { [[1, 0], [-1, 0], [0, 1], [0, -1], [0, 0]].map(p => { let [x, y] = [p[0]+xx, p[1]+yy]; @@ -11,12 +12,17 @@ function pathFinder(maze) { if (0 <= y && y < h) { if (m[y][x] === '.') { m[y][x] = '#'; - mark(x, y); + queue.push([x, y]); } } } }); }; - mark(0, 0); + + do { + let p = queue.shift(); + mark(...p); + } while (queue.length); + return m[h-1][w-1] !== '.'; }