2-D Arrays
S7P1 - VALID INITIAL CONFIGURATION
Valid Initial Configuration
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 2x2 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 check whether the given board configuration is a valid initial configuration. Below figure is the sample valid initial configuration.

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 the 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 "Yes" (without quotes) if the given configuration is a valid initial configuration. Print "No" otherwise (without quotes).
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:
Yes
Sample Input 2:
5
20 20 1 20 3
20 20 20 20 20
20 20 12 20 20
20 20 20 20 20
6 20 3 20 20
Sample Output 2:
No
Valid Initial Configuration
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 2x2 blocks of black cells.
Problem Statement:
Write a program to check whether the given board configuration is a valid initial configuration. Below figure is the sample valid initial configuration.

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 the 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 "Yes" (without quotes) if the given configuration is a valid initial configuration. Print "No" otherwise (without quotes).
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:
Yes
Sample Input 2:
5
20 20 1 20 3
20 20 20 20 20
20 20 12 20 20
20 20 20 20 20
6 20 3 20 20
Sample Output 2:
No
Solution:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n,i,j;
int[][] a=new int[50][50];
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>10 && a[i][j]!=20)
{
System.out.println("No");
return;
}
}
}
System.out.println("Yes");
}
}
S7P2 - COUNT OF NUMBERED CELLS
Count of Numbered Cells
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 2x2 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.

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
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.

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;
public 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++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=20)
{
count++;
}
}
}
System.out.println(count);
}
}
S7P3 - NUMBER OF WHITE CELLS
Number of White Cells
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 2x2 blocks of black cells.
Problem Statement:
Write a program to find the number of white cells in the final configuration of the board, given a valid initial configuration. Below figure is the sample valid initial configuration.

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 the number of white cells in the final configuration of the board.
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:
13
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:
12
Solution:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int i,j,n,sum=0;
int[][] a=new int[50][50];
n=sc.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=20)
sum+=a[i][j];
}
}
System.out.println(sum);
}
}
S7P5 - FULL ISLANDS
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 2x2 blocks of black cells.
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.

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;
public 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++)
{
a[i][j]=sc.nextInt();
}
}
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;
}
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();
}
}
}
Identify neighbour numbers in nurikabe logical game
ReplyDeleteIdentify neighbour numbers in nurikabe logical game
ReplyDelete