Ad
Algorithm To Determine Correct Character ['╦', '╣', '╠', '╩', '╬'] For Intersections In Grid
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
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad