给定一个整数N和一个位置数组arr [] ,该数组表示已经放置的非攻击车的位置,任务是按字典顺序查找可以放置在N * N棋盘上的非攻击车的位置。
白嘴鸦的移动:任何白嘴鸦都可以水平或垂直移动任意数量的空闲正方形。
例子:
Inupt: N = 4, arr[] = {(1, 4), (2, 2)}
Output: 2
3 1
4 3
Explanation:
There can be two more rooks can be placed on the 4*4 Chessboard
Input: N = 5, arr[] = {}
Output: 5
1 1
2 2
3 3
4 4
5 5
天真的方法:初始化大小为N * N的2D矩阵mat [] [] ,每个单元格的大小为0,并将Rook的初始位置标记为rook,然后遍历矩阵mat [] [],检查是否第i行和第n行第j列中包含任何“ rook”,“保持”“ rook”的数量。如果任何行包含且列两者均不包含任何放置的规则,则将规则放置在此处并将该单元格添加到结果字符串中。
最后,打印放置的车的数量和车的位置
时间复杂度: O(N 3 )
空间复杂度: O(N 2 )
高效方法:这个想法是创建两个N大小的数组,以存储第i行或第i列是否包含任何流子,现在搜索该行和相应列是否已经包含Rook变得很有效。 。
时间复杂度: O(N 2 )
空间复杂度: O(N)
最有效的方法:问题中的关键观察是,可以放置的最大队列为NK 。也就是说,如果两个菜鸟在同一行或同一列上,则它们会相互攻击。由于给定的白名单中没有两个是相互攻击的,因此输入中给定的所有行都是唯一的。同样,输入中给出的所有列都是唯一的。因此,我们剩下了NK个未使用的行和NK个未使用的列来放置新程序。
换句话说,如果我们尝试用“鸽子洞”原理放更多的NK鸽子,那么如果有N + 1羽鸽子和N个地方需要填补,那么至少一个地方包含多于1羽鸽子。
并找到字典上的最小答案。可以通过将最小的未使用的行与最小的未使用的列配对,第二最小的未使用的行与第二最小的未使用的列配对来实现。
时间复杂度: O(N)
下面是上述方法的实现:
C++
// C++ implementation to find
// count of placing non-attacking
// rooks on the N x N chessboard
#include
using namespace std;
// Function to find the count of
// placing non-attacking rooks
// on the N x N chessboard
void findCountRooks(int row[], int col[],
int n, int k)
{
// Count of the Non-attacking rooks
int res = n - k;
cout << res << "\n";
int ri = 0, ci = 0;
while (res-- > 0)
{
// Printing lexographically
// smallest configuration
while (ri < k && row[ri] == 1)
{
ri++;
}
while (ci < k && col[ci] == 1)
{
ci++;
}
cout << (ri + 1) << " "
<< (ci + 1) << "\n";
ri++;
ci++;
}
}
// Driver Code
int main()
{
int n = 4;
int k = 2;
int row[] = { 1, 2 };
int col[] = { 4, 2 };
// Function call
findCountRooks(row, col, n, k);
return 0;
}
// This code is contributed by jana_sayantan
Java
// Java implementation to find
// count of placing non-attacking
// rooks on the N x N chessboard
import java.util.Scanner;
public class P2Placerooks {
// Function to find the count of
// placing non-attacking rooks
// on the N x N chessboard
static void findCountRooks(
int row[], int col[], int n, int k)
{
// Count of the Non-attacking rooks
int res = n - k;
System.out.println(res + " ");
int ri = 0, ci = 0;
while (res-- > 0) {
// Printing lexographically
// smallest configuration
while (ri < k && row[ri] == 1) {
ri++;
}
while (ci < k && col[ci] == 1) {
ci++;
}
System.out.println((ri + 1)
+ " " + (ci + 1)
+ " ");
ri++;
ci++;
}
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
int k = 2;
int row[] = { 1, 2 };
int col[] = { 4, 2 };
// Function Call
findCountRooks(row, col, n, k);
}
}
Python3
# Python3 implementation to find
# count of placing non-attacking
# rooks on the N x N chessboard
# Function to find the count of
# placing non-attacking rooks
# on the N x N chessboard
def findCountRooks(row, col, n, k):
# Count of the Non-attacking rooks
res = n - k
print(res)
ri = 0
ci = 0
while (res > 0):
# Printing lexographically
# smallest configuration
while (ri < k and row[ri] == 1):
ri += 1
while (ci < k and col[ci] == 1):
ci += 1
print((ri + 1), "", (ci + 1))
ri += 1
ci += 1
res -= 1
# Driver Code
n = 4
k = 2
row = [ 1, 2 ]
col = [ 4, 2 ]
# Function call
findCountRooks(row, col, n, k)
# This code is contributed by sanjoy_62
C#
// C# implementation to find
// count of placing non-attacking
// rooks on the N x N chessboard
using System;
class P2Placerooks{
// Function to find the count of
// placing non-attacking rooks
// on the N x N chessboard
static void findCountRooks(int []row,
int []col,
int n, int k)
{
// Count of the Non-attacking rooks
int res = n - k;
Console.WriteLine(res + " ");
int ri = 0, ci = 0;
while (res-- > 0)
{
// Printing lexographically
// smallest configuration
while (ri < k && row[ri] == 1)
{
ri++;
}
while (ci < k && col[ci] == 1)
{
ci++;
}
Console.WriteLine((ri + 1) + " " +
(ci + 1) + " ");
ri++;
ci++;
}
}
// Driver Code
public static void Main(String[] args)
{
int n = 4;
int k = 2;
int []row = { 1, 2 };
int []col = { 4, 2 };
// Function call
findCountRooks(row, col, n, k);
}
}
// This code is contributed by Rajput-Ji
2
2 1
3 2
性能分析:
- 时间复杂度: O(N)
- 辅助空间: O(1)