Example #1
0
        //public static void Intersection(UnknownSet a, UnknownSet b, List<UnknownSet> sets)
        //{
        //    HashSet<Field> newFieldSet = new HashSet<Field>(a.fields);
        //    newFieldSet.IntersectWith(b.fields);

        //    if (newFieldSet.Count == 0) { return; }

        //    int aCount = a.fields.Count;
        //    int bCount = b.fields.Count;
        //    int newCount = newFieldSet.Count;

        //    int minA = a.Min - (aCount - newCount);
        //    int maxA = a.Max;

        //    int minB = b.Min - (bCount - newCount);
        //    int maxB = b.Max;

        //    int min = JRapp.Numeric.Max(0, minA, minB);
        //    int max = JRapp.Numeric.Min(newCount, maxA, maxB);

        //    UnknownSet finalSet;
        //    if (newCount == aCount) { a.Update(min, max); finalSet = a; }
        //    else if (newCount == bCount) { b.Update(min, max); finalSet = b; }
        //    else { finalSet = AddOrUpdateSet(new UnknownSet(newFieldSet, min, max), sets); }

        //    var newAComp = new HashSet<Field>(a.fields);
        //    newAComp.ExceptWith(finalSet.fields);
        //}

        //public static void Partial(UnknownSet a, UnknownSet b, List<UnknownSet> sets, bool rev = true)
        //{
        //    HashSet<Field> newFieldSet = new HashSet<Field>(a.fields);
        //    newFieldSet.IntersectWith(b.fields);

        //    if (newFieldSet.Count == 0) { return; }

        //    int aCount = a.fields.Count;
        //    int bCount = b.fields.Count;
        //    int newCount = newFieldSet.Count;

        //    int min = a.Min - b.Max;
        //    int max = a.Max - b.Min;

        //    if      (newCount == aCount) { a.Update(min, max); }
        //    else if (newCount == bCount) { b.Update(min, max); }
        //    else { AddOrUpdateSet(new UnknownSet(newFieldSet, min, max), sets); }

        //    if (rev) { Partial(b,a,sets,false); }
        //}

        public static void Compute(UnknownSet a, UnknownSet b, WorkingSets sets)
        {
            int C_a = a.fields.Count;
            int C_b = b.fields.Count;

            var set = Process(a, b, sets, operation: (a_, b_) => a_.IntersectWith(b_),
                              min: (nS) => JRapp.Numeric.Max(0, a.Min - (C_a - nS.Count), b.Min - (C_b - nS.Count)),
                              max: (nS) => JRapp.Numeric.Min(nS.Count, a.Max, b.Max)
                              );

            if (set == null)
            {
                return;
            }

            Process(a, set, sets, operation: (a_, nS) => a_.ExceptWith(nS),
                    min: (nS) => JRapp.Numeric.Max(a.Min - set.Max, 0),
                    max: (nS) => JRapp.Numeric.Min(a.Max - set.Min, nS.Count)
                    );

            Process(b, set, sets, operation: (b_, nS) => b_.ExceptWith(nS),
                    min: (nS) => JRapp.Numeric.Max(b.Min - set.Max, 0),
                    max: (nS) => JRapp.Numeric.Min(b.Max - set.Min, nS.Count)
                    );
        }
Example #2
0
        public static UnknownSet Process(UnknownSet a, UnknownSet b,
                                         WorkingSets sets,
                                         Action <HashSet <Field>, HashSet <Field> > operation,
                                         Func <HashSet <Field>, int> min, Func <HashSet <Field>, int> max)
        {
            HashSet <Field> newFieldSet = new HashSet <Field>(a.fields);

            operation(newFieldSet, b.fields);

            if (newFieldSet.Count == 0)
            {
                return(null);
            }

            int aCount   = a.fields.Count;
            int bCount   = b.fields.Count;
            int newCount = newFieldSet.Count;

            int myMin = min(newFieldSet);
            int myMax = max(newFieldSet);

            UnknownSet finalSet = AddOrUpdateSet(new UnknownSet(newFieldSet, myMin, myMax), sets);

            return(finalSet);
        }
Example #3
0
        public static UnknownSet AddOrUpdateSet(UnknownSet newSet, WorkingSets sets)
        {
            {
            }

            var existingSet = sets.Find(newSet);

            if (existingSet != null)
            {
                existingSet.Update(newSet.Min, newSet.Max);
                return(existingSet);
            }
            else
            {
                sets.Add(newSet);
                return(newSet);
            }
        }
Example #4
0
        public void SolveStep()
        {
            Step = Step + 1;

            var sets = new UnknownSet.WorkingSets();

            foreach (var s in knownFree)
            {
                var unknown = s.Neighbors.Unknown;

                if (unknown.Count() > 0)
                {
                    int remainingMines = s.Remaing;
                    UnknownSet.AddOrUpdateSet(
                        new UnknownSet(new HashSet <Field>(s.Neighbors.Unknown),
                                       s.Remaing, s.Remaing)
                        , sets);
                }
            }

            int prevCount = 0;

            while (sets.Count != prevCount)
            {
                int startPos = prevCount;
                prevCount = sets.Count;

                for (int i = 0; i < prevCount; i++)
                {
                    for (int j = i + 1; j < prevCount; j++)
                    {
                        UnknownSet.Compute(sets[i], sets[j], sets);
                    }
                }
            }

            foreach (var s in sets)
            {
                knownFree.AddRange(s.TryFindMine());
            }
        }