You are given a matrix of forward and back slashes, for instance:
//\\
\//\
//\/
A slash cuts along the diagonal of its cell corner-to-corner, splitting it in two pieces. Pieces from adjacent (horizontally or vertically) cells are glued together. Your task is to count the number of resulting pieces. For the same example, the pieces are easier to see in this illustration - 8 of them:
Write a function or a complete program. Input is a non-empty matrix in any convenient form. You may choose any pair of values (characters or numbers) to represent /
and \
; in the tests below we use 0=/
and 1=\
. Loopholes forbidden. Shortest wins.
in:
[[0,0,1,1],
[1,0,0,1],
[0,0,1,0]]
out:
8
in:
[[1]]
out:
2
in:
[[1,0],
[1,1],
[0,1],
[0,0]]
out:
6
in:
[[1,0,1,1,0,1,0,0,0,1,1,1],
[1,0,1,0,1,1,1,1,1,1,1,0],
[1,1,1,0,1,1,0,1,1,1,1,0],
[0,1,0,1,0,1,0,0,1,0,1,1],
[1,1,1,1,0,0,1,1,1,0,0,1]]
out:
19
in:
[[1,0,1,1,0,1,0,0,0,1,1,1,1,0,1,0,1],
[1,1,1,1,1,1,0,1,1,1,0,1,1,0,1,1,1],
[1,0,0,1,0,1,0,1,0,0,1,0,1,1,1,1,1],
[1,0,0,1,1,1,0,0,1,0,0,1,0,1,1,1,1],
[0,1,0,0,0,0,1,0,1,0,0,1,0,1,1,1,1],
[0,1,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0],
[0,1,1,1,1,1,0,0,1,0,1,0,0,1,1,1,0]]
out:
27
in:
[[0,1,1,1,1,1,1,1,0,0,1,0,1,0,0,0,0],
[1,1,1,0,0,0,1,1,1,1,1,0,1,1,0,1,0],
[1,0,0,1,1,1,0,0,0,1,0,1,0,0,1,1,1],
[0,0,0,1,1,0,1,0,0,0,1,1,0,1,1,1,0],
[1,1,0,1,0,0,1,0,0,1,0,1,0,0,0,1,0],
[0,1,0,1,0,0,0,1,0,1,0,1,0,1,1,0,0],
[0,1,1,1,0,0,1,0,1,0,0,0,0,1,1,1,1]]
out:
32