CSES - Aalto Competitive Programming 2024 - wk10 - Mon - Results
Submission details
Task:Modern art
Sender:Farah
Submission time:2024-11-11 17:45:15 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp:4:19: warning: multi-character character constant [-Wmultichar]
    4 |     min_P = float('inf')
      |                   ^~~~~
input/code.cpp:18:7: error: invalid preprocessing directive #Start
   18 |     # Start with a rectangle of width and height
      |       ^~~~~
input/code.cpp:24:7: error: invalid preprocessing directive #Initialize
   24 |     # Initialize grid
      |       ^~~~~~~~~~
input/code.cpp:26:7: error: invalid preprocessing directive #Fill
   26 |     # Fill the rectangle
      |       ^~~~
input/code.cpp:37:7: error: invalid preprocessing directive #Add
   37 |     # Add protrusions to increase perimeter by 2 for each
      |       ^~~
input/code.cpp:38:7: error: invalid preprocessing directive #Find
   38 |     # Find the boundary cells and add protrusions
      |       ^~~~
input/code.cpp:43:23: error: invalid preprocessing directive #Try
   43 |                     # Try to add to the right
      |                       ^~~
input/code.cpp:48:23:...

Code

import math

def find_min_perimeter(A):
    min_P = float('inf')
    for width in range(1, A +1):
        height = math.ceil(A / width)
        perimeter = 2 * (width + height)
        if perimeter < min_P:
            min_P = perimeter
    return min_P

def construct_figure(A, P):
    min_P = find_min_perimeter(A)
    max_P = 2 * A + 2
    if not (P >= min_P and P <= max_P and P % 2 ==0):
        return "IMPOSSIBLE"
    
    # Start with a rectangle of width and height
    for width in range(1, A +1):
        height = math.ceil(A / width)
        perimeter = 2 * (width + height)
        if perimeter <= P:
            break
    # Initialize grid
    grid = [['0' for _ in range(width +2)] for _ in range(height +2)]
    # Fill the rectangle
    count =0
    for i in range(1, height +1):
        for j in range(1, width +1):
            if count < A:
                grid[i][j] = '1'
                count +=1
            else:
                break
    current_P = find_min_perimeter(A)
    extra_P = P - current_P
    # Add protrusions to increase perimeter by 2 for each
    # Find the boundary cells and add protrusions
    if extra_P >0:
        for i in range(1, height +1):
            for j in range(1, width +1):
                if grid[i][j] == '1':
                    # Try to add to the right
                    if j < width and grid[i][j+1] == '0' and count < A and extra_P >=2:
                        grid[i][j+1] = '1'
                        count +=1
                        extra_P -=2
                    # Try to add below
                    if i < height and grid[i+1][j] == '0' and count < A and extra_P >=2:
                        grid[i+1][j] = '1'
                        count +=1
                        extra_P -=2
                if extra_P ==0:
                    break
            if extra_P ==0:
                break
    # If still need to add pixels, continue searching
    i =1
    j =1
    while extra_P >0 and count < A:
        if grid[i][j] == '1':
            # Try to add in all four directions
            directions = [(-1,0),(1,0),(0,-1),(0,1)]
            for di,dj in directions:
                ni = i + di
                nj = j + dj
                if ni >=1 and ni <= height and nj >=1 and nj <= width and grid[ni][nj] == '0':
                    grid[ni][nj] = '1'
                    count +=1
                    extra_P -=2
                    if extra_P ==0 or count ==A:
                        break
        j +=1
        if j > width:
            j =1
            i +=1
            if i > height:
                break
    if extra_P !=0 or count !=A:
        return "IMPOSSIBLE"
    # Remove the padding
    trimmed_grid = [row[1:-1] for row in grid[1:-1]]
    # Convert to strings
    output = ["".join(row) for row in trimmed_grid]
    return "POSSIBLE\n" + "\n".join(output)

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    A = int(data[0])
    P = int(data[1])
    result = construct_figure(A, P)
    print(result)

if __name__ == "__main__":
    main()