给定一个整数N使得有一个大小为N*N的棋盘和一个由K对整数组成的数组pos[][] ,它们代表了在给定棋盘中放置的车的位置。任务是找到可以放置在给定棋盘上的具有其位置的最大数量的车,以便没有车攻击其他车。按字典顺序打印位置。
例子:
Input: N = 4, K = 2, pos[][] = {{1, 4}, {2, 2}}
Output:
2
3 1
4 3
Explanation:
Only 2 more rooks can be placed on the given chessboard and their positions are (3, 1) and (4, 3).
Input: N = 5, K = 0, pos[][] = {}
Output:
5
1 1
2 2
3 3
4 4
5 5
Explanation:
Since the chessboard is empty we can place 5 rooks the given chessboard and their positions are (1, 1), (2, 2), (3, 3), (4, 4) and (5, 5).
朴素的方法:最简单的方法是尝试在棋盘的每个空位置放置一个车,并检查它是否攻击已经放置的车。以下是步骤:
- 初始化一个大小为 N*N 的二维矩阵M[][]来表示棋盘并将已经给定的车放入其中。
- 遍历完整矩阵M[][]并检查第 i 行和第 j 列是否包含任何车
- 如果第 i 行和第 j 列都不包含任何车,则将车放置在那里并将此单元格添加到结果中。
- 否则,移动到棋盘上的下一个空单元格。
时间复杂度: O(N 3 )
辅助空间: O(N 2 )
高效方法:该方法基于这样一种思想,即根据鸽巢原则,棋盘上最多可以放置(N – K)只车。以下是步骤:
- 由于没有两个给定的车会互相攻击,因此输入中给出的所有行都必须是唯一的。同样,输入中给出的所有列都必须是唯一的。
- 因此,仅将车放在N – K 个未使用的行和N – K 个未使用的列中。
- 因此,通过将最小的未使用行与最小的未使用列、第二小的未使用行与第二小的未使用的列配对,可以实现字典序最小配置。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to print the maximum rooks
// and their positions
void countRooks(int n, int k,
int pos[2][2])
{
int row[n] = {0};
int col[n] = {0};
// Initialize row and col array
for (int i = 0; i < n; i++)
{
row[i] = 0;
col[i] = 0;
}
// Marking the location of
// already placed rooks
for (int i = 0; i < k; i++)
{
row[pos[i][0] - 1] = 1;
col[pos[i][1] - 1] = 1;
}
int res = n - k;
// Print number of non-attacking
// rooks that can be placed
cout << res << " " << endl;
// To store the placed rook
// location
int ri = 0, ci = 0;
while (res-- > 0)
{
// Print lexographically
// smallest order
while (row[ri] == 1)
{
ri++;
}
while (col[ci] == 1)
{
ci++;
}
cout << (ri + 1) << " " <<
(ci + 1) << " " <
Java
// Java program for the above approach
public class GFG {
// Function to print the maximum rooks
// and their positions
private static void countRooks(int n, int k,
int pos[][])
{
int row[] = new int[n];
int col[] = new int[n];
// Initialize row and col array
for (int i = 0; i < n; i++) {
row[i] = 0;
col[i] = 0;
}
// Marking the location of
// already placed rooks
for (int i = 0; i < k; i++) {
row[pos[i][0] - 1] = 1;
col[pos[i][1] - 1] = 1;
}
int res = n - k;
// Print number of non-attacking
// rooks that can be placed
System.out.println(res + " ");
// To store the placed rook
// location
int ri = 0, ci = 0;
while (res-- > 0) {
// Print lexographically
// smallest order
while (row[ri] == 1) {
ri++;
}
while (col[ci] == 1) {
ci++;
}
System.out.println(
(ri + 1)
+ " " + (ci + 1)
+ " ");
ri++;
ci++;
}
}
// Driver Code
public static void main(String[] args)
{
// Size of board
int N = 4;
// Number of rooks already placed
int K = 2;
// Position of rooks
int pos[][] = { { 1, 4 }, { 2, 2 } };
// Function call
countRooks(N, K, pos);
}
}
Python3
# Python3 program for the above approach
# Function to prthe maximum rooks
# and their positions
def countRooks(n, k, pos):
row = [0 for i in range(n)]
col = [0 for i in range(n)]
# Marking the location of
# already placed rooks
for i in range(k):
row[pos[i][0] - 1] = 1
col[pos[i][1] - 1] = 1
res = n - k
# Print number of non-attacking
# rooks that can be placed
print(res)
# To store the placed rook
# location
ri = 0
ci = 0
while (res > 0):
# Print lexographically
# smallest order
while (row[ri] == 1):
ri += 1
while (col[ci] == 1):
ci += 1
print((ri + 1), (ci + 1))
ri += 1
ci += 1
res -= 1
# Driver Code
if __name__ == '__main__':
# Size of board
N = 4
# Number of rooks already placed
K = 2
# Position of rooks
pos= [ [ 1, 4 ], [ 2, 2 ] ]
# Function call
countRooks(N, K, pos)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to print the maximum rooks
// and their positions
private static void countRooks(int n, int k,
int [, ]pos)
{
int []row = new int[n];
int []col = new int[n];
// Initialize row and col array
for (int i = 0; i < n; i++)
{
row[i] = 0;
col[i] = 0;
}
// Marking the location of
// already placed rooks
for (int i = 0; i < k; i++)
{
row[pos[i, 0] - 1] = 1;
col[pos[i, 1] - 1] = 1;
}
int res = n - k;
// Print number of non-attacking
// rooks that can be placed
Console.WriteLine(res + " ");
// To store the placed rook
// location
int ri = 0, ci = 0;
while (res -- > 0)
{
// Print lexographically
// smallest order
while (row[ri] == 1)
{
ri++;
}
while (col[ci] == 1)
{
ci++;
}
Console.WriteLine((ri + 1) + " " +
(ci + 1) + " ");
ri++;
ci++;
}
}
// Driver Code
public static void Main(String[] args)
{
// Size of board
int N = 4;
// Number of rooks already placed
int K = 2;
// Position of rooks
int [, ]pos = {{1, 4}, {2, 2}};
// Function call
countRooks(N, K, pos);
}
}
// This code is contributed by Rajput-Ji
Javascript
2
3 1
4 3
时间复杂度: O(N 2 )
辅助空间: O(N 2 )
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live