📜  填充NxM网格所需的最小整数数

📅  最后修改于: 2021-04-24 17:11:39             🧑  作者: Mango

给定一个大小为(NxM)的网格,将使用整数填充。

网格中单元格的填充应采用以下方式:

  1. 令A,B和C为三个像元,并且B和C与A共享一侧。
  2. 单元格B和C的值必须是不同的。
  3. 令L为网格中不同整数的数量。
  4. 每个像元应包含从1到L的值。

任务是找到L的最小值和所有结果网格。

例子:

Input: N = 1, M = 2
Output:
L = 2
grid = {1, 2}

Input: 2 3
Output:
L = 3
grid = {{1, 2, 3},
        {1, 2, 3}}
Explanation: Integers in the neighbors 
of cell (2, 2) are 1, 2 and 3.
All numbers are pairwise distinct.

方法:
假定与另一个单元共享一侧的两个单元必须是不同的。对于每个此类单元格,网格中最多可能有8个单元格,其值必须不同。
它将出现4种颜色问题:填充区域所需的最大颜色将为4。

  1. 对于N <4或M <4
    所需的整数数量可能从1到4不等。
    检查8个单元格,然后填充当前单元格。
    如果8个像元中不同整数的数量小于L,则用剩余的所有整数填充当前像元,否则用L + 1个整数填充当前像元。
  2. 对于N> = 4和M> = 4
    根据4种颜色问题,所需的整数数目必须为4。
    使用4×4矩阵填充NxM矩阵。
    1 2 3 4
    1 2 3 4
    3 4 1 2
    3 4 1 2

下面是上述方法的实现:

执行:

# Python 3 implementation of
# above approach
  
  
# Function to display the matrix
def display_matrix(A):
    for i in A:
        print(*i)
  
  
# Function for calculation
def cal_main(A, L, x, i, j):
    s = set()
  
    # Checking 8 cells and
    # then fill the current cell.
    if (i - 2) >= 0:
        s.add(A[i - 2][j])
    if (i + 2) < N:
        s.add(A[i + 2][j])
    if (j - 2) >= 0:
        s.add(A[i][j - 2])
    if (j + 2) < M:
        s.add(A[i][j + 2])
    if (i - 1) >= 0 and (j - 1) >= 0:
        s.add(A[i - 1][j - 1])
    if (i - 1) >= 0 and (j + 1) < M:
        s.add(A[i - 1][j + 1])
    if (i + 1) < N and (j - 1) >= 0:
        s.add(A[i + 1][j - 1])
    if (i + 1) < N and (j + 1) < M:
        s.add(A[i + 1][j + 1])
      
    # Set to contain distinct value
    # of integers in 8 cells.
    s = s.difference({0})
  
    if len(s) < L:
  
        # Set contain remaining integers
        w = x.difference(s)
  
        # fill the current cell
        # with maximum remaining integer
        A[i][j] = max(w)
    else:
  
        # fill the current cells with L + 1 integer.
        A[i][j] = L + 1
        L += 1
  
        # Increase the value of L
        x.add(L)
    return A, L, x
  
  
# Function to find the number
# of distinct integers
def solve(N, M):
  
    # initialise the list (NxM) with 0.
    A = []
    for i in range(N):
        K = []
        for j in range(M):
            K.append(0)
        A.append(K)
      
    # Set to contain distinct
    # value of integers from 1-L
    x = set()
    L = 0
  
    # Number of integer required
    # may vary from 1 to 4.
    if N < 4 or M < 4:
        if N > M:  # if N is greater
            for i in range(N):
                for j in range(M):
                    cal_main(A, L, x, i, j)
  
        else:
            # if M is greater
            for j in range(M):
                for i in range(N):
                    cal_main(A, L, x, i, j)
    else:
  
        # Number of integer required
        # must be 4
        L = 4
  
        # 4×4 matrix to fill the NxM matrix.
        m4 = [[1, 2, 3, 4], 
            [1, 2, 3, 4], 
            [3, 4, 1, 2], 
            [3, 4, 1, 2]]
  
        for i in range(4):
            for j in range(4):
                A[i][j] = m4[i][j]
        for i in range(4, N):
            for j in range(4):
                A[i][j] = m4[i % 4][j]
        for j in range(4, M):
            for i in range(N):
                A[i][j] = A[i][j % 4]
    print(L)
    display_matrix(A)
  
  
# Driver Code
if __name__ == "__main__":
  
    # sample input
    # Number of rows and columns
    N, M = 10, 5
    solve(N, M)
输出:
4
1 2 3 4 1 2 3 4 1 2
1 2 3 4 1 2 3 4 1 2
3 4 1 2 3 4 1 2 3 4
3 4 1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4 1 2
1 2 3 4 1 2 3 4 1 2
3 4 1 2 3 4 1 2 3 4
3 4 1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4 1 2
1 2 3 4 1 2 3 4 1 2