ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมที่หาต้นทุนการทาสีกริด n*m
สำหรับสิ่งนี้ เราจะได้รับจำนวนเต็มสองจำนวน n และ m งานของเราคือคำนวณต้นทุนขั้นต่ำของการทาสี n*m grid ถ้าต้นทุนของการทาสีเซลล์เท่ากับจำนวนเซลล์ที่ทาสีแล้วที่อยู่ติดกัน
ตัวอย่างโค้ด
#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;
}
ผลลัพธ์
31