[Solved] Count of Numbered Cells with Java, C++

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:
Write a program to find the count of numbered cells, given a valid initial board configuration. Below figure is the sample valid initial configuration.

[Solved] Count of Numbered Cells with Java, C++

 
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 an integer that gives the count of numbered cells, given a valid initial board configuration.
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:
4

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

Sample Output 2:
10

Solution

import java.util.Scanner;
 class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n,i,j,count=0;
        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++)
            {
                //sauravhathi
                if(a[i][j]!=20)
                {
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}
#include <iostream>
using namespace std;
int main()
{
    int n, i, j, count = 0;
    cin>>n;
    int a[n][n];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin>>a[i][j];
            // sauravhathi
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            // sauravhathi
            if(a[i][j]!=20)
            {
                count++;
            }
        }
    }
    cout<<count;
    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 *