Given an M × N matrix. The task is to count the number of adjacent cells and calculate their sum.
Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally.
Input : m = 2, n = 2
Output : 12
Input : m = 3, n = 2
In a m X n grid there can be 3 cases:
- Corner cells touch 3 cells, and there are always 4 corner cells.
- Edge cells touch 5 cells, and there are always 2 * (m+n-4) edge cells.
- Interior cells touch 8 cells, and there are always (m-2) * (n-2) interior cells.
Sum = 3*4 + 5*2*(m+n-4) + 8*(m-2)*(n-2) = 8mn - 6m - 6n +4
Below is the implementation of the above approach:
- Count Magic squares in a grid
- Find the number of squares inside the given square grid
- Find the number of p-sided squares in a grid with K blacks painted
- Count number of squares in a rectangle
- Count number less than N which are product of perfect squares
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Count the total number of squares that can be visited by Bishop in one move
- Program to count number of distinct Squares and Cubes upto N
- Minimum product in a grid of adjacent elements
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Count possible moves in the given direction in a grid
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Count squares with odd side length in Chessboard
- Count of squares that can be drawn without lifting the pencil
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.