Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spiral Matrix logic implemented in java
//spiral matrix method is used
import java.io.*;
import java.util.Scanner;
public class BrainTeaser{
static int[] array = new int[200];
static int userchoice;
//brainFunction called here
public static void brainTeaser(int number,int n,int[][] initalarr){
//matrix array declaration
int[][] matrixarray = new int[n][n];
//initially fill 0
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
initalarr[i][j] = 0;
}
}
//fillspiral function is called
fillspiral(initalarr,0,0,n,n*n,number,matrixarray,'R');//initially the direction starts from right
}
//fillspiral function called
//global declaration
static void fillspiral(int[][] initalarr,int x,int y,int n,int k,int number,int[][] matrixarray,char dir){
//to check the condtion if the matrixarray is filled
if(x<0 || x>=n || y<0 || y>=n || matrixarray[x][y]==1)
return ;
//condition to check if number is greater
if(k > number){
initalarr[x][y] = 0;
}else{
initalarr[x][y] = number;
}
//switch condtion
matrixarray[x][y] = 1;//to check if the array is processed or not
//to check if number is greater or not
if(number > k)
initalarr[x][y] = 0;
else
initalarr[x][y] = number;
//switch case starts here
switch(dir){
case 'R':
fillspiral(initalarr,x,y+1,n,k,number-1,matrixarray,'R');
fillspiral(initalarr,x+1,y,n,k,number-1,matrixarray,'D');
fillspiral(initalarr,x,y-1,n,k,number-1,matrixarray,'L');
fillspiral(initalarr,x-1,y,n,k,number-1,matrixarray,'U');
break;
case 'D':
fillspiral(initalarr,x+1,y,n,k,number-1,matrixarray,'D');
fillspiral(initalarr,x,y-1,n,k,number-1,matrixarray,'L');
fillspiral(initalarr,x-1,y,n,k,number-1,matrixarray,'U');
fillspiral(initalarr,x,y+1,n,k,number-1,matrixarray,'R');
break;
case 'U':
fillspiral(initalarr,x-1,y,n,k,number-1,matrixarray,'U');
fillspiral(initalarr,x,y+1,n,k,number-1,matrixarray,'R');
fillspiral(initalarr,x+1,y,n,k,number-1,matrixarray,'D');
fillspiral(initalarr,x,y-1,n,k,number-1,matrixarray,'L');
break;
case 'L':
fillspiral(initalarr,x,y-1,n,k,number-1,matrixarray,'L');
fillspiral(initalarr,x-1,y,n,k,number-1,matrixarray,'U');
fillspiral(initalarr,x,y+1,n,k,number-1,matrixarray,'R');
fillspiral(initalarr,x+1,y,n,k,number-1,matrixarray,'D');
}
}
//printmatrix function called
static void printmatrix(int[][] initalarr,int n){
for(int x=0;x<n;x++){
for(int y=0;y<n;y++){
if(initalarr[x][y] == 0);
System.out.print(initalarr[x][y] + "\t");
}
System.out.println();
}
}
//main function
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Odd number:(Odd number only)");
userchoice = sc.nextInt();
System.out.println("Matrix will printed like");
System.out.println("-------------------------");
//condition to check if the number is odd or not
if((userchoice & 1) == 0){
System.out.println("Entered number is not an Odd number");
}
else{
double val = Math.ceil(Math.sqrt(userchoice));
int n = (int)val;
//array declaration
int[][] initalarr = new int[n][n];
//brainTeaser function called here
brainTeaser(userchoice,n,initalarr);
//print the matrix format of the array
printmatrix(initalarr,n);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment