#C11754. Counting Valid Moves in a Grid
Counting Valid Moves in a Grid
Counting Valid Moves in a Grid
You are given a grid with R rows and C columns, and an initial position (r, c) located within the grid. Your task is to determine the number of valid moves available from this position. A move is valid if it moves one unit up, down, left, or right and remains inside the grid.
Note: The grid uses 1-indexing i.e. the top-left cell is (1, 1) and the bottom-right cell is (R, C). The possible moves are:
- Move up: valid if r > 1
- Move down: valid if r < R
- Move left: valid if c > 1
- Move right: valid if c < C
For example, in a 3 x 3 grid starting at (2, 2), all four moves are valid.
The answer should be computed using the formula:
$$\text{moves} = \mathbf{1}_{\{r > 1\}} + \mathbf{1}_{\{r < R\}} + \mathbf{1}_{\{c > 1\}} + \mathbf{1}_{\{c < C\}} $$inputFormat
The input consists of a single line containing four space-separated integers: R
, C
, r
, and c
, where:
R
andC
denote the number of rows and columns in the grid respectively.r
andc
denote the current row and column of the starting position.
The grid uses 1-indexing.
outputFormat
Output a single integer representing the number of valid moves from the starting position that remain inside the grid.
## sample3 3 2 2
4
>