11use std:: {
2- collections:: { BinaryHeap , VecDeque } , hash:: Hash , ops:: { Bound , RangeBounds } , str:: FromStr
2+ collections:: { BinaryHeap , VecDeque } ,
3+ hash:: Hash ,
4+ ops:: { Bound , RangeBounds } ,
5+ str:: FromStr ,
36} ;
47
58use anyhow:: { Result , anyhow} ;
@@ -227,7 +230,9 @@ impl<const R: usize> State<R> {
227230 #[ inline( always) ]
228231 fn with_dist ( self , dist : usize ) -> Self {
229232 debug_assert ! ( dist <= 65535 ) ;
230- Self { inner : ( self . inner & !0xFFFF ) | dist as u64 }
233+ Self {
234+ inner : ( self . inner & !0xFFFF ) | dist as u64 ,
235+ }
231236 }
232237 #[ inline( always) ]
233238 fn dist ( self ) -> usize {
@@ -409,16 +414,16 @@ impl<const R: usize> KeyGraph<R> {
409414 }
410415
411416 fn heuristic ( & self , state : State < R > ) -> usize {
412- let mut h = 0 ;
413- for r in 0 .. R {
414- h += self . robot_keys [ r]
415- . difference ( & state. keys ( ) )
416- . into_iter ( )
417- . map ( |k| self . distances [ state. pos ( r) ] [ k] )
418- . max ( )
419- . unwrap_or ( 0 ) ;
420- }
421- h
417+ ( 0 .. R )
418+ . map ( |r| {
419+ self . robot_keys [ r]
420+ . difference ( & state. keys ( ) )
421+ . into_iter ( )
422+ . map ( |k| self . distances [ state. pos ( r) ] [ k] )
423+ . max ( )
424+ . unwrap_or ( 0 )
425+ } )
426+ . sum ( )
422427 }
423428
424429 fn explore ( & self ) -> usize {
@@ -441,18 +446,14 @@ impl<const R: usize> KeyGraph<R> {
441446 }
442447
443448 let f_cur = state. dist ( ) ;
444- let g_cur = match dist . get ( & state ) {
445- Some ( & g ) => g ,
446- None => continue ,
449+ // Truthfully this cannot fail but whatever
450+ let Some ( g_cur ) = dist . get ( & state ) . copied ( ) else {
451+ continue ;
447452 } ;
448453
449- // Heuristic check: f = g + h.
450- // If stored g is better than implied g (f - h), then this path is bad?
451- // No, if g_cur < (f_cur - h), it means we found a better path to this state already.
452- // Or simply, we can recompute h.
453454 let h_cur = self . heuristic ( state) ;
454455 if f_cur > g_cur + h_cur {
455- continue ;
456+ continue ;
456457 }
457458
458459 for robot in 0 ..R {
@@ -464,9 +465,7 @@ impl<const R: usize> KeyGraph<R> {
464465 let weight = self . distances [ state. pos ( robot) ] [ n] ;
465466 let n_g = g_cur + weight;
466467
467- let n_state = state
468- . with_pos ( robot, n)
469- . with_key ( n, n != self . count ) ;
468+ let n_state = state. with_pos ( robot, n) . with_key ( n, n != self . count ) ;
470469
471470 if n_g < dist. get ( & n_state) . copied ( ) . unwrap_or ( usize:: MAX ) {
472471 dist. insert ( n_state, n_g) ;
0 commit comments