Created
September 8, 2016 22:37
-
-
Save anonymous/f4e27d8b1a9da3734e9b50b57184f82c to your computer and use it in GitHub Desktop.
https://repl.it/DD97/19 created by amosgwa
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
//Create Nodes class | |
function node(data,child){ | |
this.data = data; | |
//Array of nodes | |
this.child = child; | |
this.visited = false; | |
} | |
//print the child | |
node.prototype.print = function(){ | |
var result = this.data+" : "; | |
for(var i in this.child){ | |
result += this.child[i].data+" "; | |
} | |
console.log(result); | |
} | |
//Inputs | |
//Input boggle matrix | |
var boggle = [['G','I','Z'], | |
['U','E','K'], | |
['Q','S','E']]; | |
//Dictionary | |
var dictionary = new Set(); | |
dictionary.add('GEEKS'); | |
dictionary.add('QUIZ'); | |
dictionary.forEach(function(v){console.log(v);}); | |
//Create each cell with node | |
var boggleNode = []; | |
for(var i in boggle){ | |
boggleNode[i] = []; | |
for(var j in boggle[i]){ | |
boggleNode[i].push(new node(boggle[i][j],[])); | |
} | |
} | |
//Find adjacent nodes | |
for(var i in boggle) { | |
for(var j in boggleNode[i]){ | |
//find the adjacent | |
var tmp = []; | |
for(var y = i-1; y <= i+1 && y < boggleNode.length; y++){ | |
for(var x = j-1; x <= j+1 && x < boggleNode[i].length; x++){ | |
if(y >= 0 && x >= 0 && (y != i || x != j)){ | |
tmp.push(boggleNode[y][x]); | |
} | |
} | |
} | |
boggleNode[i][j].child = boggleNode[i][j].child.concat(tmp); | |
} | |
} | |
printMatrix(boggleNode); | |
printMatrixNode(boggleNode); | |
//DFS | |
function dfs(currentNode, str){ | |
str += currentNode.data; | |
currentNode.visited = true; | |
if(dictionary.has(str)){ | |
console.log(str); | |
} | |
for(var i in currentNode.child){ | |
if(!currentNode.child[i].visited){ | |
dfs(currentNode.child[i], str); | |
} | |
} | |
str = str.substring(0,str.length-1); | |
currentNode.visited = false; | |
} | |
for(var i in boggleNode){ | |
for(var j in boggleNode[i]){ | |
dfs(boggleNode[i][j],'',[]); | |
boggleNode[i][j].visited = false; | |
} | |
} | |
//Helper function | |
//Pring the matrix | |
function printMatrix(input){ | |
console.log(); | |
for(var i in input) { | |
var tmp = ""; | |
for(var j in input) { | |
tmp += input[i][j].data+" "; | |
} | |
console.log(tmp); | |
} | |
console.log(); | |
} | |
function printMatrixNode(input){ | |
console.log(); | |
for(var i in input) { | |
for(var j in input) { | |
input[i][j].print(); | |
} | |
} | |
console.log(); | |
} |
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
Native Browser JavaScript | |
>>> GEEKS | |
QUIZ | |
G I Z | |
U E K | |
Q S E | |
G : I U E | |
I : G Z U E K | |
Z : I E K | |
U : G I E Q S | |
E : G I Z U K Q S E | |
K : I Z E S E | |
Q : U E S | |
S : U E K Q E | |
E : E K S | |
GEEKS | |
QUIZ | |
=> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment