-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_minimum_exchange.cpp
72 lines (59 loc) · 1.28 KB
/
10_minimum_exchange.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//{ Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution {
public:
int MinimumExchange(vector<vector<char>>matrix){
int ans1, ans2, n, m;
ans1 = ans2 = 0;
n = matrix.size();
m = matrix[0].size();
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if( matrix[i][j] == 'A'){
if((i+j)%2==1){
ans1++;
}else{
ans2++;
}
}
else{
if((i+j)%2==1){
ans2++;
}else{
ans1++;
}
}
}
}
if(ans1%2==0){
ans1 /=2;
}
if(ans2%2==0){
ans2 /= 2;
}
return min(ans1, ans2);
}
};
//{ Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int n, m;
cin >> n >> m;
vector<vector<char>>matrix(n, vector<char>(m,'x'));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)
cin >> matrix[i][j];
}
Solution obj;
int ans = obj.MinimumExchange(matrix);
cout << ans <<"\n";
}
return 0;
}
// } Driver Code Ends