32
2
Challenge
Given a binary matrix and a binary string, determine if that binary string can be found starting at any point in the matrix and moving in any direction at any subsequent point to form the binary string. That is, can the string be found folded up however inside the matrix?
The string can only be folded at 90 degrees or 180 degrees (edge connections; Manhattan Distance 1) and cannot overlap itself at any point.
Example
Let's take the following example:
Matrix:
010101
111011
011010
011011
Snake: 0111111100101
This is a truthy test case. We can see the snake folded up in the following position:
0-1 0 1 0 1
|
1 1 1-0 1 1
| | | |
0 1 1 0-1-0
| |
0 1-1 0 1 1
Rules
- Standard Loopholes Apply
- You may take the length of the string and the width and height of the matrix as input if you'd like
- You may take the binary matrix and the binary string as a multiline string/array of strings/newline-joined string/anything else-joined string and a string
- You may take the dimensions as a flat array instead of several arguments
- Your program must finish for any 5 x 5 matrix with any string of up to length 10 in under a minute
Limitations
- The matrix is not necessarily square
- The string will be non-empty
- The string can be length-1
- The string will not contain more squares than available (that is,
len(string) <= width(matrix) * height(matrix)
Test Cases
Truthy
01010
10101
01010
10101
01010
0101010101010101010101010
01110
01100
10010
10110
01101
011111000110100
0
0
10
01
1010
100
010
001
100010001
Falsy
00000
00000
00000
00000
00000
1
10101
01010
10101
01010
10101
11
100
010
001
111
10001
01010
00100
01010
10001
1000100010001000101010100
Sandbox Post (Deleted) – HyperNeutrino – 2017-09-25T02:07:20.850
4Or: Binary Boggle! Also, can you add a few more test cases? – Jonah – 2017-09-25T03:04:53.553
1What do flat, sharp, and round mean in this context? Does not square mean that the width and height maybe not be equal, or that the array can be jagged? – Tahg – 2017-09-25T10:28:13.400
what on earth is a round array – Conor O'Brien – 2017-09-25T10:45:11.890
Related. – Martin Ender – 2017-09-25T11:08:32.607
@Tahg I'll remove it because it's confusing but it was a joke :P – HyperNeutrino – 2017-09-25T12:00:27.187
@Jonah added a few more – HyperNeutrino – 2017-09-25T12:03:34.967
@ConorO'Brien lol was joke but wasn't funny lol :( removed it – HyperNeutrino – 2017-09-25T12:03:48.613
If you wanted your additional test cases to be formatted like your first one is, you could use the program I link to at the bottom of my answer. – Jonathan Frech – 2017-09-25T12:11:53.143
@JonathanFrech Cool, thanks. I'll reformat these cuz it's hard to test – HyperNeutrino – 2017-09-25T12:15:04.730
Can I take the input as a char[][] and char[] instead of a String? – Tahg – 2017-09-25T22:47:02.493
@Tahg Yes you may – HyperNeutrino – 2017-09-25T23:00:05.223