@@ -537,17 +537,22 @@ def add_links(cells: List[Tuple[int, int]]) -> None:
537537 add_links (_unit_cells_box (b ))
538538
539539 color : List [Optional [int ]] = [None ] * len (nodes )
540+ component : List [Optional [int ]] = [None ] * len (nodes )
541+ comp_id = 0
540542 for start in range (len (nodes )):
541543 if color [start ] is not None :
542544 continue
543545 color [start ] = 0
546+ component [start ] = comp_id
544547 stack = [start ]
545548 while stack :
546549 u = stack .pop ()
547550 for v in adj [u ]:
548551 if color [v ] is None :
549552 color [v ] = 1 - color [u ]
553+ component [v ] = comp_id
550554 stack .append (v )
555+ comp_id += 1
551556
552557 for unit_kind in ("row" , "col" , "box" ):
553558 for unit_idx in range (9 ):
@@ -558,13 +563,15 @@ def add_links(cells: List[Tuple[int, int]]) -> None:
558563 ]
559564 if len (cells ) < 2 :
560565 continue
561- by_color : Dict [int , List [Tuple [int , int ]]] = {}
566+ by_color : Dict [Tuple [ int , int ] , List [Tuple [int , int ]]] = {}
562567 for (r , c ) in cells :
563- col = color [idx [(r , c )]]
564- if col is None :
568+ node_idx = idx [(r , c )]
569+ col = color [node_idx ]
570+ comp = component [node_idx ]
571+ if col is None or comp is None :
565572 continue
566- by_color .setdefault (col , []).append ((r , c ))
567- for col_value , positions in sorted (by_color .items ()):
573+ by_color .setdefault (( comp , col ) , []).append ((r , c ))
574+ for ( comp , col_value ) , positions in sorted (by_color .items ()):
568575 if len (positions ) >= 2 :
569576 r , c = positions [0 ]
570577 if d in cand [r ][c ]:
@@ -575,6 +582,7 @@ def add_links(cells: List[Tuple[int, int]]) -> None:
575582 "digit" : d ,
576583 "unit" : unit_kind ,
577584 "unit_index" : unit_idx ,
585+ "component" : comp ,
578586 "color" : col_value ,
579587 "r" : r ,
580588 "c" : c ,
0 commit comments