Ad

Algorithm To Determine Correct Character ['╦', '╣', '╠', '╩', '╬'] For Intersections In Grid

- 1 answer

I cannot even begin to wrap my head around this.

Given an array of binary values with 0 corresponding to open space and 1 being a wall:

11111111111111111111
10001000000000000001
10101111111010101111
10101111111010100001
10101111111010111101
10101111111010000001
10100000000010111111
11111111111111111111

How could you write an algorithm to change it to something like this:

╔═══╦══════════════╗
║   ║              ║
║ ║ ╠╦╦╦╦╦╗ ║ ║ ═══╣
║ ║ ╠╬╬╬╬╬╣ ║ ║    ║
║ ║ ╠╬╬╬╬╬╣ ║ ╚═══ ║
║ ║ ╚╩╩╩╩╩╝ ║      ║
║ ║         ║ ╔╦╦╦╦╣
╚═╩═════════╩═╩╩╩╩╩╝

╔═══╦══════════════╗
║   ║              ║
║ ║ ╠═════╗ ║ ║ ═══╣
║ ║ ║     ║ ║ ║    ║
║ ║ ║     ║ ║ ╚═══ ║
║ ║ ╚═════╝ ║      ║
║ ║         ║ ╔════╝
╚═╩═════════╩═╝

I would really appreciate any kind of guidance for this problem. Javascript would be preferred but anything would help!

Ad

Answer

This will do the job.

function toMap(str){
    var chars = " ║═╚║║╔╠═╝═╩╗╣╦╬";
    var arr = str.split("\n");
    var v = (x,y,s)=>(y >= 0 && x >= 0 && y < arr.length && arr[y].charAt(x)==="1") << (s|0);
    return arr.map((r,y)=>r.split("").map((c,x)=>chars.charAt(v(x,y)&&(v(x,y-1)|v(x+1,y,1)|v(x,y+1,2)|v(x-1,y,3)))).join("")).join("\n")
}

toMap(`11111111111111111111
10001000000000000001
10101111111010100111
10101011111010100001
10101110111010111101
10101111111010000001
10100000000010111111
111111111111111`)

To understand the whole code, I recommend that you inflate the code and start debugging it and annotating it with comments.

Ad
source: stackoverflow.com
Ad