Last active
March 21, 2021 20:17
-
-
Save petr999/9abb71d53d5693c678be6806bceeb4ec to your computer and use it in GitHub Desktop.
matrix path walkthrough: 0's are no, 1's are go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ABSTRACT: find route or single short diagonal route | |
// Returns 2d-array filled with falses | |
function getArrOfFalses( arr ){ | |
let rv = [] | |
arr.forEach( ( row, i ) => { | |
let rowRv = [] | |
row.forEach( ( col, j ) => { | |
rowRv.push( false ) | |
} ) | |
rv.push( rowRv ) | |
} ) | |
return rv | |
} | |
// Returns bool if cell exists on a matrix | |
function isSafe( arr, i, j ){ | |
const rowsNum = arr.length | |
const colsNum = arr[ rowsNum - 1 ].length | |
const rv = ( ( ( i >= 0 ) && ( i < rowsNum ) ) | |
&& ( ( j >= 0 ) && ( j < colsNum ) ) | |
) | |
return rv | |
} | |
// Returns bool, changes cells arg | |
function isPath( arr, i, j, visited, cells ){ | |
let rv = false | |
if( isSafe( arr, i, j ) | |
&& ! visited[ i ][ j ] | |
){ | |
// console.log( i, j, visited, visited[ i, j ] ) | |
visited[ i ][ j ] = true | |
const value = arr[ i ][ j ] | |
if( 3 == value ){ | |
rv = true | |
} else if( ( 1 == value ) || ( 2 == value ) ) { | |
cells[ i ][ j ] = true | |
let rvStep | |
rvStep = isPath( arr, i - 1, j, visited, cells ) | |
if( rvStep ){ return true } | |
rvStep = isPath( arr, i + 1, j, visited, cells ) | |
if( rvStep ){ return true } | |
rvStep = isPath( arr, i, j - 1, visited, cells ) | |
if( rvStep ){ return true } | |
rvStep = isPath( arr, i, j + 1, visited, cells ) | |
if( rvStep ){ return true } | |
} | |
} | |
return rv | |
} | |
// Takes array[], returns [ bool, array[] ] | |
function atPath( arr ){ | |
let rv = false | |
let [ visited, cells, ] = [ getArrOfFalses( arr ), getArrOfFalses( arr ), ] | |
arr.forEach( ( row, i ) => { | |
row.forEach( ( col, j ) => { | |
if( 2 == col ){ | |
rv = isPath( arr, i, j, visited, cells ) | |
} | |
} ) | |
} ) | |
return [ rv, cells, ] | |
} | |
// Takes array[], returns bool | Int | |
function getPath( arr ){ | |
let rv = false | |
let flag, cellsFrom, cellsTo, atPathRv | |
const rowLastNum = arr.length - 1 | |
const colLastNum = arr[ rowLastNum ].length - 1 | |
arr[ 0 ][ 0 ] = 2 | |
arr[ rowLastNum][ colLastNum ] = 3 | |
atPathRv = atPath( arr ) | |
flag = atPathRv[0] | |
cellsFrom = atPathRv[1] | |
if( flag ){ return true } | |
arr[ 0 ][ 0 ] = 1 | |
arr[ rowLastNum][ colLastNum ] = 1 | |
arr[ 0 ][ 0 ] = 3 | |
arr[ rowLastNum ][ colLastNum ] = 2 | |
atPathRv = atPath( arr ) | |
cellsTo = atPathRv[1] | |
arr[ 0 ][ 0 ] = 1 | |
arr[ rowLastNum][ colLastNum ] = 1 | |
if ( flag ){ | |
rv = flag | |
} else { | |
rv = findShortDiags( cellsFrom, cellsTo ) | |
} | |
return rv; | |
} | |
function findShortDiags( cellsFrom, cellsTo ){ | |
let rv = 0 | |
let[ cellsFromPairs, cellsToPairs ] = [ [], [], ] | |
cellsFrom.forEach( ( row, i ) => { | |
row.forEach( ( col, j ) => { | |
if( col ){ | |
cellsFromPairs.push( [ i, j, ] ) | |
} | |
} ) | |
} ) | |
cellsTo.forEach( ( row, i ) => { | |
row.forEach( ( col, j ) => { | |
if( col ){ | |
cellsToPairs.push( [ i, j, ] ) | |
} | |
} ) | |
} ) | |
cellsFromPairs.forEach( cellFrom => { | |
const [ iFrom, jFrom, ] = cellFrom | |
cellsToPairs.forEach( cellTo => { | |
const [ iTo, jTo, ] = cellTo | |
const [ iDiff, jDiff, ] = [ iFrom - iTo, jFrom - jTo, ].map( Math.abs ) | |
if( ( 1 == iDiff ) && ( 1 == jDiff ) ){ | |
rv += 2 | |
} | |
if( | |
( ( 2 == iDiff ) && ( 0 == jDiff ) ) | |
|| | |
( ( 0 == iDiff ) && ( 2 == jDiff ) ) | |
){ | |
rv ++ | |
} | |
} ) | |
} ) | |
if( 0 == rv ){ rv = false } | |
return rv | |
} | |
// Takes string[], returns bool | Int | |
function matrixPath( strArr ){ | |
const arr = strArr.map( elem => elem.split( `` ) ) | |
const rv = getPath( arr ) | |
return rv | |
} | |
let rv | |
rv = matrixPath( ["11100", "10011", "10101", "10011"] ) | |
console.log( rv ) | |
rv = matrixPath( ["11100", "10111", "10101", "10011"] ) | |
console.log( rv ) | |
rv = matrixPath( ["11100", "10011", "10111", "10011"] ) | |
console.log( rv ) | |
rv = matrixPath( ["11000", "11011", "10101", "10011"] ) | |
console.log( rv ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment