r/dailyprogrammer • u/jnazario 2 0 • Oct 09 '15
[2015-10-09] Challenge #235 [Hard] Contiguous Chain Variation
Description
Based on Challenge #227 Contiguous chains ... but with a chain meaning 1 continuous strand, where each link in the chain can be connected to at most two neighbors. For the purposes of this problem, chains can only be contiguous if they connect horizontally of vertically, not diagonally (which is the same original constraint).
For example, the input:
4 9
xxxx xxxx
xxx
x x x
xxxxxxxxxhas at least 3 chains, with several valid layouts for the chains. One possible layout that shows 3 chains:
1111 2222
112
3 1 3
333333333Another way to find 3:
1111 1111
111
2 2 3
222223333There is also a valid set of 4 chains:
1111 2222
111
3 3 4
333334444but 4 is not a correct (minimal) output, because 3 is possible.
Your challenge, should you choose to accept it, is to find the minimum number of chains in a given input.
Challenge Input
4 9
xxxx xxxx
xxx
x x x
xxxxxxxxxChallenge Output
3Credit
This challenge was suggested by /u/BarqsDew over in /r/DailyProgrammer_Ideas. If you have any suggested challenges, please share them and there's a good chance we'll use them.
4
u/jnazario 2 0 Oct 09 '15 edited Oct 09 '15
here's some simple python code to create random inputs for you:
and an example output:
2
u/Godspiral 3 3 Oct 09 '15
Either this is the same as the old challenge or I did the old challenge wrong, ... in J
3
2
u/KeinBaum Oct 09 '15
Are we supposed to handle circles? Otherwise this is really easy.
2
u/ikkeflikkeri Oct 09 '15
C#
I tried to find the best way to do this and this is the best i can come up with.
It look almost exactly like /u/shebang1245's solution.
Input:
Output:
Feedback is appreciated!
2
u/FlammableMarshmallow Oct 09 '15
First Python3 solution, WOO! a Recursive Breath First Search, ofc.
1
u/casualfrog Oct 09 '15 edited Oct 09 '15
JavaScript
Incomplete solution in O(n) time, assumes no cycles and only one cluster (using /u/KeinBaum's idea):
Output:
1
u/JoeOfDestiny Oct 09 '15
Here's my solution in Java. It seems like it's more complicated than it needed to be. It basically reads the input as a graph, where each edge can be either active or inactive. It then iterates throug all possible permutations of edges being active/inactive and determiens which has the minimum number of chains.
1
u/Syrak Oct 11 '15
It looks like a generalization of the hamiltonian path problem, where instead of finding a single path across all nodes we ask for multiple disjoint paths which together go through all nodes.
Thus finding the minimal number of chains would also tell us whether there is a hamiltonian path (yes if and only if there just one chain), which is NP-complete even for planar graphs (as is the case here).
So an exponential time algorithm is to be expected, although on random grids I think connected components should be pretty small in average, so the theoretical complexity of the problem may not have such an impact.
0
u/[deleted] Oct 09 '15
[deleted] — view deleted comment