给定的数N,所述任务是构建的N * N的方矩阵其中在一些元素的联合i相第i列第i行包含在范围[1,2 * N-1]的每一个元素。如果不存在这样的矩阵,则打印-1。
注意:对于特定的N,可以有多种可能的解决方案
例子:
Input: N = 6
Output:
6 4 2 5 3 1
10 6 5 3 1 2
8 11 6 1 4 3
11 9 7 6 2 4
9 7 10 8 6 5
7 8 9 10 11 6
Explanation:
The above matrix is of 6 * 6 in which every ith row and column contains elements from 1 to 11, like:
1st row and 1st column = {6, 4, 2, 5, 3, 1} & {6, 10, 8, 11, 9, 7} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
2nd row and 2nd column = {10, 6, 5, 3, 1, 2} & {4, 6, 11, 9, 7, 8} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
3rd row and 3rd column = {8, 11, 6, 1, 4, 3} & {2, 5, 6, 7, 10, 9} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
4th row and 4th column = {11, 9, 7, 6, 2, 4} & {5, 3, 1, 6, 8, 10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
5th row and 5th column = {9, 7, 10, 8, 6, 5} & {3, 1, 4, 2, 6, 11} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
6th row and 6th column = {7, 8, 9, 10, 11, 6} & {1, 2, 3, 4, 5, 6} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Input: N = 6
Output: -1
Explanation:
There is no such matrix possible in which every i’th row and i’th column contains every elements from 1 to 9. Hence, the answer is -1.
方法:
如果仔细观察,我们可以看到:
- 对于除1以外的任何奇数,不可能生成方矩阵
- 为了生成偶数阶的方矩阵,其思想是在1到N-1的范围内填充矩阵的对角元素的上半部分,并用N和对角线的下半部分填充所有对角元素元素可以填充的数字范围为N +1到2N – 1 。
下面是这种方法的算法:
- 除了N = 1之外,无法填充奇数阶矩阵
- 对于偶数阶矩阵,
- 首先,填充所有等于N的对角线元素。
- 考虑矩阵对角平分的两半,每半可以填充N-1个元素。
- 用[1,N-1]中的元素填充上半部分,并用[N + 1,2N-1]中的元素填充下半部分。
- 可以很容易地观察到第二行的最后一个元素可以始终为2的模式。
- 现在,最后一列中的连续元素相差2。因此,对于从1开始的所有i,可以将广义形式表示为A [i] = [(N-2)+ 2i]%(N-1)+1至N-1
- 只需将N添加到下半部分的所有元素。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
int matrix[100][100];
// Function to find the square matrix
void printRequiredMatrix(int n)
{
// For Matrix of order 1,
// it will contain only 1
if (n == 1) {
cout << "1"
<< "\n";
}
// For Matrix of odd order,
// it is not possible
else if (n % 2 != 0) {
cout << "-1"
<< "\n";
}
// For Matrix of even order
else {
// All diagonal elements of the
// matrix can be N itself.
for (int i = 0; i < n; i++) {
matrix[i][i] = n;
}
int u = n - 1;
// Assign values at desired
// place in the matrix
for (int i = 0; i < n - 1; i++) {
matrix[i][u] = i + 1;
for (int j = 1; j < n / 2; j++) {
int a = (i + j) % (n - 1);
int b = (i - j + n - 1) % (n - 1);
if (a < b)
swap(a, b);
matrix[b][a] = i + 1;
}
}
// Loop to add N in the lower half
// of the matrix such that it contains
// elements from 1 to 2*N - 1
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
matrix[i][j] = matrix[j][i] + n;
// Loop to print the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << matrix[i][j] << " ";
cout << "\n";
}
}
cout << "\n";
}
// Driver Code
int main()
{
int n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG {
static int matrix[][] = new int[100][100];
// Function to find the square matrix
static void printRequiredMatrix(int n)
{
// For Matrix of order 1,
// it will contain only 1
if (n == 1) {
System.out.println("1");
}
// For Matrix of odd order,
// it is not possible
else if (n % 2 != 0) {
System.out.println("-1");
}
// For Matrix of even order
else {
// All diagonal elements of the
// matrix can be N itself.
for (int i = 0; i < n; i++) {
matrix[i][i] = n;
}
int u = n - 1;
// Assign values at desired
// place in the matrix
for (int i = 0; i < n - 1; i++) {
matrix[i][u] = i + 1;
for (int j = 1; j < n / 2; j++) {
int a = (i + j) % (n - 1);
int b = (i - j + n - 1) % (n - 1);
if (a < b) {
int temp = a;
a = b;
b = temp;
}
matrix[b][a] = i + 1;
}
}
// Loop to add N in the lower half
// of the matrix such that it contains
// elements from 1 to 2*N - 1
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
matrix[i][j] = matrix[j][i] + n;
// Loop to print the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(matrix[i][j] + " ");
System.out.println() ;
}
}
System.out.println();
}
// Driver Code
public static void main (String[] args)
{
int n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
import numpy as np;
matrix = np.zeros((100,100));
# Function to find the square matrix
def printRequiredMatrix(n) :
# For Matrix of order 1,
# it will contain only 1
if (n == 1) :
print("1");
# For Matrix of odd order,
# it is not possible
elif (n % 2 != 0) :
print("-1");
# For Matrix of even order
else :
# All diagonal elements of the
# matrix can be N itself.
for i in range(n) :
matrix[i][i] = n;
u = n - 1;
# Assign values at desired
# place in the matrix
for i in range(n - 1) :
matrix[i][u] = i + 1;
for j in range(1, n//2) :
a = (i + j) % (n - 1);
b = (i - j + n - 1) % (n - 1);
if (a < b) :
a,b = b,a
matrix[b][a] = i + 1;
# Loop to add N in the lower half
# of the matrix such that it contains
# elements from 1 to 2*N - 1
for i in range(n) :
for j in range(i) :
matrix[i][j] = matrix[j][i] + n;
# Loop to print the matrix
for i in range(n) :
for j in range(n) :
print(matrix[i][j] ,end=" ");
print();
print()
# Driver Code
if __name__ == "__main__" :
n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach
using System;
class GFG {
static int [,]matrix = new int[100, 100];
// Function to find the square matrix
static void printRequiredMatrix(int n)
{
// For Matrix of order 1,
// it will contain only 1
if (n == 1) {
Console.WriteLine("1");
}
// For Matrix of odd order,
// it is not possible
else if (n % 2 != 0) {
Console.WriteLine("-1");
}
// For Matrix of even order
else
{
// All diagonal elements of the
// matrix can be N itself.
for (int i = 0; i < n; i++) {
matrix[i, i] = n;
}
int u = n - 1;
// Assign values at desired
// place in the matrix
for (int i = 0; i < n - 1; i++) {
matrix[i, u] = i + 1;
for (int j = 1; j < n / 2; j++) {
int a = (i + j) % (n - 1);
int b = (i - j + n - 1) % (n - 1);
if (a < b) {
int temp = a;
a = b;
b = temp;
}
matrix[b, a] = i + 1;
}
}
// Loop to add N in the lower half
// of the matrix such that it contains
// elements from 1 to 2*N - 1
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
matrix[i, j] = matrix[j, i] + n;
// Loop to print the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
Console.Write(matrix[i, j] + " ");
Console.WriteLine() ;
}
}
Console.WriteLine();
}
// Driver Code
public static void Main (String[] args)
{
int n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
}
}
// This code is contributed by Yash_R
1
-1
6 4 2 5 3 1
10 6 5 3 1 2
8 11 6 1 4 3
11 9 7 6 2 4
9 7 10 8 6 5
7 8 9 10 11 6
性能分析:
- 时间复杂度:与上述方法一样,在最坏的情况下,有两个循环遍历整个N * N矩阵,这需要O(N 2 )时间,因此时间复杂度将为O(N 2 ) 。
- 辅助空间复杂度:与上述方法一样,没有使用额外的空间,因此辅助空间复杂度将为O(1)