Official

B - 海図の航路 / Sea Chart Lanes Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

Find the number of columns where every cell is sea .. We record whether each column contains an island # and count the columns that contain no islands at all.

Analysis

If a column contains even a single island, a ship cannot sail straight down that column from top to bottom. Therefore, the condition for a column to be a valid route is that “the column contains no #.”

For example, consider the following sea chart:

..#.
#...
..#.
  • The \(1\)-st and \(3\)-rd columns from the left contain islands, so they cannot be used as routes.
  • The \(2\)-nd and \(4\)-th columns from the left consist entirely of sea, so they can be used as routes.

Thus, the answer is \(2\).

Even a naive approach that checks all cells in each column takes \(O(HW)\) time, which is fast enough for this problem. No special optimization is required to avoid TLE.

In the provided code, instead of storing the entire sea chart in memory, we use a method of marking columns that contain islands. This allows us to process the input row by row.

Note that simply encountering a sea cell is not enough to conclude that the column can be a route, because there might be an island in a different row of the same column.

Algorithm

Prepare an array blocked to record the presence of islands in each column. If blocked[j] is true, it indicates that column \(j\) contains an island.

  1. Initialize an array blocked of length \(W\) with all elements set to false.
  2. Read the string for each row and inspect all columns:
    • If s[j] is #, set blocked[j] to true.
    • If s[j] is ., do nothing.
  3. After processing all rows, count and output the number of columns where blocked[j] is false.

Since we only set a column to true when an island is found, the columns that remain false after processing are those with no islands at all. This corresponds to the columns that can serve as routes, yielding the correct answer.

Complexity

  • Time complexity: \(O(HW)\)
    We inspect all \(HW\) cells and finally check the marks of the \(W\) columns.
  • Space complexity: \(O(W)\)
    We use the array blocked of length \(W\) and a string to store one row at a time while reading the input.

Implementation Notes

  • Since array and string indices in C++ are 0-indexed, column indices range from \(0\) to \(W-1\).

  • Once blocked[j] is set to true, it must not be reset to false even if sea is found in subsequent rows. If there is even one island, that column cannot be used as a route.

  • There is no need to store the entire sea chart. We can update the information about columns with islands each time a row is read.

    Source Code

// 問題文から独立に実装: 島のある列に印を付け、印のない列を数える
#include <iostream>
#include <string>
#include <vector>
int main() {
    int h, w; std::cin >> h >> w;
    std::vector<bool> blocked(w, false);
    for (int i = 0; i < h; ++i) {
        std::string s; std::cin >> s;
        for (int j = 0; j < w; ++j) if (s[j] == '#') blocked[j] = true;
    }
    int open = 0;
    for (int j = 0; j < w; ++j) if (!blocked[j]) open++;
    std::cout << open << std::endl;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: