-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSpiral_Pattern.java
98 lines (96 loc) · 2.33 KB
/
Spiral_Pattern.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.Scanner;
public class Spiral_Pattern
{
static void printSpiralPattern(int size)
{
//create two variables row and col to traverse rows and columns
int row = 0, col = 0;
int boundary = size - 1;
int sizeLeft = size - 1;
int flag = 1;
//variables r, l, u and d are used to determine the movement
// r = right, l = left, d = down, u = upper
char move = 'r';
//creating a 2D array for matrix
int[][] matrix =new int [size][size];
for (int i = 1; i < size * size + 1; i++)
{
matrix[row][col] = i;
//determining the next index
switch (move)
{
//if move = right, go right
case 'r':
col += 1;
break;
//if move = left, go left
case 'l':
col -= 1;
break;
//if move = up, go up
case 'u':
row -= 1;
break;
//if move = down, go down
case 'd':
row += 1;
break;
}
if (i == boundary)
{
//adding left size for the next boundary
boundary = boundary + sizeLeft;
//decreasing the size of left by 1, if 2 rotations have been made
if (flag != 2)
{
flag = 2;
}
else
{
flag = 1;
sizeLeft -= 1;
}
//rotating the movement
switch (move)
{
//if move = right, rotate to down
case 'r':
move = 'd';
break;
// if move = down, rotate to left
case 'd':
move = 'l';
break;
// if move = left, rotate to up
case 'l':
move = 'u';
break;
// if move = up, rotate to right
case 'u':
move = 'r';
break;
}
}
}
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
{
int n = matrix[row][col];
if(n < 10)
System.out.print(n +" ");
else
System.out.print(n +" ");
}
System.out.println();
}
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of array: \n");
int size = scanner.nextInt();
System.out.println("Spiral Matrix or Pattern is: \n");
printSpiralPattern(size);
}
}