给定两个整数N和M ,表示棋盘的尺寸。任务是计算在N * M棋盘上放置黑骑士和白骑士的方法,以使它们不会互相攻击?骑士必须放置在不同的正方形上。骑士可以水平移动两个正方形,垂直移动一个正方形(L形),或者垂直移动两个正方形和水平移动一个正方形(L形)。如果一个人可以一步到达对方,则骑士会互相攻击。
例子:
Input: N=2, M=2
Output: 12
Explanation:
We can place a black and a white knight in 12 possible
ways such that none of them attracts each other.
Input: N=2, M=3
Output: 26
天真的方法:
- 幼稚的方法是针对每个牢房计数放置骑士的方式,以使两者可以相互攻击,然后从总体安排中减去计数。
- 可以将总安排计算为:
Total arrangements = M * N * (M * N - 1)
- 因为对于每个单元格,我们可以在(M * N – 1)个单元格中放置另一个骑士,并且总共M * N个单元格将在那里。
C++
// C++ program to count arrangements
// of two knight so that they do not
// attack each other.
#include
using namespace std;
// Function returns the count
// of arrangments
long long Solve(int n, int m)
{
int X_axis[]{ -2, -1, 1, 2};
int Y_axis[]{ 1, 2, 2, 1 };
long long ret = 0;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
for (int k = 0; k < 4; ++k)
{
int x = i + X_axis[k],
y = j + Y_axis[k];
if (x >= 0 && x < m
&& y >= 0 && y < n)
++ret;
}
}
}
long long Total = m * n;
Total = Total * (Total - 1) / 2;
return 2 * (Total - ret);
}
// Driver code
int main()
{
int N = 2, M = 3;
cout << Solve(N, M) << endl;
return 0;
}
Java
// Java program to count arrangements
// of two knight so that they do not
// attack each other.
import java.io.*;
import java.util.*;
class GFG {
// Function returns the count
// of arrangments
static long Solve(int n, int m)
{
int X_axis[] = { -2, -1, 1, 2};
int Y_axis[] = { 1, 2, 2, 1 };
long ret = 0;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
for(int k = 0; k < 4; ++k)
{
int x = i + X_axis[k];
int y = j + Y_axis[k];
if (x >= 0 && x < m &&
y >= 0 && y < n)
++ret;
}
}
}
long Total = m * n;
Total = Total * (Total - 1) / 2;
return 2 * (Total - ret);
}
// Driver code
public static void main(String[] args)
{
int N = 2, M = 3;
System.out.println(Solve(N, M));
}
}
// This code is contributed by coder001
Python3
# Python3 program to count arrangements
# of two knight so that they do not
# attack each other
# Function returns the count
# of arrangments
def Solve(n, m):
X_axis = []
X_axis = [ -2, -1, 1, 2 ]
Y_axis = []
Y_axis = [ 1, 2, 2, 1 ]
ret = 0
for i in range(m):
for j in range(n):
for k in range(4):
x = i + X_axis[k]
y = j + Y_axis[k]
if (x >= 0 and x < m and
y >= 0 and y < n):
ret += 1
Total = m * n
Total = Total * (Total - 1) // 2
return 2 * (Total - ret)
# Driver code
N = 2
M = 3
print(Solve(N, M))
# This code is contributed by sanjoy_62
C#
// C# program to count arrangements
// of two knight so that they do not
// attack each other.
using System;
class GFG{
// Function returns the count
// of arrangments
static long Solve(int n, int m)
{
int []X_axis = { -2, -1, 1, 2};
int []Y_axis = { 1, 2, 2, 1 };
long ret = 0;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
for(int k = 0; k < 4; ++k)
{
int x = i + X_axis[k];
int y = j + Y_axis[k];
if (x >= 0 && x < m &&
y >= 0 && y < n)
++ret;
}
}
}
long Total = m * n;
Total = Total * (Total - 1) / 2;
return 2 * (Total - ret);
}
// Driver code
public static void Main(String[] args)
{
int N = 2, M = 3;
Console.Write(Solve(N, M));
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ program to count arrangements
// of two knight so that they do not
// attack each other.
#include
using namespace std;
// Function returns the count
// of arrangments
long long Solve(int N, int M)
{
// Total arrangements
int ans = (N * M - 1) * N * M;
if (N >= 1 && M >= 2)
{
// Attacks possible in one horizontal
// and two vertical steps
ans -= (4 * (N - 1) * (M - 2));
}
if (N >= 2 && M >= 1)
{
// Attacks possible in Two horizontal
// and one vertical steps
ans -= (4 * (N - 2) * (M - 1));
}
return ans;
}
// Driver code
int main()
{
int N = 2, M = 3;
cout << Solve(N, M) << endl;
return 0;
}
Java
// Java program to count arrangements
// of two knight so that they do not
// attack each other.
import java.io.*;
import java.util.*;
class GFG {
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
// Total arrangements
int ans = (N * M - 1) * N * M;
if (N >= 1 && M >= 2)
{
// Attacks possible in one horizontal
// and two vertical steps
ans -= (4 * (N - 1) * (M - 2));
}
if (N >= 2 && M >= 1)
{
// Attacks possible in Two horizontal
// and one vertical steps
ans -= (4 * (N - 2) * (M - 1));
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 2, M = 3;
System.out.println(Solve(N, M));
}
}
// This code is contributed by coder001
Python3
# Python3 program to count arrangements
# of two knight so that they do not
# attack each other.
# Function returns the count
# of arrangments
def Solve(N, M):
# Total arrangements
ans = (N * M - 1) * N * M
if (N >= 1 and M >= 2):
# Attacks possible in one horizontal
# and two vertical steps
ans -= (4 * (N - 1) * (M - 2))
if (N >= 2 and M >= 1):
# Attacks possible in Two horizontal
# and one vertical steps
ans -= (4 * (N - 2) * (M - 1))
return ans
# Driver code
N = 2
M = 3
print(Solve(N, M))
# This code is contributed by sanjoy_62
C#
// C# program to count arrangements
// of two knight so that they do not
// attack each other.
using System;
class GFG{
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
// Total arrangements
int ans = (N * M - 1) * N * M;
if (N >= 1 && M >= 2)
{
// Attacks possible in one horizontal
// and two vertical steps
ans -= (4 * (N - 1) * (M - 2));
}
if (N >= 2 && M >= 1)
{
// Attacks possible in Two horizontal
// and one vertical steps
ans -= (4 * (N - 2) * (M - 1));
}
return ans;
}
// Driver code
static public void Main ()
{
int N = 2, M = 3;
Console.Write(Solve(N, M));
}
}
// This code is contributed by ShubhamCoder
Javascript
输出:
26
时间复杂度: O(N * M)
空间复杂度: O(1)。
高效方法:
- 假设木板有N行M列,现在首先考虑骑士在水平方向上移动2步,在垂直方向上移动1步,所以如果我们在(i,j)处移动,那么我们可以到达(i + 2,j + 1),(i + 2,j-1),(i-2,j + 1),(i-2,j-1)。要在板子内部放置(i + 2),我们可以将位置0到N -3即我们必须离开最后两行,否则(i + 2)将超出范围。类似地,如果(i-2)范围为2到N,则可能。
- 类似地,对于(j + 1),范围将是0到N-2,对于(j + 1)范围将是1到M-1,即在每种情况下少一列。
- 因此,在这种情况下,可能的攻击等于4 *(N – 2)*(M – 1)的安排
- 类似地,如果我们考虑在垂直方向上走两步,在水平方向上走一步,我们将少排一行,少排两列,以便两个骑士可以互相攻击。
- 我们将从总安排中减去此安排,即M * N *(M * N – 1) 。
C++
// C++ program to count arrangements
// of two knight so that they do not
// attack each other.
#include
using namespace std;
// Function returns the count
// of arrangments
long long Solve(int N, int M)
{
// Total arrangements
int ans = (N * M - 1) * N * M;
if (N >= 1 && M >= 2)
{
// Attacks possible in one horizontal
// and two vertical steps
ans -= (4 * (N - 1) * (M - 2));
}
if (N >= 2 && M >= 1)
{
// Attacks possible in Two horizontal
// and one vertical steps
ans -= (4 * (N - 2) * (M - 1));
}
return ans;
}
// Driver code
int main()
{
int N = 2, M = 3;
cout << Solve(N, M) << endl;
return 0;
}
Java
// Java program to count arrangements
// of two knight so that they do not
// attack each other.
import java.io.*;
import java.util.*;
class GFG {
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
// Total arrangements
int ans = (N * M - 1) * N * M;
if (N >= 1 && M >= 2)
{
// Attacks possible in one horizontal
// and two vertical steps
ans -= (4 * (N - 1) * (M - 2));
}
if (N >= 2 && M >= 1)
{
// Attacks possible in Two horizontal
// and one vertical steps
ans -= (4 * (N - 2) * (M - 1));
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 2, M = 3;
System.out.println(Solve(N, M));
}
}
// This code is contributed by coder001
Python3
# Python3 program to count arrangements
# of two knight so that they do not
# attack each other.
# Function returns the count
# of arrangments
def Solve(N, M):
# Total arrangements
ans = (N * M - 1) * N * M
if (N >= 1 and M >= 2):
# Attacks possible in one horizontal
# and two vertical steps
ans -= (4 * (N - 1) * (M - 2))
if (N >= 2 and M >= 1):
# Attacks possible in Two horizontal
# and one vertical steps
ans -= (4 * (N - 2) * (M - 1))
return ans
# Driver code
N = 2
M = 3
print(Solve(N, M))
# This code is contributed by sanjoy_62
C#
// C# program to count arrangements
// of two knight so that they do not
// attack each other.
using System;
class GFG{
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
// Total arrangements
int ans = (N * M - 1) * N * M;
if (N >= 1 && M >= 2)
{
// Attacks possible in one horizontal
// and two vertical steps
ans -= (4 * (N - 1) * (M - 2));
}
if (N >= 2 && M >= 1)
{
// Attacks possible in Two horizontal
// and one vertical steps
ans -= (4 * (N - 2) * (M - 1));
}
return ans;
}
// Driver code
static public void Main ()
{
int N = 2, M = 3;
Console.Write(Solve(N, M));
}
}
// This code is contributed by ShubhamCoder
Java脚本
输出:
26
时间复杂度: O(1)。
空间复杂度: O(1)。