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.
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.
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?