Cost of painting n * m grid in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find the cost of painting n*m grid.

For this we will be provided with two integers n and m. Our task is to calculate the minimum cost of painting a n*m grid is the cost of painting a cell is equal to the number of painted cells adjacent to it.


 Live Demo

#include <bits/stdc++.h>
using namespace std;
//calculating the minimum cost
int calc_cost(int n, int m){
   int cost = (n - 1) * m + (m - 1) * n;
   return cost;
int main(){
   int n = 4, m = 5;
   cout << calc_cost(n, m);
   return 0;


Published on 05-Feb-2020 11:45:55