📌  相关文章
📜  根据给定条件从给定位置逃离给定矩阵的移动计数

📅  最后修改于: 2022-05-13 01:56:06.675000             🧑  作者: Mango

根据给定条件从给定位置逃离给定矩阵的移动计数

给定一个N x M矩阵mat[][] ,最初我们站在索引为(i, j)的单元格处,任务是找到逃避给定矩阵所需的操作数,其中在每个操作mat[x] [y]可以从mat[i][j]得到, x表示mat[i][j]的二进制表示中0 的计数, y表示mat[i]的二进制表示中1 的计数[j] .

例子

方法:给定的问题可以在 Brian Kernighan 的算法和内置日志函数的帮助下解决。以下是要遵循的步骤:

  • 创建一个变量Jump ,它存储了转义给定二维数组所需的跳转操作数。最初,跳转 = 0
  • 如果初始单元格已经超出给定矩阵的范围,则返回0
  • 计算mat[i][j]的二进制表示中01 的计数。
  • 跳转到下一个有效索引并将mat[i][j]设置为-1 。此外,将Jump的值增加1
  • 检查跳转后mat[i][j]的当前值是否=-1 。这意味着当前索引已经被访问过,因此创建了一个永无止境的循环。因此,返回-1
  • 重复上述过程,直到当前索引已被访问或超出给定矩阵的范围。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#define Max 100
using namespace std;
 
// Function to find total count of
// set bits in any number
int brianKernighan(int N)
{
    int Count = 0;
    while (N) {
        N = N & (N - 1);
        Count += 1;
    }
    return Count;
}
 
// Function to find left most Set bit
// position in any number
int getMsbIndex(int N)
{
    return ((int)log2(N) + 1);
}
 
// Function to find the number of
// jumps to escape the matrix from
// the given index [i, j]
int countJumps(int Mat[][Max],
               int N, int M,
               int i, int j)
{
    // Initialize the variable Jump
    int C0, C1, Jump = 0;
 
    while (i < N && j < M) {
 
        // When the element is already visited
        // then a closed loop is formed and
        // it is impossible to escape then
        // return -1.
        if (Mat[i][j] == -1)
            return -1;
 
        // Calculate Count of 1 in Mat[i][j]
        C1 = brianKernighan(Mat[i][j]);
 
        // Calculate Count of 0 in Mat[i][j]
        C0 = getMsbIndex(Mat[i][j]) - C1;
 
        // Set the element Mat[i][j] visited
        Mat[i][j] = -1;
 
        // Set i and j to count if 0 and 1
        i = C0, j = C1;
 
        // Increment Jump by 1
        Jump++;
    }
 
    // Return number of Jumps to escape
    // the matrix if it is possible to
    // escape
    return Jump;
}
 
// Driver Code
int main()
{
    int N = 3, M = 4;
    int i = 0, j = 0;
    int Mat[][Max] = { { 5, 13, 8, 1 },
                       { 7, 9, 2, 15 },
                       { 12, 4, 8, 3 } };
 
    cout << countJumps(Mat, N, M, i, j);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
// Function to find total count of
// set bits in any number
static int brianKernighan(int N)
{
    int Count = 0;
    while (N != 0) {
        N = N & (N - 1);
        Count += 1;
    }
    return Count;
}
 
// Function to find left most Set bit
// position in any number
static int getMsbIndex(int N)
{
    return ((int)(Math.log(N) / Math.log(2)) + 1);
}
 
// Function to find the number of
// jumps to escape the matrix from
// the given index [i, j]
static int countJumps(int Mat[][],
               int N, int M,
               int i, int j)
{
   
    // Initialize the variable Jump
    int C0, C1, Jump = 0;
 
    while (i < N && j < M) {
 
        // When the element is already visited
        // then a closed loop is formed and
        // it is impossible to escape then
        // return -1.
        if (Mat[i][j] == -1)
            return -1;
 
        // Calculate Count of 1 in Mat[i][j]
        C1 = brianKernighan(Mat[i][j]);
 
        // Calculate Count of 0 in Mat[i][j]
        C0 = getMsbIndex(Mat[i][j]) - C1;
 
        // Set the element Mat[i][j] visited
        Mat[i][j] = -1;
 
        // Set i and j to count if 0 and 1
        i = C0; j = C1;
 
        // Increment Jump by 1
        Jump++;
    }
 
    // Return number of Jumps to escape
    // the matrix if it is possible to
    // escape
    return Jump;
}
 
// Driver Code
public static void main (String[] args) {
         
    int N = 3, M = 4;
    int i = 0, j = 0;
    int Mat[][] = { { 5, 13, 8, 1 },
                       { 7, 9, 2, 15 },
                       { 12, 4, 8, 3 } };
 
    System.out.println(countJumps(Mat, N, M, i, j));
}
}
 
// This code is contributed by target_2.


Python3
# Python Program to implement
# the above approach
import math as Math
Max = 100
 
# Function to find total count of
# set bits in any number
def brianKernighan(N):
    Count = 0
    while (N):
        N = N & (N - 1)
        Count += 1
    return Count
 
# Function to find left most Set bit
# position in any number
def getMsbIndex(N):
    return Math.floor(Math.log2(N) + 1)
 
# Function to find the number of
# jumps to escape the matrix from
# the given index [i, j]
def countJumps(Mat, N, M, i, j):
   
    # Initialize the variable Jump
    Jump = 0
 
    while (i < N and j < M):
 
        # When the element is already visited
        # then a closed loop is formed and
        # it is impossible to escape then
        # return -1.
        if (Mat[i][j] == -1):
            return -1
 
        # Calculate Count of 1 in Mat[i][j]
        C1 = brianKernighan(Mat[i][j])
 
        # Calculate Count of 0 in Mat[i][j]
        C0 = getMsbIndex(Mat[i][j]) - C1
 
        # Set the element Mat[i][j] visited
        Mat[i][j] = -1
 
        # Set i and j to count if 0 and 1
        i = C0
        j = C1
 
        # Increment Jump by 1
        Jump += 1
 
    # Return number of Jumps to escape
    # the matrix if it is possible to
    # escape
    return Jump
 
# Driver Code
N = 3
M = 4
i = 0
j = 0
Mat = [[5, 13, 8, 1],
       [7, 9, 2, 15],
       [12, 4, 8, 3]]
 
print(countJumps(Mat, N, M, i, j))
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find total count of
// set bits in any number
static int brianKernighan(int N)
{
    int Count = 0;
    while (N != 0) {
        N = N & (N - 1);
        Count += 1;
    }
    return Count;
}
 
// Function to find left most Set bit
// position in any number
static int getMsbIndex(int N)
{
    return ((int)(Math.Log(N) / Math.Log(2)) + 1);
}
 
// Function to find the number of
// jumps to escape the matrix from
// the given index [i, j]
static int countJumps(int[,] Mat,
               int N, int M,
               int i, int j)
{
   
    // Initialize the variable Jump
    int C0, C1, Jump = 0;
 
    while (i < N && j < M) {
 
        // When the element is already visited
        // then a closed loop is formed and
        // it is impossible to escape then
        // return -1.
        if (Mat[i, j] == -1)
            return -1;
 
        // Calculate Count of 1 in Mat[i][j]
        C1 = brianKernighan(Mat[i, j]);
 
        // Calculate Count of 0 in Mat[i][j]
        C0 = getMsbIndex(Mat[i, j]) - C1;
 
        // Set the element Mat[i][j] visited
        Mat[i, j] = -1;
 
        // Set i and j to count if 0 and 1
        i = C0; j = C1;
 
        // Increment Jump by 1
        Jump++;
    }
 
    // Return number of Jumps to escape
    // the matrix if it is possible to
    // escape
    return Jump;
}
 
// Driver Code
public static void Main()
{
    int N = 3, M = 4;
    int i = 0, j = 0;
    int[,] Mat = {{ 5, 13, 8, 1 },
                       { 7, 9, 2, 15 },
                       { 12, 4, 8, 3 } };
 
    Console.Write(countJumps(Mat, N, M, i, j));
}
}
 
// This code is contributed by sanjoy_62.


Javascript



输出
4

时间复杂度:O(N * M * log(M * N))
辅助空间:O(1)