📜  用1填充整个矩阵所需的最短时间

📅  最后修改于: 2021-06-26 23:48:30             🧑  作者: Mango

给定一个大小为N的矩阵,该矩阵由01组成,任务是找到用1填充整个矩阵所需的最短时间。矩阵中的每个瞬时1可以将其八个相邻单元中的所有0转换为1 ,即(i,j)处的1可以将位置(i,j-1)的所有0转换为1 。 , (i,j + 1)(i-1,j)(i + 1,j)(i-1,j-1)(i-1,j + 1)(i + 1, j-1)(i + 1,j + 1)

例子:

方法:
为了解决这个问题,我们使用了BFS方法。遍历矩阵并将矩阵的索引初始存储为1。循环直到队列变空,并将所有队列元素的相邻有效单元格转换为1。导致至少从0转换为1的BFS遍历级别数是答案。

下面的代码是上述方法的实现:

C++
// C++ program to calculate the number of steps
// in fill all entire matrix with '1'
#include
using namespace std;
 
// Function to return total number
// of steps required to fill
// entire matrix with '1'
int numberOfSteps(int n,vector> mtrx)
{
    // Queue to store indices with 1
    queue> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mtrx[i][j] == 1) {
                q.push({i,j});
            }
        }
    }
     
    // Intialise step variable as zero
    int step = 0 ;
 
    // BFS approach
    while (!q.empty())
    {
        int qsize = q.size();
        // Visit all indices with 1 and
        // fill its neighbouring cells by 1
        while (qsize--)
        {
            pair p  = q.front();
            q.pop();
            int i = p.first;
            int j = p.second;
            // Convert the neighbour from '0' to '1'
            if((j > 0) && mtrx[i][j-1] == 0)
            {
                mtrx[i][j-1] = 1;
                q.push({i,j-1});
            }
            // Convert the neighbour from '0' to '1'
            if((i < n-1) && mtrx[i+1][j] == 0)
            {
                mtrx[i+1][j] = 1;
                q.push({i+1,j});
            }
            // Convert the neighbour from '0' to '1'
            if((j < n-1) && mtrx[i][j+1] == 0)
            {
                mtrx[i][j+1] = 1;
                q.push({i,j + 1});
            }
            // Convert the neighbour from '0' to '1'
            if((i > 0) && mtrx[i-1][j] == 0)
            {
                mtrx[i-1][j] = 1;
                q.push({i-1,j});
            }
            // Convert the neighbour from '0' to '1'
            if((i > 0) && (j > 0) &&
                                mtrx[i-1][j-1] == 0)
            {
                mtrx[i-1][j-1] = 1;
                q.push({i-1,j-1});
            }
            // Convert the neighbour from '0' to '1'
            if((i > 0) && (j < (n-1)) &&
                                mtrx[i-1][j+1] == 0)
            {
                mtrx[i-1][j+1] = 1;
                q.push({i-1,j+1});
            }
            // Convert the neighbour from '0' to '1'
            if((i < (n-1)) && (j < (n-1)) &&
                                mtrx[i+1][j+1] == 0)
            {
                mtrx[i+1][j+1] = 1;
                q.push({i+1,j + 1});
            }
            // Convert the neighbour from '0' to '1'
            if((i < (n-1)) && (j > 0) &&
                                mtrx[i+1][j-1] == 0)
            {
                mtrx[i+1][j-1] = 1;
                q.push({i+1,j-1});
            }
        }
        //count steps
        step++;
    }
     
    return step-1;
}
 
// Driver code
int main()
{
    int n = 5 ; //dimension of matrix NXN
    vector> mtrx = {{0,0,0,0,0},
                                {1,0,0,0,0},
                                {0,0,0,0,0},
                                {0,0,1,0,0},
                                {0,0,0,1,0}};
    // Print number of steps
    cout << numberOfSteps(n, mtrx);
 
    return 0;
}


Java
// Java program to calculate the
// number of steps in fill all
// entire matrix with '1'
import java.util.*;
 
class GFG{
     
static class pair
{
    int first, second;
     
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
 
// Function to return total number
// of steps required to fill
// entire matrix with '1'
static int numberOfSteps(int n, int[][] mtrx)
{
     
    // Queue to store indices with 1
    Queue q = new LinkedList<>();
     
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if (mtrx[i][j] == 1)
            {
                q.add(new pair(i, j));
            }
        }
    }
 
    // Intialise step variable as zero
    int step = 0;
 
    // BFS approach
    while (!q.isEmpty())
    {
        int qsize = q.size();
         
        // Visit all indices with 1 and
        // fill its neighbouring cells by 1
        while (qsize-- > 0)
        {
            pair p  = q.peek();
            q.remove();
             
            int i = p.first;
            int j = p.second;
             
            // Convert the neighbour from '0' to '1'
            if ((j > 0) && mtrx[i][j - 1] == 0)
            {
                mtrx[i][j - 1] = 1;
                q.add(new pair(i, j - 1));
            }
             
            // Convert the neighbour from '0' to '1'
            if ((i < n - 1) && mtrx[i + 1][j] == 0)
            {
                mtrx[i + 1][j] = 1;
                q.add(new pair(i + 1, j));
            }
             
            // Convert the neighbour from '0' to '1'
            if ((j < n - 1) && mtrx[i][j + 1] == 0)
            {
                mtrx[i][j + 1] = 1;
                q.add(new pair(i, j + 1));
            }
             
            // Convert the neighbour from '0' to '1'
            if ((i > 0) && mtrx[i - 1][j] == 0)
            {
                mtrx[i - 1][j] = 1;
                q.add(new pair(i - 1, j));
            }
             
            // Convert the neighbour from '0' to '1'
            if ((i > 0) && (j > 0) &&
                mtrx[i - 1][j - 1] == 0)
            {
                mtrx[i - 1][j - 1] = 1;
                q.add(new pair(i - 1, j - 1));
            }
             
            // Convert the neighbour from '0' to '1'
            if ((i > 0) && (j < (n - 1)) &&
                mtrx[i - 1][j + 1] == 0)
            {
                mtrx[i - 1][j + 1] = 1;
                q.add(new pair(i - 1, j + 1));
            }
             
            // Convert the neighbour from '0' to '1'
            if ((i < (n - 1)) && (j < (n - 1)) &&
                mtrx[i + 1][j + 1] == 0)
            {
                mtrx[i + 1][j + 1] = 1;
                q.add(new pair(i + 1, j + 1));
            }
             
            // Convert the neighbour from '0' to '1'
            if ((i < (n - 1)) && (j > 0) &&
                mtrx[i + 1][j - 1] == 0)
            {
                mtrx[i + 1][j - 1] = 1;
                q.add(new pair(i + 1, j - 1));
            }
        }
         
        // Count steps
        step++;
    }
    return step - 1;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Dimension of matrix NXN
    int n = 5;
    int [][]mtrx = { { 0, 0, 0, 0, 0 },
                     { 1, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0 },
                     { 0, 0, 1, 0, 0 },
                     { 0, 0, 0, 1, 0 } };
                      
    // Print number of steps
    System.out.print(numberOfSteps(n, mtrx));
}
}
 
// This code is contributed by Amit Katiyar


Python 3
# Python 3 program to calculate the number of steps
# in fill all entire matrix with '1'
 
# Function to return total number
# of steps required to fill
# entire matrix with '1'
def numberOfSteps(n,mtrx):
     
    # Queue to store indices with 1
    q = []
    for i in range(n):
        for j in range(n):
            if (mtrx[i][j] == 1):
                q.append([i,j])
     
    # Intialise step variable as zero
    step = 0
 
    # BFS approach
    while (len(q)):
        qsize = len(q)
         
        # Visit all indices with 1 and
        # fill its neighbouring cells by 1
        while(qsize):
            p = q[0]
            q.remove(q[0])
            i = p[0]
            j = p[1]
             
            # Convert the neighbour from '0' to '1'
            if((j > 0) and mtrx[i][j - 1] == 0):
                mtrx[i][j - 1] = 1
                q.append([i, j - 1])
                 
            # Convert the neighbour from '0' to '1'
            if((i < n-1) and mtrx[i + 1][j] == 0):
                mtrx[i + 1][j] = 1
                q.append([i + 1, j])
                 
            # Convert the neighbour from '0' to '1'
            if((j < n-1) and mtrx[i][j + 1] == 0):
                mtrx[i][j + 1] = 1
                q.append([i, j + 1])
                 
            # Convert the neighbour from '0' to '1'
            if((i > 0) and mtrx[i - 1][j] == 0):
                mtrx[i - 1][j] = 1
                q.append([i - 1, j])
                 
            # Convert the neighbour from '0' to '1'
            if((i > 0) and (j > 0) and
                                   mtrx[i - 1][j - 1] == 0):
                mtrx[i - 1][j - 1] = 1
                q.append([i - 1, j - 1])
                 
            # Convert the neighbour from '0' to '1'
            if((i > 0) and (j < (n-1)) and 
                                    mtrx[i - 1][j + 1] == 0):
                mtrx[i - 1][j + 1] = 1
                q.append([i - 1, j + 1])
                 
            # Convert the neighbour from '0' to '1'
            if((i < (n - 1)) and (j < (n - 1)) and
                                     mtrx[i + 1][j + 1] == 0):
                mtrx[i + 1][j + 1] = 1
                q.append([i + 1, j + 1])
                 
            # Convert the neighbour from '0' to '1'
            if((i < (n - 1)) and (j > 0) and
                                    mtrx[i + 1][j - 1] == 0):
                mtrx[i + 1][j - 1] = 1
                q.append([i + 1,j - 1])
             
            qsize -= 1
             
        #count steps
        step += 1
     
    return step-1
 
# Driver code
if __name__ == '__main__':
     
    #dimension of matrix NXN
    n = 5
    mtrx = [[ 0, 0, 0, 0, 0 ],
            [ 1, 0, 0, 0, 0 ],
            [ 0, 0, 0, 0, 0 ],
            [ 0, 0, 1, 0, 0 ],
            [ 0, 0, 0, 1, 0 ]]
             
    # Print number of steps
    print(numberOfSteps(n, mtrx))
     
# This code is contributed by Samarth


C#
// C# program to calculate the
// number of steps in fill all
// entire matrix with '1'
using System;
using System.Collections.Generic;
class GFG{
     
public class pair
{
  public int first, second;
  public pair(int first, int second) 
  {
    this.first = first;
    this.second = second;
  }   
}
 
// Function to return total number
// of steps required to fill
// entire matrix with '1'
static int numberOfSteps(int n,
                         int[,] mtrx)
{
  // Queue to store indices with 1
  Queue q = new Queue();
 
  for(int i = 0; i < n; i++)
  {
    for(int j = 0; j < n; j++)
    {
      if (mtrx[i, j] == 1)
      {
        q.Enqueue(new pair(i, j));
      }
    }
  }
 
  // Intialise step variable as zero
  int step = 0;
 
  // BFS approach
  while (q.Count != 0)
  {
    int qsize = q.Count;
 
    // Visit all indices
    // with 1 and fill
    // its neighbouring
    // cells by 1
    while (qsize-- > 0)
    {
      pair p  = q.Peek();
      q.Dequeue();
      int i = p.first;
      int j = p.second;
 
      // Convert the neighbour
      // from '0' to '1'
      if ((j > 0) &&
           mtrx[i, j - 1] == 0)
      {
        mtrx[i, j - 1] = 1;
        q.Enqueue(new pair(i, j - 1));
      }
 
      // Convert the neighbour
      // from '0' to '1'
      if ((i < n - 1) &&
           mtrx[i + 1, j] == 0)
      {
        mtrx[i + 1, j] = 1;
        q.Enqueue(new pair(i + 1, j));
      }
 
      // Convert the neighbour
      // from '0' to '1'
      if ((j < n - 1) &&
           mtrx[i, j + 1] == 0)
      {
        mtrx[i, j + 1] = 1;
        q.Enqueue(new pair(i, j + 1));
      }
 
      // Convert the neighbour
      // from '0' to '1'
      if ((i > 0) &&
           mtrx[i - 1, j] == 0)
      {
        mtrx[i - 1, j] = 1;
        q.Enqueue(new pair(i - 1, j));
      }
 
      // Convert the neighbour
      // from '0' to '1'
      if ((i > 0) && (j > 0) &&
           mtrx[i - 1, j - 1] == 0)
      {
        mtrx[i - 1, j - 1] = 1;
        q.Enqueue(new pair(i - 1, j - 1));
      }
 
      // Convert the neighbour
      // from '0' to '1'
      if ((i > 0) && (j < (n - 1)) &&
           mtrx[i - 1, j + 1] == 0)
      {
        mtrx[i - 1, j + 1] = 1;
        q.Enqueue(new pair(i - 1, j + 1));
      }
 
      // Convert the neighbour
      // from '0' to '1'
      if ((i < (n - 1)) && (j < (n - 1)) &&
           mtrx[i + 1, j + 1] == 0)
      {
        mtrx[i + 1, j + 1] = 1;
        q.Enqueue(new pair(i + 1, j + 1));
      }
 
      // Convert the neighbour
      // from '0' to '1'
      if ((i < (n - 1)) && (j > 0) &&
           mtrx[i + 1, j - 1] == 0)
      {
        mtrx[i + 1, j - 1] = 1;
        q.Enqueue(new pair(i + 1, j - 1));
      }
    }
 
    // Count steps
    step++;
  }
  return step - 1;
}
 
// Driver code
public static void Main(String[] args)
{
  // Dimension of matrix NXN
  int n = 5;
  int [,]mtrx = {{0, 0, 0, 0, 0},
                 {1, 0, 0, 0, 0},
                 {0, 0, 0, 0, 0},
                 {0, 0, 1, 0, 0},
                 {0, 0, 0, 1, 0}};
 
  // Print number of steps
  Console.Write(numberOfSteps(n, mtrx));
}
}
 
// This code is contributed by Rajput-Ji


输出:
3




如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。