📌  相关文章
📜  可以在棋盘中进行最小切割,使其不分成 2 个部分

📅  最后修改于: 2021-10-23 08:28:40             🧑  作者: Mango

给定 M x N 棋盘。任务是确定我们可以在棋盘上进行的最大切割次数,以使棋盘不被分成 2 个部分。
例子:

Input: M = 2, N = 4
Output: Maximum cuts = 3

Input: M = 3, N = 3
Output: Maximum cuts = 4

表示:

  1. 对于 M = 2, N = 2 我们只能进行 1 次切割(红色标记)。如果我们再剪 1 刀,那么棋盘就会分成 2 块。
  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)