Today is 10th class Maths Pre-Board exam and the students are arranged in a classroom forming N rows and M columns. There are two sets in Maths paper : SET-A and SET-B. The invigilator of the class being a new teacher, doesn't know the rules of distributing papers. So, he distributed the question papers (by mixing both the sets) randomly. Soon, he is being informed by one of the students the rule of distributing the paper which is that no adjacent students should get the same set of paper i.e. if a student gets a SET-A then the other students adjacent to him should get SET-B to avoid cheating.
Now since the paper is already distributed. The invigilator will have to exchange the sets of some students to ensure that no two adjacent students get the same set. Find the minimum number of students whose sets will be exchanged by the invigilator so that no two adjacent students have the same sets.
Two students are adjacent if they share one of their common sides (left,right,front or back if exists).
First line of the input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two lines. First Line contains the Integers N and M. N line follows each of which contains M spaced letters either A or B (or u can say a character array A of N rows and M columns is given input which consist of either A or B). A denotes SET-A and B denotes SET-B.
The output of each test case consist of a single line denoting the minimum number of students whose sets will be exchanged by the invegilator so that no two adjacent students have the same sets.
A A B A
B A B A
B B A B
Explanation: Minimum number of students whose sets got changed are 4 (indexes: ((0,1),(0,2),(0,3),(2,0))). The final alloted sets are:
A B A B
B A B A
A B A B
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.