Frogs Jump
Given a grid, each cell of the grid is either empty or contains one frog. It is guaranteed that each column of the input grid contains at least one frog. You can choose frogs and remove them from the grid. After this, all remaining frogs will jump.
Each frog can jump up, down, left, or right by cell, but cannot jump outside the grid. All frogs must choose a legal direction and jump simultaneously exactly once. After jumping, the number of frogs in each row and each column of the grid must remain unchanged.
Note that:
- Even if two frogs' jump directions conflict, they can still jump to their respective target cells.
- Multiple frogs are allowed to jump to the same cell, because a cell can hold arbitrarily many frogs.
Find the minimum such that there exists a valid jumping scheme.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
For each test case, the first line contains a single integer (), where is the number of columns in the grid.
The next two lines each contain a string of length ; we use the character to represent a cell containing a frog, and the character to represent an empty cell.
It is guaranteed that each column of the input grid contains at least one frog.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single line containing the minimum such that there exists a valid jumping scheme.
Note
In the first test case, two frogs can jump to each other's cells.
In the second test case, the left frog can jump down, and the right frog can jump up. After the jumps, the total number of frogs in each row and each column remains .
| o | f |
|---|---|
| f | o |
In the third test case, three frogs can all jump to the same cell , although it is not a valid answer, since the number of frogs in the first column changes from to .
| o | f f f | o |
|---|---|---|
| o | o | o |
4 2 ff oo 2 fo of 3 fof ofo 1 f o
0 0 1 1
Discussions & Comments
Share your approach or ask questions about this problem (0)
You must be logged in to comment or start a discussion on this problem.
Log InNo comments yet. Be the first to comment!
Actions
Open on CodeforcesAbout Platform
Problem Discussions
0Have questions or want to share your solution approach? Join the discussion.
View / write comments