# Problem Description
This is a grid pattern generation problem where we need to:
- Create an n×n grid (where n is always odd)
- Place black cells (represented as 1) in specific positions
- Fill remaining cells with white (represented as 0)
- Return the resulting grid as a 2D matrix
- The pattern follows a specific rule of expansion as n increases

# Visual Facts
1. Grid Properties:
   - Each grid is n×n where n is odd (1, 3, 5, 7, 9)
   - Each step adds 2 to n, creating a new outer ring

2. Cell States:
   - Cells are either black (filled) or white (empty)
   - For n=1: Single black cell
   - For n=3: One black cell at the center
   - For n=5: Five black cells
   - For n=7: Five black cells
   - For n=9: Nine black cells

# Visual Patterns
A cell at position [i][j] is black when either:
   1. It's the center: i = j = n//2
   2. It's a corner of an odd ring: |i-n//2| = |j-n//2| = 2k where k ≥ 1

This means black cells appear at:
- Center position
- Positions where distance from center (in both x and y) is equal and is a multiple of 2

All other cells are white.

This explains:
- n=1: Center only
- n=3: Center only
- n=5: Center + 4 cells at distance 2
- n=7: Same as n=5
- n=9: Center + 4 cells at distance 2 + 4 cells at distance 4


# Solution
from typing import List

def solution(n: int) -> List[List[int]]:
    """
    Given an n x n grid, determine the coordinates of a specific pattern that is formed on the grid. 
    
    Parameters:
        n (int): The dimensions of the grid (which is an n*n 2D matrix). n is always an odd number.
    
    Returns:
        List[List[int]]: A n*n 2D matrix where the coordinates of the black cells are marked with 1 and the rest are marked with 0.
    """
    grid = [[0 for _ in range(n)] for _ in range(n)]
    
    mid = n // 2
    grid[mid][mid] = 1
    
    for i in range(mid + 1):
        if (i-mid) % 2 == 1:
            continue
        grid[i][i] = 1
        grid[i][n - 1 - i] = 1
        grid[n - 1 - i][i] = 1
        grid[n - 1 - i][n - 1 - i] = 1
    
    return grid


# Test Script
def reference(n):
    grid = [[0 for _ in range(n)] for _ in range(n)]
    
    # Middle index
    mid = n // 2
    
    # Set the center
    grid[mid][mid] = 1
    
    # Set the diagonals
    for i in range(mid + 1):
        if (i-mid) % 2 == 1:
            continue
        grid[i][i] = 1
        grid[i][n-1-i] = 1
        grid[n-1-i][i] = 1
        grid[n-1-i][n-1-i] = 1
    
    return grid

def test():
    for n in range(3, 40, 4):
        try:
            result = solution(n)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`n`: {n}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        expected = reference(n)
        if result != expected:
            error_message = (
                f"The following Test Case failed:\n"
                f"`n`: {n}\n\n"
                f"Actual Output:\n{result}\n\n"
                f"Expected Output:\n{expected}"
            )
            assert False, error_message

test()