[Solved] Full Islands with Java, C++

Full Islands: Nurikabe logical game (sometimes called Islands in the Stream) is a binary determination puzzle. The puzzle is played on a typically rectangular grid of cells, some of which contain numbers. You must decide for each cell if it is white or black (by clicking on them) according to the following rules:

  • All of the black cells must be connected.
  • Each numbered cell must be part of a white island of connected white cells.
  • Each island must have the same number of white cells as the number it contains (including the numbered cell).
  • Two islands may not be connected.
  • There cannot be any 2×2 blocks of black cells.

Unnumbered cells start out grey and cycle through white and black when clicked. Initially numbered cells are white in color.

Problem Statement:
The step 1 of solving the puzzle is identifying “Full islands”.
An island is full if it contains as many white cells as the number in the region. Any 1s are trivially full regions. When you encounter a full region, any cells that boarder it must be black. Here we show the cells that must be black due to a single celled white island.

[Solved] Full Islands with Java, C++

Write a program that when given the initial board configuration will identify the full islands.

Input Format:
First line of the input is an integer N that gives the number of rows and columns of the grid.
Next N lines will have a valid initial board configuration with N*N cells. Assume that the maximum number in a cell can be 10. Grey colored cells are represented by the integer 20 in the matrix representation of the input configuration.

Output Format:
Output should display the board configuration with N*N cells after applying step 1. Grey colored cells are represented by the integer 20, numbered cells are represented by the same number given in the input and black cells are represented by 0.
Refer sample input and output for formatting specifications.

Sample Input 1:
5
20 20 1 20 3
20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
6 20 3 20 20

Sample Output 1:
20 0 1 0 3
20 20 0 20 20
20 20 20 20 20
20 20 20 20 20
6 20 3 20 20

Sample Input 2:
5
20 20 20 20 20
3 20 20 6 20
20 20 20 20 20
20 2 20 20 1
20 20 20 20 20

Sample Output 2:
20 20 20 20 20
3 20 20 6 20
20 20 20 20 0
20 2 20 0 1
20 20 20 20 0

Solution

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int i,j,n;
        int[][] a=new int[50][50];
        n=sc.nextInt();
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                //sauravhathi
                a[i][j]=sc.nextInt();
            }
        }
        
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(a[i][j]==1)
                {
                     //sauravhathi
                    if(i==0 && j==0)
                    {
                    
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                    
                    }
                    else if(i==0 && j==n)
                    {
                    
                    a[i+1][j]=0;
                    
                    a[i][j-1]=0;
                    }
                    else if(i==n && j==0)
                    {
                    a[i-1][j]=0;
                    
                    a[i][j+1]=0;
                    
                    }
                    else if(i==n && j==n)
                    {
                    a[i-1][j]=0;
                    
                    
                    a[i][j-1]=0;
                    }
                    else if(i==0)
                    {
                    
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                    a[i][j-1]=0;
                    }
                    else if(i==n)
                    {
                    a[i-1][j]=0;
                    
                    a[i][j+1]=0;
                    a[i][j-1]=0;
                    }
                    else if(j==0)
                    {
                    a[i-1][j]=0;
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                    
                    }
                    else if(j==n)
                    {
                    a[i-1][j]=0;
                    a[i+1][j]=0;
                    
                    a[i][j-1]=0;
                    }
                    else 
                    {
                    a[i-1][j]=0;
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                    a[i][j-1]=0;
                    }
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }
}
#include <iostream>
using namespace std;

int main() {
    int i,j,n;
    cin>>n;
    int[][] a=new int[50][50];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin>>a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(a[i][j]==1)
            {
                if(i==0&&j==0)
                {
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                }
                else if(i==0&&j==n)
                {
                    a[i+1][j]=0;
                    a[i][j-1]=0;
                }
                else if(i==n&&j==0)
                {
                    a[i-1][j]=0;
                    a[i][j+1]=0;
                }
                else if(i==n&&j==n)
                {
                    a[i-1][j]=0;
                    a[i][j-1]=0;
                }
                else if(i==0)
                {
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                    a[i][j-1]=0;
                }
                //saurachathi
                else if(i==n)
                {
                    a[i-1][j]=0;
                    a[i][j+1]=0;
                    a[i][j-1]=0;
                }
                else if(j==0)
                {
                    a[i-1][j]=0;
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                }
                else if(j==n)
                {
                    a[i-1][j]=0;
                    a[i+1][j]=0;
                    a[i][j-1]=0;
                }
                else
                {
                    a[i-1][j]=0;
                    a[i+1][j]=0;
                    a[i][j+1]=0;
                    a[i][j-1]=0;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            //sauravathi
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *