The Programming Project: Spiral
Showing posts with label Spiral. Show all posts
Showing posts with label Spiral. Show all posts

Friday, August 1, 2014

ISC COMPUTER SCIENCE PRACTICAL 1998 SPIRAL MATRIX using recursion in JAVA, Python and C++


Spiral Matrix
Write a program which takes N as input, where N is the order of an square matrix. Generate the spiral matrix of order N.
INPUT:
N = 1
OUTPUT:
1

INPUT:
N=2
OUTPUT
1 2
4 3

INPUT:
N=3
OUTPUT
1 2 3
8 9 4
7 6 5

INPUT:
N=4
OUTPUT
1     2    3    4
12  13  14   5
11  16  15   6
10  9    8     7

INPUT:
N=7
OUTPUT
1   2    3   4  5   6   7
24 25 26 27 28 29 8
23 40 41 42 43 30 9
22 39 48 49 44 31 10
21 38 47 46 45 32 11
20 37 36 35 34 33 12
19 18 17 16 15 14 13

Logic: Go through the code!!!!!! All the best!

JAVA CODE

import java.util.*;

public class ISC1998Spiral {
    public static void main(String[] args) {
        int N, val = 0, row, col, ri, ci;
        int i, j;
        int[][] matrix;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the order of the matrix:");
        N = in.nextInt();
        matrix = new int[N + 1][N + 1];
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                matrix[i][j] = 1;
            }
        }
        row = col = 1;
        ri = (N - (N - 2));
        ci = (N - (N - 1));
        SpiralGeneration sp = new SpiralGeneration();
        System.out.println("OUTPUT:");
        sp.spiral(matrix, N, row, col, val, ri, ci);
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                String s = String.valueOf(matrix[i][j]);
                while (s.length() < 4) {
                    s += " ";
                }
                System.out.print(s);
            }
            System.out.println();
        }
    }
}

class SpiralGeneration {
    public void spiral(int[][] matrix, int N, int row, int col, int val, int ri, int ci) {
        int a, b, tmp, i, j, l = 0;
        a = row;
        b = col;
        tmp = val + 1;
        if (N == 1 || N == 2) // stopping condition
        {
            if (N == 1) {
                matrix[row][col] = tmp;
            } else {
                matrix[row][col] = tmp;
                matrix[row][col + 1] = tmp + 1;
                matrix[row + 1][col + 1] = tmp + 2;
                matrix[row + 1][col] = tmp + 3;
            }
        } else {
            for (b = 1; b <= N; b++) {
                matrix[row][b - 1 + col] = tmp++;
                if (row != b - 1 + col)
                    matrix[b - 1 + col][row] = (4 * N - 2) - (matrix[row][b - 1 + col] - val) + val;
            }
            b--;
            for (a = 1; a <= N - 1; a++) {
                matrix[a + row][b - 1 + col] = tmp++;
                if (a + row != b - 1 + col)
                    matrix[b - 1 + col][a + row] = (4 * N - 2) - (matrix[a + row][b - 1 + col] - val) + val;
            }
            val = matrix[ri][ci];
            spiral(matrix, N - 2, ++row, ++col, val, ++ri, ++ci);
        }
    }
}




C code

#include<stdio.h>
#include<malloc.h>
void spiral(int **matrix,int N, int row, int col, int val,int ri,int ci);
int main() {
    int N,val=0,row,col,ri,ci;
    int i,j;
    int **matrix;
    printf("\n Enter the order of the matrix:");
    scanf("%d",&N);
    matrix=(int **)malloc((N+1)*sizeof(int*));
        for(i=0;i<=N;i++)
            matrix[i]=(int *)malloc((N+1)*sizeof(double));
    for(i=1;i<=N;i++) {
        for(j=1;j<=N;j++) {
            matrix[i][j]=1;
            }
        }   
    row=col=1;   
    ri = (N-(N-2));
    ci = (N-(N-1));
    spiral(matrix,N,row,col,val,ri,ci);
    printf("\n");
    for(i=1;i<=N;i++) {
        for(j=1;j<=N;j++) {
            if ( i== 1)
            printf("%d          ",matrix[i][j]);
            else
            printf("%d         ",matrix[i][j]);
            }
        printf("\n");
        }
    return 0;
}
void spiral(int **matrix,int N,int row,int col,int val,int ri,int ci) {
    int a,b,tmp,i,j,l=0;
    a = row;
    b = col;
    tmp = val +1;
    if (N== 1 || N==2 ) // stopping condition
        {
            if ( N == 1) {
            matrix[row][col] = tmp;
            }
            else {
            matrix[row][col] = tmp;
            matrix[row][col+1] = tmp+1;
            matrix[row+1][col+1] = tmp+2;
            matrix[row+1][col] = tmp+3;
            }
        }
    else    {       
        for( b = 1 ; b <= N ; b++) {
            matrix[row][b-1+col] = tmp++;
            if( row!=b-1+col )
            matrix[b-1+col][row]  = (4*N-2)-(matrix[row][b-1+col]-val) + val ;
            }
        b--;
        for( a = 1 ; a <= N-1; a++) {
            matrix[a+row][b-1+col] = tmp++;
            if( a+row!=b-1+col)
            matrix[b-1+col][a+row]  = (4*N-2)-(matrix[a+row][b-1+col]-val) +val;
            }
        val = matrix[ri][ci];   
        spiral(matrix,N-2,++row,++col,val,++ri,++ci);   
    }   
}


Python Code

Passing List to function, recursion in python


def SpiralMatrix():
    N = int(input("Enter the order of the Magic Square (odd):"))
    matrix=[[ 0 for row in range(N+2)]for col in range(N+2)]
    for i in range (N+2):
        for j in range (N+2):
            matrix[i][j]=1
    row=col=1  
    val = 0
    ri = (N-(N-2))
    ci = (N-(N-1))  
    def spiral(matrix,N,row,col,val,ri,ci):
        a = row
        b = col
        tmp = val +1
        #print a,b,tmp
        if N== 1 or N==2:
            if N == 1:
                matrix[row][col] = tmp;
            else:
                matrix[row][col] = tmp
                matrix[row][col+1] = tmp+1
                matrix[row+1][col+1] = tmp+2
                matrix[row+1][col] = tmp+3
           
        else:      
            for b in range(N):
                b = b+1
                matrix[row][b-1+col] = tmp
                tmp = tmp+1
                if row != b-1+col:
                    matrix[b-1+col][row]  = (4*N-2)-(matrix[row][b-1+col]-val) + val
               
            for a in range(N-1):
                a = a+1
                matrix[a+row][b-1+col] = tmp
                tmp = tmp+1
                if a+row != b-1+col:
                    matrix[b-1+col][a+row]  = (4*N-2)-(matrix[a+row][b-1+col]-val) +val
            val = matrix[ri][ci]
            row = row+1
            col = col+1
            ri = ri+1
            ci = ci+1    
            spiral(matrix,N-2,row,col,val,ri,ci)
    spiral(matrix,N,row,col,val,ri,ci)      
    print ("OUTPUT~~ SPIRAL MATRIX:")
    for i in range (N):
        for j in range (N):
            print (matrix[i+1][j+1],end =' ')
        print()
SpiralMatrix()