@@ -604,126 +604,126 @@ func getPrevStates(gMouse, gCat [][]int, m, c, t int, ans [][][]int) [][]int {
604604
605605``` ts
606606function canMouseWin(grid : string [], catJump : number , mouseJump : number ): boolean {
607- const m = grid .length
608- const n = grid [0 ].length
607+ const m = grid .length ;
608+ const n = grid [0 ].length ;
609609
610- let catStart = 0
611- let mouseStart = 0
612- let food = 0
610+ let catStart = 0 ;
611+ let mouseStart = 0 ;
612+ let food = 0 ;
613613
614- const dirs = [- 1 , 0 , 1 , 0 , - 1 ]
614+ const dirs = [- 1 , 0 , 1 , 0 , - 1 ];
615615
616- const gMouse: number [][] = Array .from ({ length: m * n }, () => [])
617- const gCat: number [][] = Array .from ({ length: m * n }, () => [])
616+ const gMouse: number [][] = Array .from ({ length: m * n }, () => []);
617+ const gCat: number [][] = Array .from ({ length: m * n }, () => []);
618618
619619 for (let i = 0 ; i < m ; i ++ ) {
620620 for (let j = 0 ; j < n ; j ++ ) {
621- const c = grid [i ][j ]
621+ const c = grid [i ][j ];
622622
623- if (c === ' #' ) continue
623+ if (c === ' #' ) continue ;
624624
625- const v = i * n + j
625+ const v = i * n + j ;
626626
627- if (c === ' C' ) catStart = v
628- else if (c === ' M' ) mouseStart = v
629- else if (c === ' F' ) food = v
627+ if (c === ' C' ) catStart = v ;
628+ else if (c === ' M' ) mouseStart = v ;
629+ else if (c === ' F' ) food = v ;
630630
631631 for (let d = 0 ; d < 4 ; d ++ ) {
632- const a = dirs [d ]
633- const b = dirs [d + 1 ]
632+ const a = dirs [d ];
633+ const b = dirs [d + 1 ];
634634
635635 for (let k = 0 ; k <= mouseJump ; k ++ ) {
636- const x = i + k * a
637- const y = j + k * b
636+ const x = i + k * a ;
637+ const y = j + k * b ;
638638
639- if (! (0 <= x && x < m && 0 <= y && y < n && grid [x ][y ] !== ' #' )) break
639+ if (! (0 <= x && x < m && 0 <= y && y < n && grid [x ][y ] !== ' #' )) break ;
640640
641- gMouse [v ].push (x * n + y )
641+ gMouse [v ].push (x * n + y );
642642 }
643643
644644 for (let k = 0 ; k <= catJump ; k ++ ) {
645- const x = i + k * a
646- const y = j + k * b
645+ const x = i + k * a ;
646+ const y = j + k * b ;
647647
648- if (! (0 <= x && x < m && 0 <= y && y < n && grid [x ][y ] !== ' #' )) break
648+ if (! (0 <= x && x < m && 0 <= y && y < n && grid [x ][y ] !== ' #' )) break ;
649649
650- gCat [v ].push (x * n + y )
650+ gCat [v ].push (x * n + y );
651651 }
652652 }
653653 }
654654 }
655655
656656 function getPrevStates(m : number , c : number , t : number , ans : number [][][]): number [][] {
657- const pt = t ^ 1
658- const pre: number [][] = []
657+ const pt = t ^ 1 ;
658+ const pre: number [][] = [];
659659
660660 if (pt === 1 ) {
661661 for (const pc of gCat [c ]) {
662- if (ans [m ][pc ][1 ] === 0 ) pre .push ([m , pc , pt ])
662+ if (ans [m ][pc ][1 ] === 0 ) pre .push ([m , pc , pt ]);
663663 }
664664 } else {
665665 for (const pm of gMouse [m ]) {
666- if (ans [pm ][c ][0 ] === 0 ) pre .push ([pm , c , pt ])
666+ if (ans [pm ][c ][0 ] === 0 ) pre .push ([pm , c , pt ]);
667667 }
668668 }
669669
670- return pre
670+ return pre ;
671671 }
672672
673673 function calc(): number {
674- const N = m * n
674+ const N = m * n ;
675675
676676 const degree: number [][][] = Array .from ({ length: N }, () =>
677- Array .from ({ length: N }, () => [0 , 0 ])
678- )
677+ Array .from ({ length: N }, () => [0 , 0 ]),
678+ );
679679
680680 const ans: number [][][] = Array .from ({ length: N }, () =>
681- Array .from ({ length: N }, () => [0 , 0 ])
682- )
681+ Array .from ({ length: N }, () => [0 , 0 ]),
682+ );
683683
684684 for (let i = 0 ; i < N ; i ++ ) {
685685 for (let j = 0 ; j < N ; j ++ ) {
686- degree [i ][j ][0 ] = gMouse [i ].length
687- degree [i ][j ][1 ] = gCat [j ].length
686+ degree [i ][j ][0 ] = gMouse [i ].length ;
687+ degree [i ][j ][1 ] = gCat [j ].length ;
688688 }
689689 }
690690
691- const q: number [][] = []
691+ const q: number [][] = [];
692692
693693 for (let i = 0 ; i < N ; i ++ ) {
694- ans [food ][i ][1 ] = 1
695- ans [i ][food ][0 ] = 2
696- ans [i ][i ][1 ] = 2
697- ans [i ][i ][0 ] = 2
698-
699- q .push ([food , i , 1 ])
700- q .push ([i , food , 0 ])
701- q .push ([i , i , 0 ])
702- q .push ([i , i , 1 ])
694+ ans [food ][i ][1 ] = 1 ;
695+ ans [i ][food ][0 ] = 2 ;
696+ ans [i ][i ][1 ] = 2 ;
697+ ans [i ][i ][0 ] = 2 ;
698+
699+ q .push ([food , i , 1 ]);
700+ q .push ([i , food , 0 ]);
701+ q .push ([i , i , 0 ]);
702+ q .push ([i , i , 1 ]);
703703 }
704704
705705 while (q .length ) {
706- const [mPos, cPos, t] = q .shift ()!
707- const currentAns = ans [mPos ][cPos ][t ]
706+ const [mPos, cPos, t] = q .shift ()! ;
707+ const currentAns = ans [mPos ][cPos ][t ];
708708
709709 for (const [pm, pc, pt] of getPrevStates (mPos , cPos , t , ans )) {
710710 if (pt === currentAns - 1 ) {
711- ans [pm ][pc ][pt ] = currentAns
712- q .push ([pm , pc , pt ])
711+ ans [pm ][pc ][pt ] = currentAns ;
712+ q .push ([pm , pc , pt ]);
713713 } else {
714- degree [pm ][pc ][pt ]--
714+ degree [pm ][pc ][pt ]-- ;
715715 if (degree [pm ][pc ][pt ] === 0 ) {
716- ans [pm ][pc ][pt ] = currentAns
717- q .push ([pm , pc , pt ])
716+ ans [pm ][pc ][pt ] = currentAns ;
717+ q .push ([pm , pc , pt ]);
718718 }
719719 }
720720 }
721721 }
722722
723- return ans [mouseStart ][catStart ][0 ]
723+ return ans [mouseStart ][catStart ][0 ];
724724 }
725725
726- return calc () === 1
726+ return calc () === 1 ;
727727}
728728```
729729
0 commit comments