❓ The question first
❓ THE QUESTION
Problem — Trianglane
A lane is 2 rows of triangular tiles, C columns wide. Wet tiles (1) must be outlined with tape; dry tiles (0) need none. Tape is not placed where two wet tiles share a whole side. Each tile side is 1 metre. How many metres of tape are needed?
Input
First line: C. Second line: C space-separated 0/1 values (top row). Third line: the bottom row.
7 0 0 1 1 0 1 0 0 0 1 0 1 0 0
Output
One integer: the metres of tape.
11
🎬 The solution, step by step
The idea — be generous, then correct:
tape = 3 × (number of wet tiles)— as if every wet tile stood alone.- Every side shared by two wet tiles was counted twice but needs no tape:
tape −= 2per shared side. - Shared sides hide in only 3 places: along the top row, along the bottom row, and between the rows — between rows only in even columns (0-based); odd columns touch at a point.
Animation — walk the official sample
💻 The code
C = int(input()) top = list(map(int, input().split())) bottom = list(map(int, input().split())) tape = 3 * (sum(top) + sum(bottom)) # generous: every wet tile alone for row in (top, bottom): # shared sides inside each row for c in range(C - 1): if row[c] == 1 and row[c + 1] == 1: tape -= 2 for c in range(0, C, 2): # shared sides between rows: if top[c] == 1 and bottom[c] == 1: # EVEN columns only tape -= 2 print(tape)
⭐ Key points & traps
The trap that decides this problem: between-row neighbours in odd columns touch at a point, not a side. Subtracting 2 for them is the classic wrong answer. Even columns only — count columns from 0.
Reading the input: unlike most grid problems in this course, the rows arrive as space-separated numbers, so read them with
input().split() and int(), not as character strings.Trust but verify: the island-perimeter cross-check (Step 6) is a second, independent way to see 11 — useful in a contest when you doubt a formula.
✋ Your turn — predict first, then check
Quick trap check: C = 2, both rows are 0 1 (a wet pair in column 1). Tape?
Column 1 is odd — the two wet tiles touch at a point only, so nothing is subtracted: tape = 3 × 2 = 6. Move the same pair to column 0 (even) and it becomes 6 − 2 = 4.