The Wayback Machine - https://web.archive.org/web/20220302223552/https://www.geeksforgeeks.org/minimum-number-of-water-to-land-conversion-to-make-two-islands-connected-in-a-grid-set-2/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum number of Water to Land conversion to make two islands connected in a Grid | Set 2

  • Last Updated : 22 Feb, 2022

Given a 2D grid arr[][] of ‘W’ and ‘L’ where ‘W’ denotes water and ‘L’ denotes land, the task is to find the minimum number of water components ‘W’ that must be changed to land component ‘L’ so that two islands becomes connected.

An island is the set of connected ‘L’s

Note: There can be only two disjoint islands.

Examples:

Input: arr[][] = {{‘W’, ‘L’}, {‘L’, ‘W’}}; 
Output: 1 
Explanation: For the given set of islands if we change arr[1][1] to ‘W’ 
then, set of all island are connected. 
Therefore, the minimum number of ‘W’ must be changed to ‘L’ is 1.

Input: arr[][] = {{‘W’, ‘L’, ‘W’}, {‘W’, ‘W’, ‘W’}, {‘W’, ‘W’, ‘L’}} 
Output: 2

 

Approach based on Floodfill algorithm: The approach based on Floodfill algorithm is discussed in Set-1 of this article.

Efficient Approach: This problem can be solved by using DFS and BFS algorithms. The idea is to use DFS to find all the land components of one island and simultaneously adding the bordering land components to queue which will be used in BFS to expand and find the shortest path to second island. Follow the steps mentioned below to solve the problem:

  • Use a nested loop to find the first occurrence of ‘L’ in arr[][].
  • Call DFS to find all the elements of this island and if any ‘L’  is a border element (i.e. surrounded by ‘W’ on at least one side) also add it in a queue.
  • Expand this island using BFS algorithm using the queue and visited array created during DFS call.
    • At each level increase the distance by 1.
    • By using BFS find the smallest path from border of one island to second island.
    • If the next component to be added in the queue is ‘L’, return the distance up till now.

Note: All elements of the first island can also be found using the BFS algorithm also.

Below is the implementation of the above approach:

Java




// Java program to implement the approach
import java.util.*;
 
class GFG {
    static int R;
    static int C;
 
    // A class to represent point and
    // dist from first island
    static class qNode {
        int x;
        int y;
        int dist;
        qNode(int x, int y, int d)
        {
            this.x = x;
            this.y = y;
            this.dist = d;
        }
    }
 
    // Function to find minimum conversions
    static int minConversions(char[][] arr)
    {
        R = arr.length;
        C = arr[0].length;
 
        // Queue to be used in bfs
        Queue<qNode> q = new ArrayDeque<>();
        boolean[][] visited
            = new boolean[R][C];
        boolean flag = false;
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (arr[i][j] == 'L') {
 
                    // Visited first island
                    // completely and at
                    // same time maintaining
                    // visited array and queue
                    dfs(i, j, visited, q, arr);
                    flag = true;
                    break;
                }
            }
 
            // Breaking the nested loop
            // once first island found
            if (flag)
                break;
        }
 
        return bfs(q, visited, arr);
    }
 
    // Arrays to get the adjacent indices
    // from one index of thegrid
    static int[] dirx = { 0, 1, 0, -1 };
    static int[] diry = { 1, 0, -1, 0 };
 
    // Function to run DFS
    static void dfs(int i, int j,
                    boolean[][] visited,
                    Queue<qNode> q,
                    char[][] arr)
    {
        visited[i][j] = true;
 
        // Checking if it is border component
        if (isBorder(i, j, arr)) {
            q.add(new qNode(i, j, 0));
        }
 
        // Calling dfs in all 4 directions
        for (int idx = 0; idx < 4; idx++) {
            int x = i + dirx[idx];
            int y = j + diry[idx];
 
            if (isValid(x, y)
                && arr[x][y] == 'L'
                && !visited[x][y]) {
                dfs(x, y, visited, q, arr);
            }
        }
    }
 
    // Return true if surrounded by water
    // in any of 4 direction
    static boolean isBorder(int i, int j,
                            char[][] arr)
    {
        for (int idx = 0; idx < 4; idx++) {
            int x = i + dirx[idx];
            int y = j + diry[idx];
            if (isValid(x, y)
                && arr[x][y] == 'W')
                return true;
        }
        return false;
    }
 
    // Function to implement BFS
    static int bfs(Queue<qNode> q,
                   boolean[][] visited,
                   char[][] arr)
    {
 
        while (q.size() > 0) {
            qNode p = q.remove();
 
            for (int idx = 0; idx < 4;
                 idx++) {
                int x = p.x + dirx[idx];
                int y = p.y + diry[idx];
 
                // If next unvisited component
                // is land, return dist
                // till of p only
                if (isValid(x, y)
                    && arr[x][y] == 'L'
                    && !visited[x][y]) {
                    return p.dist;
                }
                if (isValid(x, y)
                    && arr[x][y] == 'W'
                    && !visited[x][y]) {
 
                    q.add(new qNode(x, y,
                                    p.dist + 1));
                    visited[x][y] = true;
                }
            }
        }
        return -1;
    }
 
    // To check if indices are in matrix
    static boolean isValid(int x, int y)
    {
        if (x < 0 || y < 0
            || x >= R || y >= C)
            return false;
        return true;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        char[][] arr = { { 'W', 'L', 'W' },
                         { 'W', 'W', 'W' },
                         { 'W', 'W', 'L' } };
 
        // Function call
        int ans = minConversions(arr);
        System.out.println(ans);
    }
}

C#




// C# program to implement the approach
using System;
using System.Collections.Generic;
 
public class GFG {
  static int R;
  static int C;
 
  // A class to represent point and
  // dist from first island
  class qNode {
    public int x;
    public int y;
    public int dist;
    public qNode(int x, int y, int d)
    {
      this.x = x;
      this.y = y;
      this.dist = d;
    }
  }
 
  // Function to find minimum conversions
  static int minConversions(char[,] arr)
  {
    R = arr.Length;
    C = arr.GetLength(0);
 
    // Queue to be used in bfs
    Queue<qNode> q = new Queue<qNode>();
    bool[,] visited
      = new bool[R,C];
    bool flag = false;
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        if (arr[i,j] == 'L') {
 
          // Visited first island
          // completely and at
          // same time maintaining
          // visited array and queue
          dfs(i, j, visited, q, arr);
          flag = true;
          break;
        }
      }
 
      // Breaking the nested loop
      // once first island found
      if (flag)
        break;
    }
 
    return bfs(q, visited, arr);
  }
 
  // Arrays to get the adjacent indices
  // from one index of thegrid
  static int[] dirx = { 0, 1, 0, -1 };
  static int[] diry = { 1, 0, -1, 0 };
 
  // Function to run DFS
  static void dfs(int i, int j,
                  bool[,] visited,
                  Queue<qNode> q,
                  char[,] arr)
  {
    visited[i,j] = true;
 
    // Checking if it is border component
    if (isBorder(i, j, arr)) {
      q.Enqueue(new qNode(i, j, 0));
    }
 
    // Calling dfs in all 4 directions
    for (int idx = 0; idx < 4; idx++) {
      int x = i + dirx[idx];
      int y = j + diry[idx];
 
      if (isValid(x, y)
          && arr[x,y] == 'L'
          && !visited[x,y]) {
        dfs(x, y, visited, q, arr);
      }
    }
  }
 
  // Return true if surrounded by water
  // in any of 4 direction
  static bool isBorder(int i, int j,
                       char[,] arr)
  {
    for (int idx = 0; idx < 4; idx++) {
      int x = i + dirx[idx];
      int y = j + diry[idx];
      if (isValid(x, y)
          && arr[x,y] == 'W')
        return true;
    }
    return false;
  }
 
  // Function to implement BFS
  static int bfs(Queue<qNode> q,
                 bool[,] visited,
                 char[,] arr)
  {
 
    while (q.Count > 0) {
      qNode p = q.Dequeue();
 
      for (int idx = 0; idx < 4;
           idx++) {
        int x = p.x + dirx[idx];
        int y = p.y + diry[idx];
 
        // If next unvisited component
        // is land, return dist
        // till of p only
        if (isValid(x, y)
            && arr[x,y] == 'L'
            && !visited[x,y]) {
          return p.dist;
        }
        if (isValid(x, y)
            && arr[x,y] == 'W'
            && !visited[x,y]) {
 
          q.Enqueue(new qNode(x, y,
                              p.dist + 1));
          visited[x,y] = true;
        }
      }
    }
    return -1;
  }
 
  // To check if indices are in matrix
  static bool isValid(int x, int y)
  {
    if (x < 0 || y < 0
        || x >= R || y >= C)
      return false;
    return true;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    char[,] arr = { { 'W', 'L', 'W' },
                   { 'W', 'W', 'W' },
                   { 'W', 'W', 'L' } };
 
    // Function call
    int ans = minConversions(arr);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by shikhasingrajput

 
 

Output
2

 

Time Complexity: O(N2)
Auxiliary Space: O(N2)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!