-
Notifications
You must be signed in to change notification settings - Fork 0
/
Black and White.cpp
76 lines (59 loc) · 1.29 KB
/
Black and White.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
73
74
75
76
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
long long numOfWays(int n, int m);
int main() {
// code
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
cout << numOfWays(n, m) << endl;
}
return 0;
}
// } Driver Code Ends
//Function to find out the number of ways we can place a black and a
//white Knight on this chessboard such that they cannot attack each other.
bool isValid(int i, int j, int &N, int &M){
return i>=0 && i<N && j>=0 && j<M;
}
int solve(int i, int j, int &N, int &M){
long long count = 0;
if(isValid(i-2,j+1,N,M)){
count++;
}
if(isValid(i-2,j-1,N,M)){
count++;
}
if(isValid(i-1,j+2,N,M)){
count++;
}
if(isValid(i+1,j+2,N,M)){
count++;
}
if(isValid(i+2,j+1,N,M)){
count++;
}
if(isValid(i+2,j-1,N,M)){
count++;
}
if(isValid(i-1,j-2,N,M)){
count++;
}
if(isValid(i+1,j-2,N,M)){
count++;
}
return N*M - count - 1;
}
long long numOfWays(int N, int M){
long long ans = 0;
long long mod = 1e9+7;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
ans = (ans + solve(i,j,N,M))%mod;
}
}
return ans;
}