1
+ package com .problems .recursion ;
2
+
3
+ import com .util .PrintUtl ;
4
+
5
+ /*
6
+ * Problem links:
7
+ * https://leetcode.com/problems/fill-a-special-grid/description/
8
+ *
9
+ * Solution link
10
+ *
11
+ * */
12
+ public class FillASpecialGrid {
13
+ public static void main (String [] args ) {
14
+ type1 ();
15
+ type2 ();
16
+ }
17
+
18
+ // recursive way
19
+ private static void type2 () {
20
+ int N = 2 ;
21
+ int [][] ans = specialGrid1 (N );
22
+ PrintUtl .print2D (ans );
23
+ }
24
+
25
+ public static int [][] specialGrid1 (int N ) {
26
+ if (N == 0 ) return new int [][]{new int []{0 }};
27
+ int n = 1 << N ;
28
+ int [][] arr = new int [n ][n ];
29
+ int low = 0 , high = (1 << 2 * N ) - 1 ;
30
+ fill2 (arr , low , high , 0 , n - 1 , 0 , n - 1 );
31
+ return arr ;
32
+ }
33
+
34
+ private static void fill2 (int [][] arr , int low , int high , int x1 , int x2 , int y1 , int y2 ) {
35
+ int count = high - low + 1 ;
36
+ int d = (x2 - x1 + 1 ) / 2 ;
37
+ int q = count / 4 ;
38
+ if (count == 4 ) {
39
+ arr [x1 ][y2 ] = low ;
40
+ arr [x2 ][y2 ] = low + 1 ;
41
+ arr [x2 ][y1 ] = low + 2 ;
42
+ arr [x1 ][y1 ] = high ;
43
+ } else {
44
+ fill2 (arr , low , low + q - 1 , x1 , x1 + d - 1 , y1 + d , y2 );
45
+ fill2 (arr , low + q , low + 2 * q - 1 , x1 + d , x2 , y1 + d , y2 );
46
+ fill2 (arr , low + 2 * q , low + 3 * q - 1 , x1 + d , x2 , y1 , y1 + d - 1 );
47
+ fill2 (arr , low + 3 * q , high , x1 , x1 + d - 1 , y1 , y1 + d - 1 );
48
+ }
49
+ }
50
+
51
+ // iterative way
52
+ private static void type1 () {
53
+
54
+ }
55
+ }
0 commit comments