Ejemplo n.º 1
0
        public UGrCells Copy()
        {
            UGrCells Pcpy = new UGrCells(tfx, no);

            Pcpy.B81 = B81.Copy();
            return(Pcpy);
        }
        public IEnumerable <GroupedLink> IEGet_SuperLinkFirst(UCell UC, int no)
        {
            List <UCellLink> Plst = CeLKMan.CeLK81[UC.rc];

            if (Plst != null)
            {
                foreach (var LK in Plst.Where(p => ((p.no == no) && (p.type == W))))
                {
                    yield return(new GroupedLink(LK));
                }
                foreach (var LK in Plst.Where(p => ((p.no != no) && (p.type == S))))
                {
                    yield return(new GroupedLink(LK));
                }
            }

            UGrCells GUC = new UGrCells(-9, no, UC);

            if (GNPXApp000.GMthdOption["GroupedCells"] == "1")
            {
                foreach (var GLK in GLKMan.GrpCeLKLst)
                {
                    if (GLK.UGCellsA.Equals(GUC))
                    {
                        yield return(GLK);
                    }
                }
            }

            //first link is not ALS
            yield break;
        }
        private void SetGroupedLink(int tfx, int type, int no, UGrCells LA, UGrCells LB, bool Print = false)
        {
            if (LA.Count == 0 || LB.Count == 0)
            {
                return;
            }
            if (LA.Count == 1 && LB.Count == 1)
            {
                return;
            }
            GroupedLink GrpLK = new GroupedLink(LA, LB, tfx, type);
            int         ix    = GrpCeLKLst.FindIndex(P => (P.Equals(GrpLK)));

            if (ix >= 0)
            {
                return;
            }
            GrpCeLKLst.Add(GrpLK);

            if (Print)
            {
                WriteLine($"\r*LA tfx:{tfx} type:{type} no:{(no+1)}");
                LA.ForEach(P => WriteLine(P));
                WriteLine("*LB");
                LB.ForEach(P => WriteLine(P));
            }
        }
 public ALSLink(UALS ALSbase, UGrCells UGCellsA, UGrCells UGCellsB, int tfx)
 {
     this.ALSbase  = ALSbase;
     this.UGCellsA = UGCellsA;
     this.UGCellsB = UGCellsB;
     this.tfx      = tfx;
     this.type     = S;
 }
Ejemplo n.º 5
0
 public bool Equals(UGrCells other)
 {
     return(other != null &&
            //tfx == other.tfx &&      //Difference in tfx is acceptable. Important as an algorithm.
            no == other.no &&
            EqualityComparer <Bit81> .Default.Equals(B81, other.B81) &&
            FreeB == other.FreeB);
 }
Ejemplo n.º 6
0
        public GroupedLink(UGrCells UGCellsA, UGrCells UGCellsB, int tfx, int type) : this()
        {
            this.UGCellsA = UGCellsA; this.UGCellsB = UGCellsB; //this.tfx=tfx;
            this.type     = type;

            FreeB = UGCellsA.Aggregate(0, (Q, P) => Q | P.FreeB);
            FreeB = UGCellsB.Aggregate(FreeB, (Q, P) => Q | P.FreeB);
        }
Ejemplo n.º 7
0
        public UGrCells SelectNoCells(int no)
        {
            int          noB  = 1 << no;
            List <UCell> UCsS = UCellLst.FindAll(Q => (Q.FreeB & noB) > 0);
            UGrCells     GCs  = new UGrCells(tfx, no);

            GCs.Add(UCsS);
            return(GCs);
        }
Ejemplo n.º 8
0
        public GroupedLink(UCellLink LK) : this()
        {
            UCelLK    = LK;
            UGCellsA  = new UGrCells(LK.tfx, LK.no, LK.UCe1);
            UGCellsB  = new UGrCells(LK.tfx, LK.no, LK.UCe2);
            this.type = LK.type;
            this.tfx  = LK.tfx;

            FreeB = UGCellsA.Aggregate(0, (Q, P) => Q | P.FreeB);
            FreeB = UGCellsB.Aggregate(FreeB, (Q, P) => Q | P.FreeB);
        }
 public IEnumerable <GroupedLink> IEGet_GlkNoType(UGrCells GLK, int no, int typB)
 {
     foreach (var P in GrpCeLKLst)
     {
         if (P.UGCellsA == GLK)
         {
             yield return(P);
         }
     }
     yield break;
 }
        private bool _GroupedNL_LoopCheck(GroupedLink GLK0, GroupedLink GLKnxt)
        {
            UGrCells Qorg = GLK0.UGCellsA;
            UGrCells Qdst = GLKnxt.UGCellsB;

            if (Qdst.Count != 1)
            {
                return(false);
            }
            return(Qdst[0].rc == Qorg[0].rc);
        }
        //Link in ALS(when 1-number is excluded and locked, other digits are in one house)
        public void QSearch_AlsInnerLink()
        {
            if (ALSLst == null)
            {
                PrepareALSLinkMan(1);
            }
            if (ALSLst.Count == 0)
            {
                return;
            }
            if (AlsInnerLink != null)
            {
                return;
            }

            foreach (var Pals in ALSLst.Where(p => (p.Size >= 2 && p.singly)))
            {
                List <int>  noLst = Pals.FreeB.IEGet_BtoNo().ToList();
                Permutation prm   = new Permutation(noLst.Count, 2);//using permutation
                while (prm.Successor(2))
                {
                    int noS = noLst[prm.Index[0]];
                    int noD = noLst[prm.Index[1]];

                    UGrCells GS = Pals.SelectNoCells(noS); //ALS cell group with noS
                    UGrCells GD = Pals.SelectNoCells(noD); //ALS cell group with noD

                    Bit81 B81D = new Bit81();
                    GD.ForEach(q => B81D.BPSet(q.rc));

                    for (int tfx = 0; tfx < 27; tfx++)
                    {
                        if (!(B81D - pHouseCells[tfx]).IsZero())
                        {
                            continue;
                        }
                        _SetGroupedLink(Pals, GS, GD, tfx);
                    }
                }
            }
            if (AlsInnerLink == null || AlsInnerLink.Count <= 0)
            {
                return;
            }
            AlsInnerLink.Sort();
            int ID = 0;

            AlsInnerLink.ForEach(P => P.ID = (ID++));
            //    AlsInnerLink.ForEach(P=>{
            //    //    if( P.ALSbase.Size==2 && P.tfx==18 ){
            //            WriteLine($"ALSLink {P.UGCellsA.GCToString()} -> tfx:{P.tfx} {P.UGCellsB.GCToString()}" );
            //   //    }
            //    } );
        }
        public bool Check_SuperLinkSequence(GroupedLink GLKpre, GroupedLink GLKnxt)
        {
            if (GLKpre == null)
            {
                WriteLine("null");
            }
            int typP = GLKpre.type;

            if (GLKpre is ALSLink)
            {
                typP = S;
            }
            int noP = GLKpre.no2;

            int typN = GLKnxt.type;
            int noN  = GLKnxt.no;

            UCellLink LKpre = GLKpre.UCelLK;
            UCellLink LKnxt = GLKnxt.UCelLK;

            int FreeBC = 0;

            if (LKpre != null)
            {
                FreeBC = pBDL[LKpre.rc2].FreeBC;

                if (LKnxt != null)   //singleLink -> singleLink
                {
                    return(_Check_SWSequenceSub(typP, noP, LKnxt.type, noN, FreeBC));
                }
                else                //singleLink -> multiLink
                {
                    UGrCells UGrCs = GLKnxt.UGCellsA;
                    if (UGrCs.Count == 1) //singleCell -> singleCell
                    {
                        return(_Check_SWSequenceSub(typP, noP, typN, noN, FreeBC));
                    }
                }
            }
            else if (GLKpre.UGCellsB.Count == 1 && LKnxt != null) // multiLink -> singleLink
            {
                FreeBC = GLKpre.UGCellsB.FreeB.BitCount();
                return(_Check_SWSequenceSub(typP, noP, typN, noN, FreeBC));
            }

            FreeBC = GLKpre.UGCellsB.FreeB.BitCount();
            return(_Check_SWSequenceSub(typP, noP, typN, noN, FreeBC));
        }
        private void _SetGroupedLink(UALS P, UGrCells GS, UGrCells GD, int tfx)
        {
            ALSLink ALSLK = new ALSLink(P, GS, GD, tfx);

            if (AlsInnerLink == null)
            {
                AlsInnerLink = new List <ALSLink>();
            }
            if (AlsInnerLink.Count > 0)
            {
                int ix = AlsInnerLink.FindIndex(Q => (Q.Equals(ALSLK)));
                if (ix >= 0)
                {
                    return;
                }
            }
            AlsInnerLink.Add(ALSLK);

            //WriteLine( $"ALSLink {GS.GCToString()} -> tfx:{tfx} {GD.GCToString()}" );
        }
Ejemplo n.º 14
0
        public GroupedLink(UCell UC, int no1, int no2, int type, bool rootF = false) : this()
        {
            this.rootF = rootF;
            int F = (1 << no1) | (1 << no2);

            // if( no1==no2 || (UC.FreeB&(1<<no1))==0 || (UC.FreeB&(1<<no2))==0 ){
            if ((UC.FreeB & (1 << no1)) == 0 || (UC.FreeB & (1 << no2)) == 0)
            {
                UGCellsA = UGCellsB = null;
                return;
            }

            UGCellsA  = new UGrCells(-1, no1, UC);
            UGCellsB  = new UGrCells(-1, no2, UC);
            UCelLK    = null;
            this.type = (UC.FreeBC == 2)? type: 2;          //2:WeakLink

            FreeB = UGCellsA.Aggregate(0, (Q, P) => Q | P.FreeB);
            FreeB = UGCellsB.Aggregate(FreeB, (Q, P) => Q | P.FreeB);
        }
Ejemplo n.º 15
0
        private bool _IsLooped(GroupedLink P)
        {
            UGrCells PB = P.UGCellsB;
            var      Q  = P.preGrpedLink as GroupedLink;
            int      nc = 0;

            while (Q != null)
            {
                UGrCells QA = Q.UGCellsA;
                if (QA.Equals(PB))
                {
                    return(true);
                }
                Q = Q.preGrpedLink as GroupedLink;
                if (++nc > 20)
                {
                    return(true);                   //БеБеБеБе ГoГOВаВш БеБеБеБе
                }
            }
            return(false);
        }
Ejemplo n.º 16
0
        public int CompareTo(object obj)
        {
            UGrCells Q = obj as UGrCells;

            if (this.no != Q.no)
            {
                return(this.no - Q.no);
            }
            if (this.Count != Q.Count)
            {
                return(this.Count - Q.Count);
            }
            for (int k = 0; k < Count; k++)
            {
                if (this[k].rc != Q[k].rc)
                {
                    return(this[k].rc - Q[k].rc);
                }
            }
            return(0);
        }
Ejemplo n.º 17
0
        public bool EqualsRC(object obj)
        {
            UGrCells Q = obj as UGrCells;

            if (Q == null)
            {
                return(false);
            }
            if (this.Count != Q.Count)
            {
                return(false);
            }
            for (int k = 0; k < Count; k++)
            {
                if (this[k].rc != Q[k].rc)
                {
                    return(false);
                }
            }
            return(true);
        }
        public IEnumerable <GroupedLink> IEGet_SuperLink(GroupedLink GLKpre)
        {
            int  SWCtrl = GLKpre.type;
            bool ALSpre = GLKpre is ALSLink;

            if (GLKpre.UGCellsB.Count == 1)
            {
                UCell            U    = GLKpre.UGCellsB[0];
                List <UCellLink> Plst = CeLKMan.CeLK81[U.rc];
                if (Plst != null)
                {
                    foreach (var LK in Plst)
                    {
                        if (ALSpre && LK.type != W)
                        {
                            continue;
                        }
                        GroupedLink GLK = new GroupedLink(LK);
                        if (Check_SuperLinkSequence(GLKpre, GLK))
                        {
                            yield return(GLK);
                        }
                    }
                }
            }

            if (GNPXApp000.GMthdOption["GroupedCells"] == "1")
            {
                foreach (var GP in GLKMan.GrpCeLKLst)
                {
                    if (ALSpre && GP.type != W)
                    {
                        continue;
                    }
                    if (!GLKpre.UGCellsB.EqualsRC(GP.UGCellsA))
                    {
                        continue;
                    }
                    if (GLKpre.no2 != GP.no)
                    {
                        continue;
                    }
                    if (Check_SuperLinkSequence(GLKpre, GP))
                    {
                        yield return(GP);
                    }
                }
            }

            if (GNPXApp000.GMthdOption["ALS"] == "1" && ALSMan.AlsInnerLink != null)
            {
                if (GLKpre.type == W)
                {
                    foreach (var GP in ALSMan.AlsInnerLink.Where(p => (p.ALSbase.Level == 1)))
                    {
                        if (GLKpre.no2 != GP.no)
                        {
                            continue;
                        }
                        if (GLKpre.UGCellsB.Equals(GP.UGCellsA))
                        {
                            yield return(GP);
                        }
                    }
                }
            }

            if (ALSpre)
            {
                ALSLink ALK   = GLKpre as ALSLink;
                int     noB   = 1 << ALK.no2;
                Bit81   BPnoB = new Bit81(pBDL, noB);

                Bit81 BP = BPnoB & ALK.UGCellsB.B81;
                //      ALK.UGCellsB.ForEach(P=>{ if((P.FreeB&noB)>0) BP.BPSet(P.rc); });

                Bit81 UsedCs = GLKpre.UsedCs;
                for (int tfx = 0; tfx < 27; tfx++)
                {
                    Bit81 HS = BPnoB & pHouseCells[tfx];
                    if (!(BP - HS).IsZero())
                    {
                        continue;
                    }
                    if ((HS - BP).IsZero())
                    {
                        continue;
                    }

                    Bit81 NxtBP = HS - BP - UsedCs;
                    if (NxtBP.IsZero())
                    {
                        continue;
                    }

//C                        WriteLine("\n tfx:"+tfx );
//C                        WriteLine( "   BP:"+BP );
//C                        WriteLine( "   HS:"+HS );
//C                        WriteLine( "HS-BP:"+(HS-BP) );
//C                        WriteLine( "NxtBP:"+NxtBP );

                    List <UCell> NxtCs = NxtBP.ToList().ConvertAll(rc => pBDL[rc]);
                    for (int k = 1; k < (1 << NxtCs.Count); k++)
                    {
                        UGrCells NxtGrpdCs = new UGrCells(tfx, ALK.no2);
                        int      kb        = k;
                        for (int n = 0; n < NxtCs.Count; n++)
                        {
                            if ((kb & 1) > 0)
                            {
                                NxtGrpdCs.Add(new UGrCells(NxtCs[n], ALK.no2));
                            }
                            kb >>= 1;
                        }
                        GroupedLink GP = new GroupedLink(GLKpre.UGCellsB, NxtGrpdCs, tfx, W);
//C                        WriteLine( GP );
                        yield return(GP);
                    }
                }
            }
            yield break;
        }
        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);
            }
        }
        private bool GNL_RecursiveSearch(GroupedLink GLK0, GroupedLink GLKpre, Stack <GroupedLink> SolStack, int szCtrl)
        {
            if (szCtrl < 0)
            {
                return(false);
            }

            Bit81 UsedCs = GLKpre.UsedCs;

            foreach (var GLKnxt in pSprLKsMan.IEGet_SuperLink(GLKpre))    //links that satisfy concatenation conditions
            {
                UGrCells GCsNxt = GLKnxt.UGCellsB;
                int      no     = GLKnxt.no;

                {                          //===== Chain Search =====
                    SolStack.Push(GLKnxt); //( link extension --> Push )
                    ___Debug_Print_GNLChain(SolStack);

                    //Loop was formed (the next cell matches the Origin Cell)
                    if (_GroupedNL_LoopCheck(GLK0, GLKnxt))
                    {
                        if (szCtrl == 0 && SolStack.Count > 3)
                        {
                            int SolType = _GroupedNL_CheckSolution(GLK0, GLKnxt, SolStack, UsedCs);
                            if (SolType > 0)
                            {
                                if (___GNLCC == 2898)
                                {
                                    WriteLine("-----------------");
                                }
                                ___Debug_Print_GNLChain(SolStack, "<+><+>");

                                if (SolInfoB)
                                {
                                    _GroupedNL_SolResult(GLK0, GLKnxt, SolStack, SolType);
                                }

                                __SolGL = SolCode;

                                if (__SimpleAnalizerB__)
                                {
                                    return(true);
                                }
                                if (!pAnMan.SnapSaveGP(false))
                                {
                                    return(false);
                                }
                                Thread.Sleep(10);
                                if ((++SolLimBrk) > (int)SDK_Ctrl.MltAnsOption["OneMethod"])
                                {
                                    SolLimBrk = int.MaxValue;
                                    return(false);
                                }
                            }
                        }
                    }
                    else if (!CheckUsed((UsedCs | GLK0.UsedCs), GLKnxt))
                    {
                        Bit81 UsedCsNxt = new Bit81(UsedCs);   //Create a new bit representation of used cell
                        SetUsed(ref UsedCsNxt, GLKnxt);

                        GLKnxt.UsedCs = UsedCsNxt;
                        GNL_RecursiveSearch(GLK0, GLKnxt, SolStack, szCtrl - 1); //Next step Search(recursive call
                        if (SolLimBrk == int.MaxValue)
                        {
                            return(false);
                        }
                        if (SolCode > 0)
                        {
                            return(true);
                        }
                    }
                    SolStack.Pop();     // Failure(Cancel link extension processing --> Pop )
                } //-----------------------------
            }
            return(false);
        }
Ejemplo n.º 21
0
 public GroupedLink(UGrCells UGCellsA, UGrCells UGCellsB, int type) : this()
 {
     this.UGCellsA = UGCellsA; this.UGCellsB = UGCellsB; this.type = type;
 }