+ 1
Random array generator?
Would anyone please write a function that generates a random N by N array of 1 and 0 in such a way that it resembles a path represented by 1? Here's an example of such array: [ [0,1,0,0,0,1], [1,1,0,0,0,1], [1,0,0,1,1,1], [1,1,0,1,0,0], [0,1,0,1,0,0], [0,1,1,1,0,0] ] I would really appreciate it if anybody would write such function in JS, Python, Java or C++. Thank you.
6 odpowiedzi
+ 3
What have you tried so far?
+ 2
I think this is what you need C++:
https://code.sololearn.com/cffOzL8291Uc/?ref=app
Hope this will help you 😊
+ 1
Denise Roßberg this
https://code.sololearn.com/WAeB2Bqzd7dP/?ref=app but it only makes S-shaped pattern sadly.
+ 1
Fernando Moceces
You can have a look on my code.
https://code.sololearn.com/cv99P67rf5ve/?ref=app
It is not perfect because I don't check for neighbours. I guess to get a "clear" path each 1 should have max 2 neighbours.
I am not sure if it would work when you just create conditions for the directions.
* start: random
* up/down/left/right: when no 1 occurs, when it is not out of range, when the new 1 has only the old 1 as neighbour
* break loop when no move is possible
+ 1
$p@rK very nice but the path needs to be a single continuous path
0
This looks good.
I would say now add a second loop to get from bottom to top.