You are viewing a single comment's thread. Return to all comments →
Her My javaScript Solution
function countLuck(matrix, k) { let i, j; let possibleWay = [], count = 0; for (let index = 0; index < matrix.length; index++) { if (matrix[index].includes("M")) i = index; matrix[index] = matrix[index].toString().split(""); } j = matrix[i].indexOf("M"); const arrOfWays = (i, j, mark) => { let arr = []; if (matrix[i][j + 1]) matrix[i][j + 1] == mark ? arr.push([i, j + 1]) : matrix[i][j + 1] == "*" ? arr.push("*") : arr; if (matrix[i][j - 1]) matrix[i][j - 1] == mark ? arr.push([i, j - 1]) : matrix[i][j - 1] == "*" ? arr.push("*") : arr; if (i < matrix.length - 1) if (matrix[i + 1][j]) matrix[i + 1][j] == mark ? arr.push([i + 1, j]) : matrix[i + 1][j] == "*" ? arr.push("*") : arr; if (i > 0) if (matrix[i - 1][j]) matrix[i - 1][j] == mark ? arr.push([i - 1, j]) : matrix[i - 1][j] == "*" ? arr.push("*") : arr; return arr; }; possibleWay.push([i, j]); let ind = 0; while (ind < possibleWay.length && !possibleWay.includes("*")) { let i = possibleWay[ind][0], j = possibleWay[ind][1]; let arr = arrOfWays(i, j, "."); if (arr.length != 0) { arr.length == 1 ? (matrix[i][j] = "0") : (matrix[i][j] = "1") & count++; possibleWay = [ ...possibleWay.slice(0, ind + 1), ...arr, ...possibleWay.slice(ind + 1), ]; } else { matrix[i][j] = "0"; let currInd = ind; let x = possibleWay[currInd][0], y = possibleWay[currInd][1]; while ( matrix[x][y] == "0" && arrOfWays(x, y, "0").length + arrOfWays(x, y, "1").length == 1 ) { matrix[x][y] = "W"; currInd--; x = possibleWay[currInd][0]; y = possibleWay[currInd][1]; if (matrix[x][y] == "W") while (matrix[x][y] == "W") { currInd--; x = possibleWay[currInd][0]; y = possibleWay[currInd][1]; } if (matrix[x][y] == "1" && arrOfWays(x, y, ".").length == 0) { matrix[x][y] = "0"; count--; } } } ind++; } if (count != k) return "Oops!"; return "Impressed"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Count Luck
You are viewing a single comment's thread. Return to all comments →
Her My javaScript Solution