Created
March 16, 2018 01:58
-
-
Save SoarLin/43d59ac93a60cbf6c8e8d1dc4fb55895 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/wokiqalimu
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var MAP = [ | |
["0","0","x","x","x","0","0","x","x"], | |
["0","0","0","x","x","0","0","x","0"], | |
["0","x","0","x","0","x","0","0","0"], | |
["0","x","0","0","0","x","x","0","0"] | |
]; | |
var island = 0; | |
var Width = MAP[0].length, Height = MAP.length; | |
function printMap() { | |
for (var i=0; i < Height; i++) { | |
console.log(MAP[i]); | |
} | |
console.log("================================================="); | |
} | |
function markIslandInMap(i, j) { | |
if (MAP[i][j] === "x") { | |
MAP[i][j] = "M"; | |
printMap(); | |
} | |
if (i+1 < Height && MAP[i+1][j] === "x") { | |
markIslandInMap(i+1, j); | |
} | |
if (i-1 > 0 && MAP[i-1][j] === "x") { | |
markIslandInMap(i-1, j); | |
} | |
if (j+1 < Width && MAP[i][j+1] === "x") { | |
markIslandInMap(i, j+1); | |
} | |
if (j-1 > 0 && MAP[i][j-1] === "x") { | |
markIslandInMap(i, j-1); | |
} | |
return; | |
} | |
for (var i=0; i < Height; i++) { | |
for(var j=0; j < Width; j++) { | |
if (MAP[i][j] === "x") { | |
markIslandInMap(i,j); | |
island++; | |
} else { | |
continue; | |
} | |
} | |
} | |
console.log(island); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var MAP = [ | |
["0","0","x","x","x","0","0","x","x"], | |
["0","0","0","x","x","0","0","x","0"], | |
["0","x","0","x","0","x","0","0","0"], | |
["0","x","0","0","0","x","x","0","0"] | |
]; | |
var island = 0; | |
var Width = MAP[0].length, Height = MAP.length; | |
function printMap() { | |
for (var i=0; i < Height; i++) { | |
console.log(MAP[i]); | |
} | |
console.log("================================================="); | |
} | |
function markIslandInMap(i, j) { | |
if (MAP[i][j] === "x") { | |
MAP[i][j] = "M"; | |
printMap(); | |
} | |
if (i+1 < Height && MAP[i+1][j] === "x") { | |
markIslandInMap(i+1, j); | |
} | |
if (i-1 > 0 && MAP[i-1][j] === "x") { | |
markIslandInMap(i-1, j); | |
} | |
if (j+1 < Width && MAP[i][j+1] === "x") { | |
markIslandInMap(i, j+1); | |
} | |
if (j-1 > 0 && MAP[i][j-1] === "x") { | |
markIslandInMap(i, j-1); | |
} | |
return; | |
} | |
for (var i=0; i < Height; i++) { | |
for(var j=0; j < Width; j++) { | |
if (MAP[i][j] === "x") { | |
markIslandInMap(i,j); | |
island++; | |
} else { | |
continue; | |
} | |
} | |
} | |
console.log(island);</script></body> | |
</html> |
This file contains hidden or 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
var MAP = [ | |
["0","0","x","x","x","0","0","x","x"], | |
["0","0","0","x","x","0","0","x","0"], | |
["0","x","0","x","0","x","0","0","0"], | |
["0","x","0","0","0","x","x","0","0"] | |
]; | |
var island = 0; | |
var Width = MAP[0].length, Height = MAP.length; | |
function printMap() { | |
for (var i=0; i < Height; i++) { | |
console.log(MAP[i]); | |
} | |
console.log("================================================="); | |
} | |
function markIslandInMap(i, j) { | |
if (MAP[i][j] === "x") { | |
MAP[i][j] = "M"; | |
printMap(); | |
} | |
if (i+1 < Height && MAP[i+1][j] === "x") { | |
markIslandInMap(i+1, j); | |
} | |
if (i-1 > 0 && MAP[i-1][j] === "x") { | |
markIslandInMap(i-1, j); | |
} | |
if (j+1 < Width && MAP[i][j+1] === "x") { | |
markIslandInMap(i, j+1); | |
} | |
if (j-1 > 0 && MAP[i][j-1] === "x") { | |
markIslandInMap(i, j-1); | |
} | |
return; | |
} | |
for (var i=0; i < Height; i++) { | |
for(var j=0; j < Width; j++) { | |
if (MAP[i][j] === "x") { | |
markIslandInMap(i,j); | |
island++; | |
} else { | |
continue; | |
} | |
} | |
} | |
console.log(island); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment