# Cost of painting n * m grid

Given two integers **n** and **m** which are the dimensions of a grid. The task is to find the cost of painting the grid cell by cell where the cost of painting a cell is equal to the number of painted cells adjacent to it. Any cell can be painted at any time if it hasn’t been painted already, minimize the cost of painting the grid.

**Examples:**

Input:n = 1, m = 1

Output:0

A single cell with no adjacent cells can be painted free of cost

Input:n = 1, m = 2

Output:1

1st cell will be painted free of cost but the cost of painting the second cell will be 1 as there is 1 adjacent cell to it which is painted

A **simple solution** is to generate all possible ways of painting the grid with **n * m** cells and that is **n * m!** calculate the cost for each one of them.

**Time Complexity:** O(n * m!)

**Efficient Approach:** Understand the coloring process in this graph instead of the grid. The coloring of a cell is therefore equal to the coloring of a vertex of the graph. The cost obtained for the painting of a cell is equal to the number of colored neighbors of the cell. This means that the cost obtained will be the number of edges between the current cell and the adjacent colored cells.

The crucial point is to note that after the end of coloring of all the cells, all the edges of the graph will be marked.

An interesting fact is that the edges of the graph are marked only once. This is because an edge connects two cells. We mark the cell when both cells are painted. We are not allowed to paint a cell more than once, which ensures that we only mark each cell once.

Thus, whatever the order in which you color the cells, the number of marked edges remain the same, so the cost will be the same.

And for given grid the number of edge will be **n * (m – 1) + m * (n – 1)**. This came from the fact that each row consists of **m – 1** edges and each column consists of **n – 1** edges.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum cost ` `int` `getMinCost(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `cost = (n - 1) * m + (m - 1) * n; ` ` ` `return` `cost; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4, m = 5; ` ` ` `cout << getMinCost(n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `gfg ` `{ ` ` ` `// Function to return the minimum cost ` `static` `int` `getMinCost(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `cost = (n - ` `1` `) * m + (m - ` `1` `) * n; ` ` ` `return` `cost; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `4` `, m = ` `5` `; ` ` ` `System.out.println(getMinCost(n, m)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum cost ` `def` `getMinCost(n, m): ` ` ` ` ` `cost ` `=` `(n ` `-` `1` `) ` `*` `m ` `+` `(m ` `-` `1` `) ` `*` `n ` ` ` `return` `cost ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n, m ` `=` `4` `, ` `5` ` ` `print` `(getMinCost(n, m)) ` ` ` `# This code is contributed by ` `# Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum cost ` `static` `int` `getMinCost(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `cost = (n - 1) * m + (m - 1) * n; ` ` ` `return` `cost; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 4, m = 5; ` ` ` `Console.WriteLine(getMinCost(n, m)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the minimum cost ` `function` `getMinCost(` `$n` `, ` `$m` `) ` `{ ` ` ` `$cost` `= (` `$n` `- 1) * ` `$m` `+ (` `$m` `- 1) * ` `$n` `; ` ` ` `return` `$cost` `; ` `} ` ` ` `// Driver code ` `$n` `= 4; ` `$m` `= 5; ` `echo` `getMinCost(` `$n` `, ` `$m` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

31

## Recommended Posts:

- Minimum cost to cover the given positions in a N*M grid
- Number of rectangles in N*M grid
- Count possible moves in the given direction in a grid
- Count Magic squares in a grid
- Largest connected component on a grid
- Maximum perimeter of a square in a 2D grid
- Find an N x N grid whose xor of every row and column is equal
- Check if it is possible to perform the given Grid Division
- Minimum distance to the corner of a grid from source
- Check if the robot is within the bounds of the grid after given moves
- Sum of the count of number of adjacent squares in an M X N grid
- Unique paths covering every non-obstacle block exactly once in a grid
- Minimum number of integers required to fill the NxM grid
- Min Cost Path | DP-6
- Minimum steps needed to cover a sequence of points on an infinite grid

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.