#P1524. Embroidery Needle Minimization
Embroidery Needle Minimization
Embroidery Needle Minimization
The cloth is an \(n \times m\) grid. Each cell (unit square) may have a diagonal stitch, either on the front or on the back of the cloth. A diagonal stitch in a cell covers one of the two diagonals. Specifically, a cell having a '/' stitch means that the stitch connects the top-right vertex and the bottom-left vertex of that cell; a cell having a '\\' stitch means that the stitch connects the top-left vertex and the bottom-right vertex of that cell.
During the embroidery process, a needle (i.e. a continuous thread without lifting the needle off the cloth) is used to produce a sequence of stitches. However, consecutive stitches within the same needle must lie on opposite sides of the cloth. Moreover, the needle may change sides only at grid vertices.
You are given two patterns corresponding to the two sides of the cloth. In the front pattern, a cell with a stitch is represented as a solid line character (i.e. '/' or '\\'), and in the back pattern, a cell with a stitch is represented as the corresponding character as well. A cell with no stitch is represented by a dot ('.'). Your task is to determine the minimum number of needles (threads) needed so that by stitching along the indicated diagonals (on the corresponding side), the entire pattern on both sides is embroidered.
Note: A needle is defined as a continuous embroidery process (i.e. a path through the stitches) such that consecutive stitches are on opposite sides. The needle can only switch sides at grid vertices.
inputFormat
The first line contains two integers \(n\) and \(m\) (\(1 \le n,m \le 1000\)), representing the number of rows and columns of the grid.
The next \(n\) lines each contain a string of length \(m\) representing the front pattern of the cloth. Each character is either '/', '\\', or '.', where '.' indicates that there is no stitch in that cell on the front side.
The following \(n\) lines each contain a string of length \(m\) representing the back pattern of the cloth. The characters have the same meaning, but now a stitch (if present) is on the back side.
outputFormat
Output a single integer, the minimum number of needles required to embroider the entire pattern on both sides according to the rules.
sample
2 2
/.
.\
..
..
2
</p>