public Bit81 Copy() { Bit81 Scpy = new Bit81(); Scpy.BPSet(this); return(Scpy); }
public Bit81(Bit81 P) : this() { this._BP[0] = P._BP[0]; this._BP[1] = P._BP[1]; this._BP[2] = P._BP[2]; }
} //UVWXYZ-wing private bool _UVWXYZwing(int wsz) //simple UVWXYZwing { if (pAnMan.GStage != GStageMemo) { GStageMemo = pAnMan.GStage; FBCX = pBDL.FindAll(p => p.FreeBC == wsz); } if (FBCX.Count == 0) { return(false); } bool wingF = false; foreach (var P0 in FBCX) //focused Cell { int b0 = P0.b; //focused Block foreach (int no in P0.FreeB.IEGet_BtoNo()) //focused Digit { int noB = 1 << no; Bit81 P0con = (new Bit81(pBDL, noB, FreeBC: 2)) & ConnectedCells[P0.rc]; Bit81 Pin = P0con & HouseCells[18 + P0.b]; Bit81 Pout = null, Pin2 = null; for (int dir = 0; dir < 2; dir++) //dir 0:row 1:col { int rcDir = (dir == 0)? P0.r: (9 + P0.c); Pin2 = Pin - HouseCells[rcDir]; if (Pin2.IsZero()) { continue; } Pout = (P0con & HouseCells[rcDir]) - HouseCells[18 + P0.b]; if (Pin2.Count + Pout.Count != (wsz - 1)) { continue; } int FreeBin = Pin2.AggregateFreeB(pBDL); int FreeBout = Pout.AggregateFreeB(pBDL); if ((FreeBin | FreeBout) != P0.FreeB) { continue; } Bit81 ELst = HouseCells[rcDir] & HouseCells[18 + P0.b]; ELst.BPReset(P0.rc); string msg3 = ""; foreach (var E in ELst.IEGet_rc().Select(p => pBDL[p])) { if ((E.FreeB & noB) > 0) { E.CancelB = noB; wingF = true; msg3 += " " + E.rc.ToRCString(); } } if (!wingF) { continue; } //--- ...wing found ------------- SolCode = 2; string[] xyzWingName = { "XYZ-Wing", "WXYZ-Wing", "VWXYZ-Wing", "UVWXYZ-Wing" }; string SolMsg = xyzWingName[wsz - 3]; if (SolInfoB) { P0.SetNoBBgColor(P0.FreeB, AttCr, SolBkCr2); foreach (var P in Pin2.IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(P.FreeB, AttCr, SolBkCr); } foreach (var P in Pout.IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(P.FreeB, AttCr, SolBkCr); } string msg0 = " Pivot: " + P0.rc.ToRCString(); string st = ""; foreach (var rc in Pin2.IEGet_rc()) { st += " " + rc.ToRCString(); } string msg1 = " in: " + st.ToString_SameHouseComp(); st = ""; foreach (var rc in Pout.IEGet_rc()) { st += " " + rc.ToRCString(); } string msg2 = " out: " + st.ToString_SameHouseComp(); st = ""; foreach (var rc in Pin2.IEGet_rc()) { st += " " + rc.ToRCString(); } ResultLong = SolMsg + "\r" + msg0 + "\r " + msg1 + "\r " + msg2 + "\r Eliminated: " + msg3.ToString_SameHouseComp(); Result = SolMsg + msg0 + msg1 + msg2; } if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } wingF = false; } } } return(false); }
public UALSPair(UALS ALSpre, UALS ALSnxt, int RCC, int nRCC, Bit81 rcB = null) { this.ALSpre = ALSpre; this.ALSnxt = ALSnxt; this.RCC = RCC; this.nRCC = nRCC; this.rcUsed = rcB ?? (ALSpre.B81 | ALSnxt.B81); }
private bool _ALSXYWingSub(int szT) { //(ALS sorted by size) foreach (var UC in ALSMan.ALSLst.Where(p => p.Size <= szT - 2)) { if (!UC.singly) { continue; } int szS = szT - UC.Size; UALS UA, UB, UApre = null; int nxt = 0, RccAC = -1, RccBC = -1; var cmb = new Combination(ALSMan.ALSLst.Count, 2); while (cmb.Successor(nxt)) { nxt = 0; UA = ALSMan.ALSLst[cmb.Index[0]]; if (!UA.singly || UA == UC || UA.Size > szS - 1) { continue; } if (UA != UApre) { RccAC = ALSMan.Get_AlsAlsRcc(UA, UC); //RCC if (RccAC.BitCount() != 1) { continue; } UApre = UA; } UB = ALSMan.ALSLst[cmb.Index[1]]; if (!UB.singly || UB.Size > (szS - UA.Size)) { continue; //Skip using "Sort by size" } nxt = 1; if (UB == UC || UB.Size != (szS - UA.Size)) { continue; } if (!(UA.B81 & UB.B81).IsZero()) { continue; //Overlap } RccBC = ALSMan.Get_AlsAlsRcc(UB, UC); //RCC if (RccBC.BitCount() != 1) { continue; } if (RccAC == RccBC) { continue; } int EFrB = (UA.FreeB & UB.FreeB).DifSet(RccAC | RccBC); if (EFrB == 0) { continue; } foreach (var no in EFrB.IEGet_BtoNo()) { int noB = (1 << no); Bit81 UE = new Bit81(); foreach (var P in UA.UCellLst.Where(p => (p.FreeB & noB) > 0)) { UE.BPSet(P.rc); } foreach (var P in UB.UCellLst.Where(p => (p.FreeB & noB) > 0)) { UE.BPSet(P.rc); } Bit81 TBD = (new Bit81(pBDL, noB)) - (UA.B81 | UB.B81 | UC.B81); foreach (var rc in TBD.IEGet_rc()) { if (!(UE - ConnectedCells[rc]).IsZero()) { continue; } pBDL[rc].CancelB = noB; SolCode = 2; } if (SolCode > 0) //===== ALS XY-Wing found ===== { ALSXYWing_SolResult(UA, UB, UC, RccAC, RccBC); if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } } } } } return(false); }
private bool _ALS_DeathBlossomSubEx(int sz, bool stmLinked = false) { int szM = (stmLinked? sz - 1: sz); foreach (var SC in pBDL.Where(p => p.FreeBC == sz)) //Stem Cell { if (pAnMan.CheckTimeOut()) { return(false); } List <LinkCellALS> LinkCeAlsLst = ALSMan.LinkCeAlsLst[SC.rc]; if (LinkCeAlsLst == null || LinkCeAlsLst.Count < sz) { continue; } int nxt = 0, PFreeB = SC.FreeB; var cmb = new Combination(LinkCeAlsLst.Count, szM); //Select szM ALSs in Combination while (cmb.Successor(nxt)) { int FreeB = SC.FreeB, AFreeB = 0x1FF; for (int k = 0; k < szM; k++) { nxt = k; var LK = LinkCeAlsLst[cmb.Index[k]]; //Link[cell-ALS] if ((FreeB & (1 << LK.nRCC)) == 0) { goto LNxtCmb; } FreeB = FreeB.BitReset(LK.nRCC); //nRCC:RCC of stemCell-ALS AFreeB &= LK.ALS.FreeB; if (AFreeB == 0) { goto LNxtCmb; } } if (stmLinked) { if (FreeB.BitCount() != 1 || (FreeB & AFreeB) == 0) { continue; } int no = FreeB.BitToNum(); int noB = FreeB; Bit81 Ez = new Bit81(); for (int k = 0; k < szM; k++) { var ALS = LinkCeAlsLst[cmb.Index[k]].ALS; var UClst = ALS.UCellLst; foreach (var P in UClst.Where(p => (p.FreeB & noB) > 0)) { Ez.BPSet(P.rc); } } foreach (var P in ConnectedCells[SC.rc].IEGet_rc().Select(rc => pBDL[rc])) { if ((P.FreeB & noB) == 0) { continue; } if ((Ez - ConnectedCells[P.rc]).IsZero()) { P.CancelB = noB; SolCode = 2; } } if (SolCode < 1) { continue; } var LKCAsol = new List <LinkCellALS>(); Array.ForEach(cmb.Index, nx => LKCAsol.Add(LinkCeAlsLst[nx])); _DeathBlossom_SolResult(LKCAsol, SC, no, stmLinked); if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } } else if (FreeB == 0 && AFreeB > 0) { AFreeB = AFreeB.DifSet(SC.FreeB); foreach (var no in AFreeB.IEGet_BtoNo()) { int noB = (1 << no); Bit81 Ez = new Bit81(); for (int k = 0; k < sz; k++) { var ALS = LinkCeAlsLst[cmb.Index[k]].ALS; var UClst = ALS.UCellLst; foreach (var P in UClst.Where(p => (p.FreeB & noB) > 0)) { Ez.BPSet(P.rc); } } foreach (var P in pBDL.Where(p => (p.FreeB & noB) > 0)) { if ((Ez - ConnectedCells[P.rc]).IsZero()) { P.CancelB = noB; SolCode = 2; } } if (SolCode < 1) { continue; } var LKCAsol = new List <LinkCellALS>(); Array.ForEach(cmb.Index, nx => LKCAsol.Add(LinkCeAlsLst[nx])); _DeathBlossom_SolResult(LKCAsol, SC, no, stmLinked); if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } } } LNxtCmb: continue; } } return(false); }
public IEnumerable <UFish> IEGet_CoverSet(UFish BSet, int CoverSel, bool Finned, bool CannFlg = false) { if (HBLst == null) { yield break; } List <Bit81> HCLst = new List <Bit81>(); foreach (var P in HBLst.Where(q => (BSet.HouseB & (1 << q.ID)) == 0)) { if (((1 << P.ID) & CoverSel) == 0) { continue; } if (BSet.BaseB81.IsHit(P)) { HCLst.Add(P); } } if (HCLst.Count < sz) { yield break; } Bit81 Q; Combination cmbCov = new Combination(HCLst.Count, sz); int nxt = int.MaxValue; while (cmbCov.Successor(nxt)) { int chk1 = ++GeneralLogicGen.ChkCov1; int usedLK = 0; Bit81 HC81 = new Bit81(); Bit81 OHC81 = new Bit81(); for (int k = 0; k < sz; k++) { nxt = k; int nx = cmbCov.Index[k]; if (!(Q = HC81 & HCLst[nx]).IsZero()) //overlap { if (!CannFlg) { goto nxtCmb; } OHC81 |= Q; } usedLK |= 1 << HCLst[nx].ID; //house Number HC81 |= HCLst[nx]; //Bit81 } Bit81 FinB81 = BSet.BaseB81 - HC81; if (Finned != (FinB81.Count > 0)) { continue; } UFish UF = new UFish(BSet, usedLK, HC81, FinB81, OHC81); //if(sz>=3 && CoverSel==0x7FFFFFF) WriteLine(" CoverSet: " + UF.HouseC.HouseToString() ); //********** yield return(UF); nxtCmb: continue; } yield break; }
public bool MultiColor_Type2( ) { Prepare(); CeLKMan.PrepareCellLink(1); //Generate StrongLink for (int no = 0; no < 9; no++) { int noB = (1 << no); List <Bit81[]> MCRL = _Coloring(no).ToList(); if (MCRL == null || MCRL.Count < 2) { continue; } var prm = new Permutation(MCRL.Count, 2); while (prm.Successor()) { if (prm.Index[0] < prm.Index[1]) { continue; } Bit81[] CRLa = MCRL[prm.Index[0]]; Bit81[] CRLb = MCRL[prm.Index[1]]; if (CRLa[0].IsZero() || CRLa[1].IsZero()) { continue; } if (CRLb[0].IsZero() || CRLb[1].IsZero()) { continue; } Bit81 HCRL0 = new Bit81(), HCRL1 = new Bit81(); foreach (var rc in CRLa[0].IEGet_rc()) { HCRL0 |= ConnectedCells[rc]; } foreach (var rc in CRLa[1].IEGet_rc()) { HCRL1 |= ConnectedCells[rc]; } for (int nb = 0; nb < 2; nb++) { if ((CRLb[nb] & HCRL0).IsZero()) { continue; } if ((CRLb[nb] & HCRL1).IsZero()) { continue; } Color Cr1 = _ColorsLst[0]; Color Cr2 = Color.FromArgb(100, Cr1.R, Cr1.G, Cr1.B); foreach (var P in CRLa[0].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr1); } foreach (var P in CRLa[1].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr2); } Cr1 = _ColorsLst[1]; Cr2 = Color.FromArgb(100, Cr1.R, Cr1.G, Cr1.B); foreach (var P in CRLb[1 - nb].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr2); } foreach (var P in CRLb[nb].IEGet_rc().Select(p => pBDL[p])) { P.SetCellBgColor(Cr1); P.CancelB = noB; } SolCode = 2; string SolMsg = "MultiColoring Type2 #" + (no + 1); Result = SolMsg; if (SolInfoB) { ResultLong = SolMsg; } if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } } } } return(false); }
private void SearchGroupedLink() { try{ UGrCells[] LQ = new UGrCells[3]; for (int no = 0; no < 9; no++) { int noB = 1 << no; Bit81 BPnoB = new Bit81(pBDL, noB); //------------------------------------------ for (int tfx = 0; tfx < 18; tfx++) { Bit81 BPnoB2 = BPnoB & pHouseCells[tfx]; List <Bit81> houseLst = new List <Bit81>(); List <int> tfxLst = new List <int>(); for (int k = 0; k < 9; k++) { int hx = (tfx < 9)? (k + 9): k; Bit81 BX = BPnoB2 & pHouseCells[hx]; if (BX.IsZero()) { continue; } houseLst.Add(BX); tfxLst.Add(hx); } for (int k = 0; k < 3; k++) { int hx = (tfx < 9)? (tfx / 3 * 3 + k): ((tfx - 9) / 3 + k * 3); hx += 18; Bit81 BX = BPnoB2 & pHouseCells[hx]; if (BX.IsZero()) { continue; } houseLst.Add(BX); tfxLst.Add(hx); } if (houseLst.Count < 2) { continue; } Permutation prm = new Permutation(houseLst.Count, 2); while (prm.Successor()) { int na = prm.Index[0], nb = prm.Index[1]; Bit81 HA = houseLst[na]; Bit81 HB = houseLst[nb]; if (!(HA & HB).IsZero()) { continue; } UGrCells LA = new UGrCells(tfxLst[na], no); UGrCells LB = new UGrCells(tfxLst[nb], no); foreach (var P in HA.IEGetUCeNoB(pBDL, 0x1FF)) { LA.Add(P); } foreach (var P in HB.IEGetUCeNoB(pBDL, 0x1FF)) { LB.Add(P); } SetGroupedLink(tfxLst[nb], W, no, LA, LB); if (!(BPnoB2 - (HA | HB)).IsZero()) { continue; } SetGroupedLink(tfxLst[nb], S, no, LA, LB); } } //------------------------------------------ for (int tfx = 18; tfx < 27; tfx++) { int bx = tfx - 18; int b0 = (bx / 3 * 27 + (bx % 3) * 3); //Cell number at the top left of the block Bit81 BPnoB2 = BPnoB & pHouseCells[tfx]; List <Bit81> houseLst = new List <Bit81>(); List <int> tfxLst = new List <int>(); for (int k = 0; k < 3; k++) { int r0 = (b0 / 9 + k); Bit81 BX = BPnoB2 & pHouseCells[r0]; if (BX.IsZero()) { continue; } houseLst.Add(BX); tfxLst.Add(r0); } for (int k = 0; k < 3; k++) { int c0 = (b0 % 9) + k; Bit81 BX = BPnoB2 & pHouseCells[c0 + 9]; if (BX.IsZero()) { continue; } houseLst.Add(BX); tfxLst.Add(c0 + 9); } if (houseLst.Count >= 2) { Permutation prm = new Permutation(houseLst.Count, 2); while (prm.Successor()) { int na = prm.Index[0], nb = prm.Index[1]; Bit81 HA = houseLst[na]; Bit81 HB = houseLst[nb] - HA; if (HB.IsZero()) { continue; } UGrCells LA = new UGrCells(tfxLst[na], no); UGrCells LB = new UGrCells(tfxLst[nb], no); foreach (var P in HA.IEGetUCeNoB(pBDL, 0x1FF)) { LA.Add(P); } foreach (var P in HB.IEGetUCeNoB(pBDL, 0x1FF)) { LB.Add(P); } SetGroupedLink(tfxLst[nb], W, no, LA, LB); if (!(BPnoB2 - (HA | HB)).IsZero()) { continue; } SetGroupedLink(tfxLst[nb], S, no, LA, LB); } } if (houseLst.Count >= 1) { for (int na = 0; na < houseLst.Count; na++) { Bit81 HA = houseLst[na]; UGrCells LA = new UGrCells(tfxLst[na], no); foreach (var P in HA.IEGetUCeNoB(pBDL, 0x1FF)) { LA.Add(P); } foreach (var rc in BPnoB2.IEGet_rc()) { if (HA.IsHit(rc)) { continue; } UGrCells LB = new UGrCells(-9, no, pBDL[rc]); SetGroupedLink(tfxLst[na], W, no, LA, LB); SetGroupedLink(-9, W, no, LB, LA); } } } } } } catch (Exception ex) { WriteLine(ex.Message); WriteLine(ex.StackTrace); } }
public bool ForceChain_HouseEx( ) { if (GNPXApp000.GMthdOption["ForceChainCellHouseOn"] != "1") { return(false); } GroupedLink._IDsetB = false; //ID set for debug Prepare(); pSprLKsMan.PrepareSuperLinkMan(AllF: true); string dspOpt = GNPXApp000.GMthdOption["ForceLx"]; Bit81[] sPass = new Bit81[9]; for (int k = 0; k < 9; k++) { sPass[k] = new Bit81(); } bool solvedA = false; for (int hs0 = 0; hs0 < 27; hs0++) { int noBs = pBDL.IEGetCellInHouse(hs0).Aggregate(0, (Q, P) => Q | (P.FreeB)); bool solvedB = false; foreach (var no0 in noBs.IEGet_BtoNo()) { int noB = (1 << no0); Bit81[] sTrue = new Bit81[9]; for (int k = 0; k < 9; k++) { sTrue[k] = new Bit81(all_1: true); } foreach (var P0 in pBDL.IEGetCellInHouse(hs0, noB)) { USuperLink USLK = pSprLKsMan.get_L2SprLK(P0.rc, no0, FullSearchB: false, DevelopB: false); if (USLK == null || !USLK.SolFound) { goto nextSearch; } for (int k = 0; k < 9; k++) { sTrue[k] &= (USLK.Qtrue[k] - USLK.Qfalse[k]); sTrue[k].BPReset(P0.rc); } } for (int nox = 0; nox < 9; nox++) { if (!sTrue[nox].IsZero()) { solvedB = true; break; } } if (solvedB) { solvedA = true; _ForceChainHouseDispEx(sPass, sTrue, hs0, no0); if (!SDK_Ctrl.MltAnsSearch && dspOpt == "ForceL0") { return(true); } } } if (solvedA && dspOpt == "ForceL1") { _ForceChainHouseDispEx(sPass, null, hs0, -1); if (!SDK_Ctrl.MltAnsSearch) { return(true); } } nextSearch: continue; } if (solvedA && !SDK_Ctrl.MltAnsSearch && dspOpt == "ForceL2") { _ForceChainHouseDispEx(sPass, null, -1, -1); } return(SolCode > 0); }
public IEnumerable <UBasCov> IEGet_BaseSet(int sz, int rnk) { if (UGLLst == null) { yield break; } __checkCC = 0; List <UGLink> basUGLs = new List <UGLink>(); Bit981 HB981 = new Bit981(); //BaseSet bitPattern Bit324 usedLK = new Bit324(); //usedLink(by serial number) List <int> usedLKLst = new List <int>(); Bit81Chk coverChk = new Bit81Chk(sz, rnk, basUGLs, usedLKLst, usedLKIgnrLst); var cmbBas = new Combination(UGLLst.Count, sz); long rcbn36 = 0; int jkC = 0; int nxt = int.MaxValue; //(skip function) while (cmbBas.Successor(nxt)) { GeneralLogicGen.ChkBas1++; //***** jkC++; //*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==* sz=1 if (sz == 1) { UGLink UGL = UGLLst[cmbBas.Index[0]]; if (UGL.UC is UCell) { goto LNextSet; // if size=1, cell-type is invalid } // UGL.rcBit81 is Bit81 int blkB = (int)UGL.Get_rcbnFrame(2); if (blkB.BitCount() > 1) { goto LNextSet; } int no = UGL.rcBit81.no, b = blkB.BitToNum(); var P = UGLLst.Find(U => U.Equal_no_block(no, b)); if (P == null) { goto LNextSet; } if ((P.rcBit81 - UGL.rcBit81).Count == 0) { goto LNextSet; } //there are numbers only within one block HB981.Clear(); HB981.BPSet(UGL.rcBit81.no, UGL.rcBit81, tfbSet: false); basUGLs.Clear(); basUGLs.Add(UGL); GeneralLogicGen.ChkBas2++; //***** goto LBSFound; //possibility of solution } //*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==* sz>=2 HB981.Clear(); basUGLs.Clear(); usedLKLst.Clear(); coverChk.Clear(); rcbn36 = 0; //1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 for (int k = 0; k < sz; k++) { nxt = k; UGLink UGL = UGLLst[cmbBas.Index[k]]; rcbn36 |= UGL.rcbnFrame2; if (!Check_rcbnCondition(sz, rnk, k, rcbn36)) { goto LNextSet; //# Extremely efficient } if (UGL.rcBit81 is Bit81) // ............ rcb ............ { if (k > 0 && HB981.IsHit(UGL.rcBit81.no, UGL.rcBit81)) { goto LNextSet; //included in BaseSet } HB981.BPSet(UGL.rcBit81.no, UGL.rcBit81, tfbSet: true); //register to BaseSet usedLKLst.Add(UGL.rcBit81.ID); //tfx<<4 | no int no = UGL.rcBit81.no; coverChk.noB |= 1 << no; //qq } else // ........... Cell ............ { UCell UC = UGL.UC; int rc = UC.rc; foreach (var n in UC.FreeB.IEGet_BtoNo(9)) { if (k > 0 && HB981.IsHit(n, rc)) { goto LNextSet; } HB981.BPSet(n, rc, tfbSet: true); } int IDrc = rc << 17 | 1 << 16; usedLKLst.Add(IDrc); //tfx<<4 | no } basUGLs.Add(UGL); //qq } GeneralLogicGen.ChkBas2++; //***** bool niceB = coverChk.Check_BaseSetCondition(HB981); //########## check_rcB9 ########## if (!niceB) { goto LNextSet; } //1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 if (sz >= 2) { int noBP = HB981.nzBit; int noCC = noBP.BitCount(); if (noCC >= 2) { List <int> IX = noBP.IEGet_BtoNo().ToList(); if (sz >= 2) { for (int k = 0; k < noCC; k++) { Bit81 A = new Bit81(), B = new Bit81(); int no = IX[k]; foreach (var P in basUGLs) { if (P.rcBit81 is Bit81) { if (P.rcBit81.no == no) { A |= P.rcBit81; } else { B |= P.rcBit81; } } else { int rc = P.UC.rc; foreach (var n in P.UC.FreeB.IEGet_BtoNo()) { if (n == no) { A.BPSet(rc); } else { B.BPSet(rc); } } } } int nOL = (B & A).Count; if (rnk == 0 && nOL < 2) { goto LNextSet; } if (rnk > 0 && nOL < 1) { goto LNextSet; } // WriteLine("---------- no:{0} sz:{1} rnk:{2} nOL:{3} (A-B):{4} (B-A):{5}", no, sz,rnk, nOL, (A-B).Count, (B-A).Count ); } //1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 } } } //--------------------------------------------------------------------------------------- LBSFound: if (SDK_Ctrl.UGPMan.stageNo == 12 && sz >= 3)// && rnk==1 ){ { WriteLine("\r sz:{0} rnk:{1} jkc:{2}", sz, rnk, jkC); Check_rcbnCondition(sz, rnk, sz, rcbn36, printB: true); basUGLs.ForEach(P => WriteLine(P.ToString("BaseSet"))); } usedLK.Clear(); basUGLs.ForEach(P => usedLK.BPSet(P.SrNum)); //IDrc: rc<<17 | 1<<16 UBasCov UBC = new UBasCov(basUGLs, HB981, sz, usedLK); yield return(UBC); LNextSet: continue; } yield break; }
public bool Check_BaseSetCondition(Bit981 HB981) //for sz>=2 { bool niceB = false, E = false; jkC++; #region There is a link between the link and the other link group for (int k = 0; k < sz; k++) { UGLink UGL = basUGLs[k]; Bit981 Bcum = new Bit981(); for (int m = 0; m < sz; m++) { if (m == k) { continue; } Bcum |= basUGLs[m].rcbn2; } if ((Bcum & UGL.rcbn_conn2).Count <= 0) { goto Lreturn; //false } } #endregion GeneralLogicGen.ChkBas3++; //***** #region Every element of the number(#no) has a link //それぞれのリンクは、その他のリンク群と連結する。 //リンクしないセルはrnk個数以下でなければならない。 //if(E) for(int n=0; n<9; n++ ) WriteLine(HB981.tfx27Lst[n].ToBitString(27)); Bit981 Q9 = HB981.Copy(); usedLKLst.ForEach(p => HB981.tfxReset(p & 0xF, p >> 4)); usedLKIgnrLst.ForEach(p => HB981.tfxReset(p & 0xF, p >> 4)); //if(E) for(int n=0; n<9; n++ ) WriteLine(HB981.tfx27Lst[n].ToBitString(27)); int QPP = 0; foreach (var no in noB.IEGet_BtoNo()) { Bit81 Q = Q9._BQ[no];//new Bit81(HB981._BQ[no]); //if(E) WriteLine( "HB981.tfxLst[no]: "+HB981.tfx27Lst[no].ToBitString(27) ); foreach (var tfx in HB981.tfx27Lst[no].IEGet_tfb()) { int bp = HB981.GetBitPattern_tfnx(no, tfx); if (bp <= 0) { continue; } int nc = bp.BitCount(); if (nc >= 2) { if (E) { WriteLine("Q: " + Q.ToString()); } foreach (var nx in bp.IEGet_BtoNo()) { int rc = tfx.Get_tfx_rc(nx); Q.BPReset(rc); } //if(E) WriteLine("Q: "+Q.ToString() ); } } QPP += Q.Count; } if (QPP <= rnk) { niceB = true; goto Lreturn; } #endregion GeneralLogicGen.ChkBas3++; //***** #region There is a possibility link between the patterns of numbers n1,n2 //数字間のリンクしないセルは、rnk*2以下でなければならない。 if (HB981.nzBit.BitCount() >= 2) { bool firstB = true; Bit81 Q = null; foreach (var n in HB981.nzBit.IEGet_BtoNo()) { if (firstB) { Q = HB981._BQ[n]; firstB = false; } else { Q |= HB981._BQ[n]; } } foreach (var rc in Q.IEGetRC()) { if (HB981.GetBitPattern_rcN(rc).BitCount() >= 2) //セルrcの数字間リンクがあるときは、Q9リセット { foreach (var no in noB.IEGet_BtoNo()) { Q9._BQ[no].BPReset(rc); } } } int Q9cc = Q9.BitCount(); if (Q9cc <= rnk) { niceB = true; goto Lreturn; } //直接リンクしないセルがrnk以下のときは、次のテストに進む if (Q9cc <= rnk * 2) { GeneralLogicGen.ChkBas1B++; //***** foreach (var n1 in Q9.nzBit.IEGet_BtoNo()) { foreach (var rc in Q9._BQ[n1].IEGetRC()) { foreach (var n2 in Q9.nzBit.IEGet_BtoNo().Where(nx => nx != n1)) { if ((Q9._BQ[n2] & p_connectedCells[rc]).BitCount() == 0) { continue; } //リンクしないセル間を繋ぐリンクがある(”可能性あり”で次のテストに進む GeneralLogicGen.ChkBas1A++; //***** niceB = true; goto Lreturn; } } } } } #endregion GeneralLogicGen.ChkBas4++; //***** Lreturn: return(niceB); }
//1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 public IEnumerable <UBasCov> IEGet_CoverSet(UBasCov UBC, int rnk) { if (UGLLst == null) { yield break; } List <UGLink> basUGLs = UBC.basUGLs; Bit981 HB981 = UBC.HB981; Bit324 usedLK = UBC.usedLK; int nzBit = HB981.nzBit; int nzBitCC = nzBit.BitCount(); int sz = UBC.sz; List <UGLink> UGLCovLst = new List <UGLink>(); // UGLCovLst:candidate link #region Create UGLCovLst Bit81 Bcmp = HB981.CompressToHitCells(); foreach (var P in UGLLst.Where(q => !usedLK.IsHit(q.SrNum))) { if (P.rcBit81 is Bit81) //Row, column, block link { if ((nzBit & (1 << P.rcBit81.no)) == 0) { continue; } int B = (HB981._BQ[P.rcBit81.no] & P.rcBit81).BitCount(); if (B == 0) { continue; } // if rank:0, the CoverSet has two or more common items if (rnk == 0 && (HB981._BQ[P.rcBit81.no] & P.rcBit81).Count < 2) { continue; } UGLCovLst.Add(P); } else //Cell { if (nzBitCC <= 1) { continue; } if ((nzBit & P.UC.FreeB) == 0) { continue; } int B = nzBit & P.UC.FreeB; if (B == 0) { continue; } int rc = P.UC.rc; int kcc = 0, kccLim = (rnk == 0)? 2:1; foreach (var no in B.IEGet_BtoNo()) { if (!HB981._BQ[no].IsHit(rc)) { continue; } if (++kcc >= kccLim) { UGLCovLst.Add(P); break; } } } } #endregion if (UGLCovLst.Count < sz + rnk) { yield break; } Bit981 HC981 = new Bit981(); Bit981 HLapB = new Bit981(); Bit981 Can981 = new Bit981(); Combination cmbCvr = new Combination(UGLCovLst.Count, sz + rnk); int nxt = int.MaxValue; while (cmbCvr.Successor(nxt)) { ++GeneralLogicGen.ChkCov1; HC981.Clear(); Array.ForEach(cmbCvr.Index, m => HC981 |= UGLCovLst[m].rcbn2); if (!(HB981 - HC981).IsZero()) { goto LNextSet; //BaseSet is covered? } Bit981 CsubB = HC981 - HB981; if (CsubB.IsZero()) { goto LNextSet; //excludable candidates is exist? } List <UGLink> covUGLs = new List <UGLink>(); Array.ForEach(cmbCvr.Index, m => covUGLs.Add(UGLCovLst[m])); if (rnk == 0) { Can981 = CsubB; } else //if(rnk>0){ { bool SolFound = false; foreach (int n in CsubB.nzBit.IEGet_BtoNo()) { foreach (int rc in CsubB._BQ[n].IEGetRC()) { int kc = covUGLs.Count(Q => Q.IsHit(n, rc)); if (kc == rnk + 1) { Can981.BPSet(n, rc); SolFound = true; } } } if (!SolFound) { continue; } } ++GeneralLogicGen.ChkCov2; //***** UBC.addCoverSet(covUGLs, HC981, Can981, rnk); yield return(UBC); LNextSet: continue; } yield break; }
//Coloring is an algorithm that connects the focused digit with a strong link. //http://csdenpe.web.fc2.com/page46.html public bool Color_Trap( ) { Prepare(); CeLKMan.PrepareCellLink(1); //Generate StrongLink for (int no = 0; no < 9; no++) { int noB = (1 << no); foreach (Bit81[] CRL in _Coloring(no)) { Bit81 HitB = new Bit81(); Bit81 ELM = (new Bit81(pBDL, noB)) - (CRL[0] | CRL[1]); foreach (var rc in ELM.IEGet_rc()) { Bit81 HB = HouseCells[18 + rc.ToBlock()]; if (((ConnectedCells[rc] - HB) & CRL[0]).IsZero()) { continue; } if (((ConnectedCells[rc] - HB) & CRL[1]).IsZero()) { continue; } HitB.BPSet(rc); } if (!HitB.IsZero()) { Color Cr = _ColorsLst[0]; Color Cr1 = Cr; Cr1.A = (byte)120; //Color.FromArgb(120,Cr.R,Cr.G,Cr.B); foreach (var P in HitB.IEGet_rc().Select(p => pBDL[p])) { P.CancelB = noB; } foreach (var P in CRL[0].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr); } foreach (var P in CRL[1].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr1); } SolCode = 2; string SolMsg = "Coloring Trap #" + (no + 1); Result = SolMsg; if (SolInfoB) { ResultLong = SolMsg; } if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } HitB = new Bit81(); } } } return(false); }
public IEnumerable <UFish> IEGet_BaseSet(int BaseSel, bool FinnedF = false, bool EndoFlg = false) { if (HBLst == null) { yield break; } bool basicFish = (BaseSel.BitCount() <= 9) & !FinnedF; //not F/M & notF/M int BaseSelR = 0x3FFFF ^ BaseSel; GeneralLogicGen.ChkBas1 = 0; GeneralLogicGen.ChkBas2 = 0; Bit81 Q; Combination cmbBas = new Combination(HBLst.Count, sz); int nxt = int.MaxValue; while (cmbBas.Successor(nxt)) { int chk1 = ++GeneralLogicGen.ChkBas1; int usedLK = 0; Bit81 HB81 = new Bit81(); Bit81 OHB81 = new Bit81(); int rcbB = 0; for (int k = 0; k < sz; k++) { nxt = k; int nx = cmbBas.Index[k]; Bit81F HBF = HBLst[nx]; if (((1 << HBF.ID) & BaseSel) == 0) { goto nxtCmb; } if (!(Q = HB81 & HBF).IsZero()) //overlap { if (!EndoFlg) { goto nxtCmb; } OHB81 |= Q; } usedLK |= 1 << HBF.ID; //house Number HB81 |= HBF; //Bit81 rcbB |= HBF.rcbB; if (basicFish && k > 0 && (rcbB & BaseSelR).BitCount() > sz) { goto nxtCmb; } } if (extFlag && !IsLinked9(HB81)) { continue; } int chk2 = ++GeneralLogicGen.ChkBas2; UFish UF = new UFish(no, sz, usedLK, HB81, OHB81); //if(sz>=3 && BaseSel==0x7FFFFFF) Debug_PattenPrint(UF); yield return(UF); nxtCmb: continue; } yield break; }
//=================== MultiColoring ================================== public bool MultiColor_Type1( ) { Prepare(); CeLKMan.PrepareCellLink(1); //Generate StrongLink for (int no = 0; no < 9; no++) { int noB = (1 << no); List <Bit81[]> MCRL = _Coloring(no).ToList(); if (MCRL == null || MCRL.Count < 2) { continue; } var cmb = new Combination(MCRL.Count, 2); while (cmb.Successor()) { Bit81[] CRLa = MCRL[cmb.Index[0]]; Bit81[] CRLb = MCRL[cmb.Index[1]]; for (int na = 0; na < 2; na++) { Bit81 HCRLa = new Bit81(); foreach (var rc in CRLa[na].IEGet_rc()) { HCRLa |= ConnectedCells[rc]; } for (int nb = 0; nb < 2; nb++) { if ((HCRLa & CRLb[nb]).IsZero()) { continue; } Bit81 BD0 = new Bit81(pBDL, noB); Bit81 ELMtry = BD0 - (CRLa[na] | CRLb[nb] | CRLa[1 - na] | CRLb[1 - nb]); if (ELMtry.Count == 0) { continue; } bool solF = false; Bit81 ELM = new Bit81(); foreach (var rc in ELMtry.IEGet_rc()) { if (!ConnectedCells[rc].IsHit(CRLa[1 - na])) { continue; } if (!ConnectedCells[rc].IsHit(CRLb[1 - nb])) { continue; } pBDL[rc].CancelB = noB; ELM.BPSet(rc); solF = true; } if (solF) { Color CrA = _ColorsLst[0]; foreach (var P in ELM.IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, CrA); } for (int k = 0; k < 2; k++) { Bit81[] CRLX = MCRL[cmb.Index[k]]; Color Cr1 = _ColorsLst[k]; Color Cr2 = Cr1; Cr2.A = (byte)120; foreach (var P in CRLX[1 - k].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr1); } foreach (var P in CRLX[k].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr2); } } SolCode = 2; string SolMsg = "MultiColoring Type1 #" + (no + 1); Result = SolMsg; if (SolInfoB) { ResultLong = SolMsg; } if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } solF = false; } } } } } return(false); }
private int _NL_CheckSolution(UCellLink LK0, UCellLink LKnxt, Stack <UCellLink> SolStack, Bit81 UsedCells) { bool SolFound = false; int SolType = CeLKMan.Check_CellCellSequence(LKnxt, LK0)? 1: 2; //1:Continuous 2:DisContinuous if (SolType == 1) //===== continuous ===== //=== Change WeakLink to StrongLink { List <UCellLink> SolLst = SolStack.ToList(); Bit81 UsedCellsT = UsedCells | (new Bit81(LK0.rc1)); foreach (var L in SolLst) { int noB = 1 << L.no; foreach (var P in pBDL.IEGetCellInHouse(L.tfx, noB)) { if (UsedCellsT.IsHit(P.rc)) { continue; } P.CancelB |= noB; SolFound = true; } } //=== S-S (There are no other numbers) SolLst.Reverse(); SolLst.Add(LK0); var LKpre = SolLst[0]; foreach (var LK in SolLst.Skip(1)) { if (LKpre.type == 1 && LK.type == 1) //S-S { UCell P = pBDL[LK.rc1]; int noB = P.FreeB.DifSet((1 << LKpre.no) | (1 << LK.no)); if (noB > 0) { P.CancelB = noB; SolFound = true; } } LKpre = LK; } if (SolFound) { SolCode = 2; } } else if (SolType == 2) //===== discontinuous ===== { UCell P = pBDL[LK0.UCe1.rc]; //(for MultiAns code) int dcTyp = LK0.type * 10 + LKnxt.type; switch (dcTyp) { case 11: P.FixedNo = LK0.no + 1; //Cell number determination P.CancelB = P.FreeB.DifSet(1 << (LK0.no)); SolCode = 1; SolFound = true; //(1:Fixed) break; case 12: P.CancelB = 1 << LKnxt.no; SolCode = 2; SolFound = true; break;//(2:Exclude from candidates) case 21: P.CancelB = 1 << LK0.no; SolCode = 2; SolFound = true; break; case 22: if (LK0.no == LKnxt.no) { P.CancelB = 1 << LK0.no; SolFound = true; SolCode = 2; } break; } } if (SolFound) { return(SolType); } return(-1); }
public bool Color_Wrap( ) { Prepare(); CeLKMan.PrepareCellLink(1); //Generate StrongLink for (int no = 0; no < 9; no++) { int noB = (1 << no); foreach (Bit81[] CRL in _Coloring(no)) { Bit81 BD0 = new Bit81(pBDL, noB); Bit81 ELM = BD0 - (CRL[0] | CRL[1]); if (ELM.Count == 0) { continue; } for (int k = 0; k < 2; k++) { for (int dr = 0; dr < 27; dr++) { if ((CRL[k] & HouseCells[dr]).Count < 2) { continue; } Color CrA = _ColorsLst[1]; Color Cr = _ColorsLst[0]; Color Cr1 = Cr; Color Cr2 = Color.FromArgb(120, Cr.R, Cr.G, Cr.B); foreach (var P in ELM.IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, CrA); } foreach (var P in CRL[1 - k].IEGet_rc().Select(p => pBDL[p])) { P.SetNoBBgColor(noB, AttCr, Cr1); } foreach (var P in CRL[k].IEGet_rc().Select(p => pBDL[p])) { P.SetCellBgColor(Cr2); P.CancelB = noB; } SolCode = 2; string SolMsg = "Coloring Wrap #" + (no + 1); Result = SolMsg; if (SolInfoB) { ResultLong = SolMsg; } if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } } } } } return(false); }
private bool _NL_Search(UCellLink LK0, UCellLink LKpre, Stack <UCellLink> SolStack, Bit81 UsedCells, int szCtrl) { if (szCtrl <= 0) { return(false); } foreach (var LKnxt in CeLKMan.IEGet_CeCeSeq(LKpre)) //links that satisfy concatenation conditions { int rc2Nxt = LKnxt.rc2; if (UsedCells.IsHit(rc2Nxt)) { continue; //UsedCells does not include Origin Cell } { //===== Chain Search ===== SolStack.Push(LKnxt); //___Debug_Print_NLChain(SolStack); if (rc2Nxt == LK0.rc1 && szCtrl == 1) { if (SolStack.Count > 2) //Loop was formed (the next cell matches the Origin Cell) { int SolType = _NL_CheckSolution(LK0, LKnxt, SolStack, UsedCells); //Solved? if (SolType > 0) { if (SolInfoB) { _NL_SolResult(LK0, LKnxt, SolStack, SolType); } if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(false)) { return(true); } } } } else { Bit81 UsedCellsNxt = UsedCells | (new Bit81(rc2Nxt)); //Create a new bit representation of used cell _NL_Search(LK0, LKnxt, SolStack, UsedCellsNxt, szCtrl - 1); //Next step Search(recursive call if (SolCode > 0) { return(true); } } SolStack.Pop(); //Failure(Cancel link extension processing) } //----------------------------- } return(false); }
public bool ForceChain_CellEx( ) { if (GNPXApp000.GMthdOption["ForceChainCellHouseOn"] != "1") { return(false); } GroupedLink._IDsetB = false; //ID set for debug Prepare(); pSprLKsMan.PrepareSuperLinkMan(AllF: true); string dspOpt = GNPXApp000.GMthdOption["ForceLx"]; Bit81[] sPass = new Bit81[9]; for (int k = 0; k < 9; k++) { sPass[k] = new Bit81(); } bool solvedA = false; foreach (var P0 in pBDL.Where(p => (p.FreeB > 0))) //origin cell { Bit81[] sTrue = new Bit81[9]; for (int k = 0; k < 9; k++) { sTrue[k] = new Bit81(all_1: true); } bool solvedB = false; foreach (var no0 in P0.FreeB.IEGet_BtoNo()) { int noB = (1 << no0); USuperLink USLK = pSprLKsMan.get_L2SprLKEx(P0.rc, no0, FullSearchB: true, DevelopB: false); if (USLK == null || !USLK.SolFound) { goto nextSearch; } for (int k = 0; k < 9; k++) { sTrue[k] &= (USLK.Qtrue[k] - USLK.Qfalse[k]); } } for (int nox = 0; nox < 9; nox++) { sTrue[nox].BPReset(P0.rc); if (!sTrue[nox].IsZero()) { solvedB = true; } } if (solvedB) { solvedA = true; _ForceChainCellDispEx(sPass, sTrue, P0.rc); //q if (!SDK_Ctrl.MltAnsSearch && dspOpt != "ForceL2") { break; } } nextSearch: continue; } if (solvedA && !SDK_Ctrl.MltAnsSearch && dspOpt == "ForceL2") { _ForceChainCellDispEx(sPass, null, -1); } return(SolCode > 0); }
public bool SueDeCoq( ) { if (pAnMan.GStage != GStageMemo) { GStageMemo = pAnMan.GStage; fALS.Initialize(); fALS.PrepareALSLinkMan(+2); //Generate ALS(+1 & +2) } if (fALS.ALSLst.Count <= 3) { return(false); } foreach (var ISPB in fALS.ALSLst.Where(p => p.tfx >= 18 && p.Size >= 3)) //Selecte Block-type ALS { if (ISPB.rcbRow.BitCount() <= 1 || ISPB.rcbCol.BitCount() <= 1) { continue; //Block squares have multiple rows and columns } foreach (var ISPR in fALS.ALSLst.Where(p => p.tfx < 18 && p.Size >= 3)) { //Selecte Row-type/Column-type ALS if ((ISPR.rcbBlk & ISPB.rcbBlk) == 0) { continue; //Intersect with ISPB } if (ISPR.rcbBlk.BitCount() < 2) { continue; //ISPR has multiple blocks } //Are the cell configurations of the intersections the same? if ((ISPB.B81 & HouseCells[ISPR.tfx]) != (ISPR.B81 & HouseCells[ISPB.tfx])) { continue; } // ***** the code follows HP -> http://csdenpe.web.fc2.com/page45.html ***** Bit81 IS = ISPB.B81 & ISPR.B81; //Intersection if (IS.Count < 2) { continue; } //At least 2 cells at the intersection if ((ISPR.B81 - IS).Count == 0) { continue; //There is a part other than the intersecting part in ISPR } Bit81 PB = ISPB.B81 - IS; //ISPB's outside IS Bit81 PR = ISPR.B81 - IS; //ISPR's outside IS int IS_FreeB = IS.AggregateFreeB(pBDL); //Intersection number int PB_FreeB = PB.AggregateFreeB(pBDL); //ISPB's number outside the IS int PR_FreeB = PR.AggregateFreeB(pBDL); //ISPR's number outside the IS if ((IS_FreeB & PB_FreeB & PR_FreeB) > 0) { continue; } //A.DifSet(B)=A-B=A&(B^0x1FF) int PB_FreeBn = PB_FreeB.DifSet(IS_FreeB); //Numbers not at the intersection of PB int PR_FreeBn = PR_FreeB.DifSet(IS_FreeB); //Numbers not in the intersection of PR int sdqNC = PB_FreeBn.BitCount() + PR_FreeBn.BitCount(); //Number of confirmed numbers outside the intersection if ((IS_FreeB.BitCount() - IS.Count) != (PB.Count + PR.Count - sdqNC)) { continue; } int elmB = PB_FreeB | IS_FreeB.DifSet(PR_FreeB); //Exclusion Number in PB int elmR = PR_FreeB | IS_FreeB.DifSet(PB_FreeB); //Exclusion Number in PR if (elmB == 0 && elmR == 0) { continue; } foreach (var P in _GetRestCells(ISPB, elmB)) { P.CancelB |= P.FreeB & elmB; SolCode = 2; } foreach (var P in _GetRestCells(ISPR, elmR)) { P.CancelB |= P.FreeB & elmR; SolCode = 2; } if (SolCode > 0) //--- SueDeCoq found ----- { SolCode = 2; SuDoQueEx_SolResult(ISPB, ISPR); if (ISPB.Level >= 3 || ISPB.Level >= 3) { WriteLine("Level-3"); } if (__SimpleAnalizerB__) { return(true); } if (!pAnMan.SnapSaveGP(true)) { return(true); } } } } return(false); }