📜  高程的最大可能高度,使得相邻矩阵单元的高度差最多为 1

📅  最后修改于: 2021-09-07 02:59:12             🧑  作者: Mango

给定一个大小为M x N的矩阵mat][][]表示一个区域的地形图, 0表示土地, 1表示高程,任务是通过为每个单元分配一个非负值来最大化矩阵中的高度height 使得陆地单元的高度为0,并且两个相邻单元的绝对高度差最多为 1。

例子:

方法:想法是使用BFS。请按照以下步骤解决问题:

  • 初始化一个二维数组,高度M x N以存储最终输出矩阵。
  • 初始化一个对队列 queue>q来存储 BFS 的对索引。
  • 遍历矩阵并将land cell的高度标记为0并将它们入队,也将它们标记为已访问。
  • 执行 BFS:
    • 从队列中取出一个单元格并检查其所有 4 个相邻单元格,如果其中任何一个未被访问,则将该单元格的高度标记为 1 + 当前单元格的高度。
    • 将所有未访问的相邻单元格标记为已访问。
    • 重复此操作,除非队列变空。
  • 打印最终的高度矩阵。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
#define M 3
#define N 3
 
// Utility function to find the matrix
// having the maximum height
void findHeightMatrixUtil(int mat[][N],
                          int height[M][N])
{
    // Stores index pairs for bfs
    queue > q;
 
    // Stores info about the visited cells
    int vis[M][N] = { 0 };
 
    // Traverse the matrix
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            if (mat[i][j] == 1) {
                q.push({ i, j });
                height[i][j] = 0;
                vis[i][j] = 1;
            }
        }
    }
 
    // Breadth First Search
    while (q.empty() == 0) {
 
        pair k = q.front();
        q.pop();
 
        // x & y are the row & coloumn
        // of current cell
        int x = k.first, y = k.second;
 
        // Check all the 4 adjacent cells
        // and marking them as visited
        // if not visited yet also marking
        // their height as 1 + height of cell (x, y)
 
        if (x > 0 && vis[x - 1][y] == 0) {
            height[x - 1][y] = height[x][y] + 1;
            vis[x - 1][y] = 1;
            q.push({ x - 1, y });
        }
 
        if (y > 0 && vis[x][y - 1] == 0) {
            height[x][y - 1] = height[x][y] + 1;
            vis[x][y - 1] = 1;
            q.push({ x, y - 1 });
        }
 
        if (x < M - 1 && vis[x + 1][y] == 0) {
            height[x + 1][y] = height[x][y] + 1;
            vis[x + 1][y] = 1;
            q.push({ x + 1, y });
        }
 
        if (y < N - 1 && vis[x][y + 1] == 0) {
            height[x][y + 1] = height[x][y] + 1;
            vis[x][y + 1] = 1;
            q.push({ x, y + 1 });
        }
    }
}
 
// Function to find the matrix having
// the maximum height
void findHeightMatrix(int mat[][N])
{
    // Stores output matrix
    int height[M][N];
 
    // Calling the helper function
    findHeightMatrixUtil(mat, height);
 
    // Print the final output matrix
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++)
            cout << height[i][j] << " ";
 
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // Given matrix
    int mat[][N]
        = { { 0, 0 }, { 0, 1 } };
 
    // Function call to find
    // the matrix having
    // the maximum height
    findHeightMatrix(mat);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG{
 
static final int M = 3;
static final int N = 3;
static class pair
{
    int first, second;
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }
}
   
// Utility function to find the matrix
// having the maximum height
static void findHeightMatrixUtil(int mat[][],
                          int height[][])
{
    // Stores index pairs for bfs
    Queue q = new LinkedList<>();
 
    // Stores info about the visited cells
    int [][]vis = new int[M][N];
 
    // Traverse the matrix
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            if (mat[i][j] == 1) {
                q.add(new pair( i, j ));
                height[i][j] = 0;
                vis[i][j] = 1;
            }
        }
    }
 
    // Breadth First Search
    while (q.isEmpty() == false) {
 
        pair k = q.peek();
        q.remove();
 
        // x & y are the row & coloumn
        // of current cell
        int x = k.first, y = k.second;
 
        // Check all the 4 adjacent cells
        // and marking them as visited
        // if not visited yet also marking
        // their height as 1 + height of cell (x, y)
 
        if (x > 0 && vis[x - 1][y] == 0) {
            height[x - 1][y] = height[x][y] + 1;
            vis[x - 1][y] = 1;
            q.add(new pair( x - 1, y ));
        }
 
        if (y > 0 && vis[x][y - 1] == 0) {
            height[x][y - 1] = height[x][y] + 1;
            vis[x][y - 1] = 1;
            q.add(new pair( x, y - 1 ));
        }
 
        if (x < M - 1 && vis[x + 1][y] == 0) {
            height[x + 1][y] = height[x][y] + 1;
            vis[x + 1][y] = 1;
            q.add(new pair( x + 1, y ));
        }
 
        if (y < N - 1 && vis[x][y + 1] == 0) {
            height[x][y + 1] = height[x][y] + 1;
            vis[x][y + 1] = 1;
            q.add(new pair( x, y + 1 ));
        }
    }
}
 
// Function to find the matrix having
// the maximum height
static void findHeightMatrix(int mat[][])
{
    // Stores output matrix
    int [][]height = new int[M][N];
 
    // Calling the helper function
    findHeightMatrixUtil(mat, height);
 
    // Print the final output matrix
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++)
            System.out.print(height[i][j]+ " ");
 
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
    // Given matrix
    int mat[][]
        = { { 0, 0,0 }, { 0, 1,0 },{ 0, 0,0 } };
 
    // Function call to find
    // the matrix having
    // the maximum height
    findHeightMatrix(mat);
 
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
static readonly int M = 3;
static readonly int N = 3;
class pair
{
    public int first, second;
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }
}
   
// Utility function to find the matrix
// having the maximum height
static void findHeightMatrixUtil(int [,]mat,
                          int [,]height)
{
   
    // Stores index pairs for bfs
    Queue q = new Queue();
 
    // Stores info about the visited cells
    int [,]vis = new int[M,N];
 
    // Traverse the matrix
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            if (mat[i,j] == 1) {
                q.Enqueue(new pair( i, j ));
                height[i,j] = 0;
                vis[i,j] = 1;
            }
        }
    }
 
    // Breadth First Search
    while (q.Count != 0 )
    {
 
        pair k = q.Peek();
        q.Dequeue();
 
        // x & y are the row & coloumn
        // of current cell
        int x = k.first, y = k.second;
 
        // Check all the 4 adjacent cells
        // and marking them as visited
        // if not visited yet also marking
        // their height as 1 + height of cell (x, y)
 
        if (x > 0 && vis[x - 1, y] == 0) {
            height[x - 1, y] = height[x, y] + 1;
            vis[x - 1, y] = 1;
            q.Enqueue(new pair( x - 1, y ));
        }
 
        if (y > 0 && vis[x, y - 1] == 0) {
            height[x, y - 1] = height[x, y] + 1;
            vis[x, y - 1] = 1;
            q.Enqueue(new pair( x, y - 1 ));
        }
 
        if (x < M - 1 && vis[x + 1, y] == 0) {
            height[x + 1, y] = height[x, y] + 1;
            vis[x + 1, y] = 1;
            q.Enqueue(new pair( x + 1, y ));
        }
 
        if (y < N - 1 && vis[x, y + 1] == 0) {
            height[x, y + 1] = height[x, y] + 1;
            vis[x, y + 1] = 1;
            q.Enqueue(new pair( x, y + 1 ));
        }
    }
}
 
// Function to find the matrix having
// the maximum height
static void findHeightMatrix(int [,]mat)
{
    // Stores output matrix
    int [,]height = new int[M, N];
 
    // Calling the helper function
    findHeightMatrixUtil(mat, height);
 
    // Print the readonly output matrix
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++)
            Console.Write(height[i,j]+ " ");
 
        Console.WriteLine();
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given matrix
    int [,]mat
        = { { 0, 0,0 }, { 0, 1,0 },{ 0, 0,0 } };
 
    // Function call to find
    // the matrix having
    // the maximum height
    findHeightMatrix(mat);
 
}
}
 
// This code is contributed by 29AjayKumar


输出:
2 1 2 
1 0 1 
2 1 2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live