-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratespiralmatrix.java
47 lines (44 loc) · 1.53 KB
/
generatespiralmatrix.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
import java.util.Scanner;
public class generatespiralmatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of the matrix(nxn): ");
int n = sc.nextInt();
int[][] vib = generateSpiral(n);
print(vib);
}
static void print(int[][] vib) {
for (int i = 0; i < vib.length; i++) {
for (int j = 0; j < vib[i].length; j++) {
System.out.print(vib[i][j] + " ");
}
System.out.println();
}
}
static int [][] generateSpiral(int n) {
int [][] vib = new int [n][n];
int leftcolumn = 0, rightcolumn = n-1, toprow = 0, bottomrow = n-1;
int curr = 1;
while (curr <= n*n) {
// leftcolumn to rightcolumn
for (int j = leftcolumn ; j <= rightcolumn && curr <=n*n; j ++ ) {
vib[toprow][j] = curr++;
}
toprow ++;
// toprow to bottomrow
for (int i = toprow ; i <= bottomrow && curr <=n*n ; i++) {
vib[i][rightcolumn] = curr++;
}
rightcolumn--;
for (int j = rightcolumn; j >= leftcolumn && curr <=n*n; j-- ) {
vib[bottomrow][j] = curr++;
}
bottomrow--;
for (int i = bottomrow; i >=toprow && curr <=n*n; i --) {
vib[i][leftcolumn] = curr++;
}
leftcolumn++;
}
return vib;
}
}