#K42757. Optimal Traffic Signal Placement
Optimal Traffic Signal Placement
Optimal Traffic Signal Placement
In this problem, you are given an \(n \times n\) grid representing the layout of a city. Each cell of the grid is either a road ('R') or a building ('B'). Your task is to place a single traffic signal on one of the road cells in order to maximize the coverage of green lights along its row and column.
The signal’s effectiveness is determined by the formula:
\(\text{score} = \text{rowCount}[i] + \text{colCount}[j] - 1\)
where \(\text{rowCount}[i]\) is the number of road cells in the \(i\)-th row, and \(\text{colCount}[j]\) is the number of road cells in the \(j\)-th column. The cell where the signal is placed is counted in both the row and column counts, so we subtract 1 to avoid double counting.
If there is no road in the grid, output 0. If multiple positions achieve the maximum score, your program should choose the first one encountered in row-major order.
inputFormat
The input is read from standard input and consists of:
- The first line contains an integer \(n\) (\(1 \le n \le 1000\)) representing the grid size.
- The next \(n\) lines each contain a string of length \(n\) consisting of characters 'R' and 'B', representing the grid layout.
outputFormat
If there is no road in the grid, print a single line with the integer 0. Otherwise, print two lines:
- The first line contains the number of traffic signals placed (which is 1).
- The second line contains two space-separated integers representing the 1-based row and column indices of the chosen cell.
4
RRRB
RRBB
RRRR
BBRB
1
3 1
</p>