Official

B - Make Target Editorial by en_translator


Solution 1: Implement just as instructed in the problem statement

In this approach, we first create a two-dimensional array, and then paint it with the colors specified in the problem statement. Be sure to adjust the definition of the indices \(i\) and \(j\), because most programming languages adopt \(0\)-based indices. For more details, please refer to the sample code below.

Sample code (Python3)

N = int(input())

target = [["?"] * N for _ in range(N)]

for i in range(N):
    j = N - i - 1
    if i <= j:
        for x in range(i, j + 1):
            for y in range(i, j + 1):
                if i % 2 == 0:
                    target[x][y] = "#"
                else:
                    target[x][y] = "."

for row in target:
    print("".join(row))

Solution 2: Directly decide the color for each cell

The color of cell \((i,j)\) can be determined as follows:

  • black if \(\min(i,j,N+1-i.N+1-j)\) is odd, and white otherwise.

This is because cell \((i,j)\) is painted for the last time in the \(\min(i,j,N+1-i.N+1-j)\)-th iteration.

Same caveats than solution 1 on the indices applies to this solution too.

Sample code (Python3)

N = int(input())

target = [["?"] * N for _ in range(N)]

for i in range(N):
    for j in range(N):
        if min(i, j, N - i - 1, N - j - 1) % 2 == 0:
            target[i][j] = "#"
        else:
            target[i][j] = "."

for row in target:
    print("".join(row))

Bonus

Which solution can be regarded faster?

Answer Solution 2 is faster. The difference should be significant for $N=5000$. "Computational complexity" is a useful tool to estimate the speed of a solution. To learn about computational complexity, you can refer to the chapter [W - 2.06.計算量](https://atcoder.jp/contests/apg4b/tasks/APG4b_w) ("W - 2.06.Computational complexity") in APG4b (AtCoder Programming Guide for Beginners). (Unfortunately, this material is provided only in Japanese). In AtCoder Beginner Contest, in most cases you do not have to care about the complexity (with rare exceptions like [ABC334-B](https://atcoder.jp/contests/abc334/tasks/abc334_b)). However, if you try to solve Problem C and further, implementation without care to complexity may result in a `TLE` (Time Limit Exceeded) verdict. If you want to improve your performance, we strongly recommend you to think about complexity while solving.

posted:
last update: