Cost of painting n * m grid in C++



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.

Example

 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; }

Output

31
Updated on: 2020-02-05T07:17:06+05:30

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements