Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Created December 22, 2011 00:24
Show Gist options
  • Select an option

  • Save freddiefujiwara/1508350 to your computer and use it in GitHub Desktop.

Select an option

Save freddiefujiwara/1508350 to your computer and use it in GitHub Desktop.
fields = "#S######.#";
var width = fields.length;
fields += "......#..#";
fields += ".#.##.##.#";
fields += ".#........";
fields += "##.##.####";
fields += "....#....#";
fields += ".#######.#";
fields += "....#.....";
fields += ".####.###.";
fields += "....#...G#";
var answers = [];
var walk = function(i,len,fields){
fields[i] = "*";
$.each([i-width,i-1,i+1,i+width],function(n,m){
if(0 === (i % width) && 1 === n ){return}; //right-end
if((width - 1) === (i % width) && 2 === n ){return}; //left-lend
if("."===fields[m]){walk(m,len+1,fields.slice(0))};
if("G"===fields[m]){answers.push(len+1)};
});
};
walk(1,0,fields.split(new RegExp("")));
console.log(Math.min.apply(null, answers));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment