Official

B - Visibility Editorial by en_translator


It is sufficient to count the number of cells until reaching an obstacle or outside the grid for the first time, starting from cell \((X, Y)\) and moving towards each of four directions (up, down, left and right).

Sample Code (C++)
This sample code counts cell \((X, Y)\) four times, once for each of the four directions, so the initial value is set to \(-3\) so that it is ultimately counted only once.

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int h = ri();
	int w = ri();
	int x = ri() - 1;
	int y = ri() - 1;
	std::string s[h];
	for (auto &i : s) std::cin >> i;
	
	int cnt = -3;
	for (int i = x; i < h && s[i][y] != '#'; i++) cnt++;
	for (int i = x; i >= 0 && s[i][y] != '#'; i--) cnt++;
	for (int j = y; j < w && s[x][j] != '#'; j++) cnt++;
	for (int j = y; j >= 0 && s[x][j] != '#'; j--) cnt++;
	printf("%d\n", cnt);
	return 0;
}

posted:
last update: