@@ -23,52 +23,68 @@ const spiralOrder = (matrix) => {
23
23
const moveNext = ( r , c , dir ) => {
24
24
let nextDir = nextDirection . get ( dir ) ;
25
25
let { r : newR , c : newC } = formula . get ( nextDir ) ;
26
- console . log ( `>> Going newR:${ r + newR + ( r === matrix . length ? - 1 : 0 ) } , newC:${ c + newC + ( c === matrix [ 0 ] . length ? - 1 : 0 ) } ` )
26
+ console . log ( `>> Going nextDir: ${ nextDir } , newR:${ r + newR + ( r === matrix . length ? - 1 : 0 ) } , newC:${ c + newC + ( c === matrix [ 0 ] . length ? - 1 : 0 ) } ` )
27
27
// let isREdge = r > matrix.length, isCEdge = c > matrix[0].length;
28
- console . log ( "matrix[0].length " )
29
- console . log ( matrix [ 0 ] . length )
28
+ // console.log("matrix[0].length ")
29
+ // console.log(matrix[0].length)
30
30
31
31
newR += r + ( r === matrix . length ? - 1 : 0 ) + ( r < 0 ? 1 : 0 )
32
32
newC += c + ( c === matrix [ 0 ] . length ? - 1 : 0 ) + ( c < 0 ? 1 : 0 )
33
33
34
34
return { r : newR , c : newC , dir : nextDir } ;
35
35
}
36
36
37
- const explore = ( r , c , dir , tries ) => {
38
- console . log ( `> r:${ r } , c:${ c } , dir:${ dir } , tries:${ tries } ` )
37
+ const explore = ( r , c , dir , tBorder , rBorder , bBorder , lBorder ) => {
38
+ console . log ( `> r:${ r } , c:${ c } , dir:${ dir } , tBorder:${ tBorder } , rBorder:${ rBorder } , bBorder:${ bBorder } , lBorder:${ lBorder } ` )
39
+
39
40
// edge
41
+ console . log ( r - bBorder === matrix . length )
42
+ console . log ( c - rBorder === matrix [ 0 ] . length )
43
+ console . log ( ( dir === "t" && r < 0 + tBorder ) )
44
+ console . log ( ( dir === "l" && c < 0 + lBorder ) )
45
+ console . log ( ( dir === "r" && c + rBorder === matrix [ 0 ] . length ) )
46
+ console . log ( ( dir === "b" && r + bBorder === matrix . length ) )
40
47
if (
41
- r === matrix . length ||
42
- c === matrix [ 0 ] . length ||
43
- r < 0 ||
44
- c < 0
48
+ r - bBorder === matrix . length ||
49
+ c - rBorder === matrix [ 0 ] . length ||
50
+ ( dir === "t" && r < 0 + tBorder ) ||
51
+ ( dir === "l" && c < 0 + lBorder ) ||
52
+ ( dir === "r" && c + rBorder === matrix [ 0 ] . length ) ||
53
+ ( dir === "b" && r + bBorder === matrix . length )
45
54
) {
46
55
let next = moveNext ( r , c , dir ) ;
47
- explore ( next . r , next . c , next . dir , tries ) ;
56
+
57
+ explore (
58
+ next . r + ( next . dir === "r" ? tBorder : 0 ) + ( next . dir === "l" ? - bBorder : 0 ) ,
59
+ next . c + ( next . dir === "b" ? - rBorder : 0 ) + ( next . dir === "t" ? lBorder : 0 ) ,
60
+ next . dir ,
61
+ tBorder + ( next . dir === "r" ? 1 : 0 ) ,
62
+ rBorder + ( next . dir === "b" ? 1 : 0 ) ,
63
+ bBorder + ( next . dir === "l" ? 1 : 0 ) ,
64
+ lBorder + ( next . dir === "t" ? 1 : 0 ) ) ;
48
65
return ;
49
66
}
50
67
51
68
// visited - end
52
- if ( matrix . length < tries ) {
53
- return ;
54
- }
69
+ // if (matrix.length < tries) {
70
+ // return;
71
+ // }
55
72
56
73
if ( Infinity === matrix [ r ] [ c ] ) {
57
74
// let next = moveNext(r, c, dir);
58
75
// explore(next.r, next.c, next.dir, tries + 1);
59
76
60
- let nextDir = nextDirection . get ( dir ) ;
61
- let { r : newR , c : newC } = formula . get ( nextDir ) ;
62
- console . log ( `>> Going newR:${ r + newR + ( r === matrix . length ? - 1 : 0 ) } , newC:${ c + newC + ( c === matrix [ 0 ] . length ? - 1 : 0 ) } ` )
63
- // let isREdge = r > matrix.length, isCEdge = c > matrix[0].length;
64
- console . log ( "matrix[0].length " )
65
- console . log ( matrix [ 0 ] . length )
66
-
67
- newR += r + ( r === matrix . length ? - 1 : 0 ) + ( r < 0 ? 1 : 0 ) - ( ( formula . get ( dir ) . r ) ) ;
68
- newC += c + ( c === matrix [ 0 ] . length ? - 1 : 0 ) + ( c < 0 ? 1 : 0 ) - ( ( formula . get ( dir ) . c ) ) ;
69
-
70
- // return { r: newR, c: newC, dir: nextDir };
71
- explore ( newR , newC , nextDir , tries + 1 ) ;
77
+ // let nextDir = nextDirection.get(dir);
78
+ // let { r: newR, c: newC } = formula.get(nextDir);
79
+ // console.log(`>> Going newR:${r + newR + (r === matrix.length ? -1 : 0)}, newC:${c + newC + (c === matrix[0].length ? -1 : 0)}`)
80
+ // // let isREdge = r > matrix.length, isCEdge = c > matrix[0].length;
81
+ // console.log("matrix[0].length ")
82
+ // console.log(matrix[0].length)
83
+ //
84
+ // newR += r + (r === matrix.length ? -1 : 0) + (r < 0 ? 1 : 0) - ((formula.get(dir).r));
85
+ // newC += c + (c === matrix[0].length ? -1 : 0) + (c < 0 ? 1 : 0) - ((formula.get(dir).c));
86
+ //
87
+ // explore(newR, newC, nextDir, tBorder, rBorder, bBorder, lBorder);
72
88
return ;
73
89
}
74
90
@@ -79,39 +95,39 @@ const spiralOrder = (matrix) => {
79
95
// console.log(`> r:${r}, c:${c}, addR:${dirFormula.r}, addC:${dirFormula.c}`)
80
96
// if next we turn
81
97
// matrix[r + dirFormula.r][c + dirFormula.c];
82
- explore ( r + dirFormula . r , c + dirFormula . c , dir , tries )
98
+ explore ( r + dirFormula . r , c + dirFormula . c , dir , tBorder , rBorder , bBorder , lBorder )
83
99
}
84
100
85
- explore ( 0 , 0 , "r" , 0 ) ;
101
+ explore ( 0 , 0 , "r" , 1 , 0 , 0 , 0 ) ;
86
102
console . log ( matrix )
87
103
return spiralList ;
88
104
} ;
89
105
90
106
let x = null ;
91
107
92
108
// [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,99,98,97,96,95,94,93,92,91,81,71,61,51,41,31,21,11,12,13,14,15,16,17,18,19,29,39,49,59,69,79,89,88,87,86,85,84,83,82,72,62,52,42,32,22,23,24,25,26,27,28,38,48,58,68,78,77,76,75,74,73,63,53,43,33,34,35,36,37,47,57,67,66,65,64,54,44,45,46,56,55]
93
- x = spiralOrder ( [
94
- [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ,
95
- [ 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 ] ,
96
- [ 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 ] ,
97
- [ 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 ] ,
98
- [ 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 ] ,
99
- [ 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 ] ,
100
- [ 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 ] ,
101
- [ 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 ] ,
102
- [ 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 ] ,
103
- [ 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 ]
104
- ] ) ;
105
-
106
- // [1,2,3,4,5,10,15,20,25,24,23,22,21,16,11,6,7,8,9,14,19,18,17,12,13]
107
109
// x = spiralOrder([
108
- // [1, 2, 3, 4, 5],
109
- // [6, 7, 8, 9, 10],
110
- // [11, 12, 13, 14, 15],
111
- // [16, 17, 18, 19, 20],
112
- // [21, 22, 23, 24, 25]
110
+ // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
111
+ // [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
112
+ // [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
113
+ // [31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
114
+ // [41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
115
+ // [51, 52, 53, 54, 55, 56, 57, 58, 59, 60],
116
+ // [61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
117
+ // [71, 72, 73, 74, 75, 76, 77, 78, 79, 80],
118
+ // [81, 82, 83, 84, 85, 86, 87, 88, 89, 90],
119
+ // [91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
113
120
// ]);
114
121
122
+ // [1,2,3,4,5,10,15,20,25,24,23,22,21,16,11,6,7,8,9,14,19,18,17,12,13]
123
+ x = spiralOrder ( [
124
+ [ 1 , 2 , 3 , 4 , 5 ] ,
125
+ [ 6 , 7 , 8 , 9 , 10 ] ,
126
+ [ 11 , 12 , 13 , 14 , 15 ] ,
127
+ [ 16 , 17 , 18 , 19 , 20 ] ,
128
+ [ 21 , 22 , 23 , 24 , 25 ]
129
+ ] ) ;
130
+
115
131
// x = spiralOrder([[1]]);
116
132
117
133
// [1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]
0 commit comments