#K11111. Snake Detection in a Grid
Snake Detection in a Grid
Snake Detection in a Grid
You are given a grid consisting of m rows and n columns. Each cell of the grid contains either a #
or a dot (.
).
A snake is defined as a connected sequence of #
characters that forms a straight horizontal or vertical line, with a minimum length of 2 cells. Your task is to detect if there is at least one snake present in the grid.
In other words, you need to check if there are two or more consecutive #
characters in any row (horizontal snake) or in any column (vertical snake).
You must write a program that reads the grid from the standard input and outputs YES
if a snake is present, or NO
otherwise.
The mathematical condition for a snake can be interpreted as finding if there exists an index pair \((i, j)\) such that either:
- \(grid[i][j] = '#'\) and \(grid[i][j+1] = '#'\) (horizontal condition), or
- \(grid[i][j] = '#'\) and \(grid[i+1][j] = '#'\) (vertical condition).
inputFormat
The first line of input contains two space-separated integers m
and n
, indicating the number of rows and columns of the grid, respectively.
This is followed by m
lines, each containing a string of length n
that represents a row of the grid.
outputFormat
Output a single line containing either YES
if a snake exists in the grid, or NO
if there is no snake present.
3 3
.#.
###
.#.
YES