给定M x N棋盘。任务是确定我们可以在棋盘上进行的最大切割次数,以使棋盘不分为两部分。
例子:
Input: M = 2, N = 4
Output: Maximum cuts = 3
Input: M = 3, N = 3
Output: Maximum cuts = 4
表示:
- 对于M = 2,N = 2我们只能切割1个(红色标记)。如果再削减1块,那么棋盘将分为2块。
- 对于M = 2,N = 4,我们可以切割3次(红色标记)。如果再削减1块,那么棋盘将分为2块。
因此,可以观察到没有。切口数= (m-1)*(n-1) 。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// function that calculates the
// maximum no. of cuts
int numberOfCuts(int M, int N)
{
int result = 0;
result = (M - 1) * (N - 1);
return result;
}
// Driver Code
int main()
{
int M = 4, N = 4;
// Calling function.
int Cuts = numberOfCuts(M, N);
cout << "Maximum cuts = " << Cuts;
return 0;
}
Java
// Java implementation of above approach
class GFG {
// function that calculates the
// maximum no. of cuts
static int numberOfCuts(int M, int N)
{
int result = 0;
result = (M - 1) * (N - 1);
return result;
}
// Driver Code
public static void main(String args[])
{
int M = 4, N = 4;
// Calling function.
int Cuts = numberOfCuts(M, N);
System.out.println("Maximum cuts = " + Cuts);
}
}
Python3
# Python3 implementation of
# above approach
# function that calculates the
# maximum no. of cuts
def numberOfCuts(M, N):
result = 0
result = (M - 1) * (N - 1)
return result
# Driver code
if __name__=='__main__':
M, N = 4, 4
# Calling function.
Cuts = numberOfCuts(M, N)
print("Maximum cuts = ", Cuts)
# This code is contributed by
# Kriti_mangal
C#
//C# implementation of above approach
using System;
public class GFG{
// function that calculates the
// maximum no. of cuts
static int numberOfCuts(int M, int N)
{
int result = 0;
result = (M - 1) * (N - 1);
return result;
}
// Driver Code
static public void Main (){
int M = 4, N = 4;
// Calling function.
int Cuts = numberOfCuts(M, N);
Console.WriteLine("Maximum cuts = " + Cuts);
}
//This code is contributed by akt_mit
}
PHP
Javascript
输出:
Maximum cuts = 9
时间复杂度: O(1)
辅助空间: O(1)