public double Multiply(Vector vector)
 {
     double sum = 0;
     for (int i = 0; i < n; ++i)
         sum += values[i] * vector.values[i];
     return sum;
 }
 public Vector Add(Vector vector)
 {
     double[] temp = new double[n];
     for (int i = 0; i < n; ++i)
         temp[i] = values[i] + vector.Values[i];
     return new Vector(temp);
 }
 public Vector Multiply(double number)
 {
     Vector result = new Vector(this.Values);
     for (int i = 0; i < n; ++i)
             result.Values[i] *= number;
     return result;
 }
Exemple #4
0
 /*
  *
  */
 public override RobotMove NextMove(Map map)
 {
     lastRobotPos = map.Robot;
     moveRockTarget = null;
     if(currentTarget == null)
     {
         Tuple<Vector, Stack<RobotMove>> target = FindBestTarget(map);
         RobotMove newmove = RobotMove.Abort;
         if(target == null)
             newmove = FindMovableRock(map);
         if (target == null && newmove != RobotMove.Abort)
             return newmove;
         if(target == null && newmove == RobotMove.Abort)
         {
             if(map.TotalLambdaCount > map.LambdasGathered && map.HasActiveObjects)
                 return FindSafePlace(map);
             return RobotMove.Abort;
         }
         currentTarget = target.Item1;
         plan = target.Item2;
     }
     if (!plan.Any()) return RobotMove.Abort;
     RobotMove move = plan.PeekAndPop();
     if(!plan.Any() || MoveChangeMapSignificantly(map, move))
     {
         currentTarget = null;
         plan = null;
     }
     return move;
 }
Exemple #5
0
        public static bool IsSafeMove(this Map map, Vector from, Vector to, int movesDone, int waterproofLeft)
        {
            if (waterproofLeft <= 0 && map.WaterLevelAfterUpdate(map.MovesCount + movesDone - 1) >= to.Y)
                return false;

            var emptyCells = new[] {from, to};

            bool isSafe = true;

            if (to.Y == from.Y - 1)
            {
                for (int x = to.X - 1; x <= to.X + 1; x++)
                {
                    var newPosition = map.TryToMoveRock(new Vector(x, to.Y + 2), emptyCells);
                    if (newPosition.X == to.X && newPosition.Y == to.Y + 1)
                        isSafe = false;
                }
            }

            if (to.Y + movesDone + 1 < map.Height)//камни сверху
            {
                int y = to.Y + movesDone + 1;
                for (int x = to.X - 1; x <= to.X + 1; x++)
                {
                    var newPosition = map.TryToMoveRock(new Vector(x, y), emptyCells);

                    if (newPosition.X == to.X && newPosition.Y == y - 1 && map.IsColumnEmpty(to.X, to.Y + 1, y - 2))
                        isSafe = false;
                }
            }

            return isSafe;
        }
 public Vector Multiply(Vector vector)
 {
     Vector result = new Vector(n);
     for (int i = 0; i < n; ++i)
         for (int j = 0; j < n; ++j)
             result.Values[i] += values[i, j] * vector.Values[j];
     return result;
 }
        public void Test_GetNorm()
        {
            Vector vector = new Vector(3);
            double[] A = { 4, 0, 3 };
            vector.Values = A;

            double actual = vector.GetNorm();

            Assert.AreEqual(5, actual);
        }
Exemple #8
0
 private static string[] GetTargets(Vector from, Map map)
 {
     Console.WriteLine(map.ToString());
     var waveRun = new WaveRun(map, from);
     Tuple<Vector, Stack<RobotMove>>[] targets = waveRun.EnumerateTargets((lmap, pos, stepNumber) => lmap.GetCell(pos) == MapCell.Lambda).ToArray();
     string[] formattedTargets = targets.Select(FormatTarget).ToArray();
     foreach (var target in formattedTargets)
         Console.WriteLine(target);
     return formattedTargets;
 }
 public static void DrawTarget(this IDrawer drawer, Map map, Vector from, string style, Tuple<Vector, Stack<RobotMove>> target)
 {
     drawer.Dot(style, target.Item1);
     Vector pos = from;
     foreach (var move in target.Item2)
     {
         Vector pos2 = pos.Add(move.ToVector());
         drawer.Line(style, pos, pos2);
         pos = map.GetTrampolineTarget(pos2);
     }
 }
        public void Test_Add()
        {
            double[] a = { 1, 2, 3 };
            double[] b = { 2, 4, 6 };
            Vector A = new Vector(a);
            Vector B = new Vector(b);

            Vector c = A.Add(B);

            Assert.AreEqual(3, c.Values[0]);
            Assert.AreEqual(6, c.Values[1]);
            Assert.AreEqual(9, c.Values[2]);
        }
        public void Test_Multiply()
        {
            double[] A = { 1, 2, 3 };
            double[] B = { 2, 4, 5 };
            Vector vector = new Vector(A);
            Vector vector2 = new Vector(B);

            Vector actual2 = vector.Multiply(3);

            Assert.AreEqual(3, actual2.Values[0]);
            Assert.AreEqual(6, actual2.Values[1]);
            Assert.AreEqual(9, actual2.Values[2]);

            double actual = vector.Multiply(vector2);

            Assert.AreEqual(25, actual);
        }
        public void Test_Multiply()
        {
            Matrix A = new Matrix(3);
            double[,] B = new double[3, 3];
            for (int i = 0; i < 3; ++i)
                for (int j = 0; j < 3; ++j)
                    B[i, j] = 2;
            A.Values = B;
            double[] d = { 1, 0, 2 };
            Vector vector = new Vector(d);

            Matrix result = A.Multiply(4);
            Vector result2 = A.Multiply(vector);

            Assert.AreEqual(8, result.Values[0, 0]);
            Assert.AreEqual(8, result.Values[0, 2]);
            Assert.AreEqual(8, result.Values[2, 2]);
            //Assert.AreEqual(6, result2.Values[0]);
            //Assert.AreEqual(6, result2.Values[1]);
            //Assert.AreEqual(6, result2.Values[2]);
        }
Exemple #13
0
 public static Vector GetTrampolineTarget(this Map map, Vector trampolineOrJustCell)
 {
     if (!map.GetCell(trampolineOrJustCell).IsTrampoline()) return trampolineOrJustCell;
     return map.Targets[map.TrampToTarget[map.GetCell(trampolineOrJustCell)]];
 }
Exemple #14
0
 public MapCell GetCell(Vector pos)
 {
     return QTree.Get(field, pos.X, pos.Y, 0, Width - 1, 0, Height - 1);
 }
 public override RobotMove NextMove(Map map, Vector target, SpecialTargetType type)
 {
     return NextMove(map);
 }
Exemple #16
0
        public static Vector TryToMoveRock(this Map map, Vector p, params Vector[] emptyCells)
        {
            Func<int, int, bool> isEmpty =
                (xx, yy) => emptyCells.Contains(new Vector(xx, yy)) || IsEmptyOrRobot(map.GetCell(xx, yy));
            var x = p.X;
            var y = p.Y;
            if (map.GetCell(x, y).IsRock())
            {

                if (isEmpty(x, y - 1))
                {
                    return new Vector(x, y - 1);
                }
                MapCell upCell = map.GetCell(x, y - 1);
                if (upCell.IsRock()
                    && isEmpty(x + 1, y) && isEmpty(x + 1, y - 1))
                {
                    return new Vector(x + 1, y - 1);
                }
                if (upCell.IsRock()
                    && (!isEmpty(x + 1, y) || !isEmpty(x + 1, y - 1))
                    && isEmpty(x - 1, y) && isEmpty(x - 1, y - 1))
                {
                    return new Vector(x - 1, y - 1);
                }
                if (upCell == MapCell.Lambda
                    && isEmpty(x + 1, y) && isEmpty(x + 1, y - 1))
                {
                    return new Vector(x + 1, y - 1);
                }
            }

            return p;
        }
Exemple #17
0
 public abstract RobotMove NextMove(Map map, Vector target, SpecialTargetType type);
Exemple #18
0
        private RobotMove FindMovableRock(Map map)
        {
            var left = new Vector(-1, 0);
            var right = new Vector(1, 0);
            var up = new Vector(0, 1);

            var leftRobot = map.Robot.Add(left);
            var rightRobot = map.Robot.Add(right);
            var upRobot = map.Robot.Add(up);

            var leftCheck = map.GetCell(leftRobot) != MapCell.Wall && map.IsSafeMove(map.Robot, map.Robot.Add(left), 1, map.WaterproofLeft);
            var rightCheck = map.GetCell(rightRobot) != MapCell.Wall && map.IsSafeMove(map.Robot, map.Robot.Add(right), 1, map.WaterproofLeft);

            if (map.GetCell(leftRobot).IsRock() && map.GetCell(leftRobot.Add(left)) == MapCell.Empty && leftCheck)
                return RobotMove.Left;
            if (map.GetCell(rightRobot).IsRock() && map.GetCell(rightRobot.Add(right)) == MapCell.Empty && rightCheck)
                return RobotMove.Right;

            if (map.GetCell(upRobot).IsRock() && map.GetCell(leftRobot).IsMovable() && leftCheck)
                return RobotMove.Left;
            if (map.GetCell(upRobot).IsRock() && map.GetCell(rightRobot).IsMovable() && rightCheck)
                return RobotMove.Right;

            var waveRun = new WaveRun(map, map.Robot);
            moveRockTarget = waveRun.EnumerateTargets(
                (lmap, position, used) =>
                    {
                        if (lmap.GetCell(position.Add(up)).IsRock() && (lmap.GetCell(position.Add(left)).IsMovable() || lmap.GetCell(position.Add(right)).IsMovable()))
                            return true;
                        if (lmap.GetCell(position).IsMovable() && lmap.GetCell(position.Add(left)).IsRock() && lmap.GetCell(position.Add(left).Add(left)).IsRockMovable())
                            return true;
                        if (lmap.GetCell(position).IsMovable() && lmap.GetCell(position.Add(right)).IsRock() && lmap.GetCell(position.Add(right).Add(right)).IsRockMovable())
                            return true;
                        if (lmap.GetCell(position) == MapCell.Earth && lmap.GetCell(position.Add(right)).IsRock()
                                && lmap.GetCell(position.Add(right).Add(right)) != MapCell.Wall && !lmap.GetCell(position.Add(right).Add(right)).IsRock())
                            return true;
                        if (lmap.GetCell(position) == MapCell.Earth && lmap.GetCell(position.Add(left)).IsRock()
                                && lmap.GetCell(position.Add(right).Add(right)) != MapCell.Wall && !lmap.GetCell(position.Add(right).Add(right)).IsRock())
                            return true;
                        return false;
                    }).FirstOrDefault();

            if(moveRockTarget == null)
                return RobotMove.Abort;

            return moveRockTarget.Item2.Any() ? moveRockTarget.Item2.Peek() : RobotMove.Abort;
        }
Exemple #19
0
 private QTree SetCell(Vector pos, MapCell newValue)
 {
     return SetCell(pos.X, pos.Y, newValue);
 }
Exemple #20
0
        public Map(string[] lines)
        {
            State = CheckResult.Nothing;

            var firstBlankLineIndex = Array.IndexOf(lines, "");
            Height = firstBlankLineIndex == -1 ? lines.Length : firstBlankLineIndex;
            Width = lines.Take(Height).Max(a => a.Length);

            field = new QTree();
            for (var row = 0; row < Height + 2; row++)
                for (var col = 0; col < Width + 2; col++)
                {
                    if (row == 0 || row == Height + 1 || col == 0 || col == Width + 1)
                        QTree.SimpleAdd(field, col, row, 0, Width + 1, 0, Height + 1, MapCell.Wall);
                    else
                    {
                        var x = col - 1;
                        var y = Height - row;
                        var line = lines[y].PadRight(Width, ' ');
                        var mapCell = Parse(line[x]);
                        QTree.SimpleAdd(field, col, row, 0, Width + 1, 0, Height + 1, mapCell);
                        if (mapCell == MapCell.Lambda || mapCell == MapCell.LambdaRock)
                            TotalLambdaCount++;
                        else if (mapCell == MapCell.Robot)
                        {
                            RobotX = col;
                            RobotY = row;
                        }
                        else if (mapCell == MapCell.ClosedLift || mapCell == MapCell.OpenedLift)
                            Lift = new Vector(col, row);
                        else if (mapCell == MapCell.Beard)
                            Beard.Add(new Vector(col, row));
                        else if (TargetsChars.Contains((char) mapCell))
                            Targets[mapCell] = new Vector(col, row);
                        else if (TrampolinesChars.Contains((char) mapCell))
                            Trampolines[mapCell] = new Vector(col, row);
                    }
                }

            InitializeVariables(lines.Skip(Height + 1).ToArray());

            Height += 2;
            Width += 2;

            InitializeActiveRocks();
        }
Exemple #21
0
 private void InitializeActiveRocks()
 {
     for (var y = 1; y < Height - 1; y++)
         for (var x = 1; x < Width - 1; x++)
         {
             var rockPos = new Vector(x, y);
             if (TryToMoveRock(rockPos, GetCell(rockPos), this) != null)
                 activeRocks.Add(rockPos);
         }
 }
Exemple #22
0
 private static Vector TryToMoveRock(Vector p, MapCell xyCell, Map mapToUse)
 {
     int x = p.X;
     int y = p.Y;
     if (xyCell.IsRock())
     {
         var upCell = mapToUse.GetCell(x, y - 1);
         if (upCell == MapCell.Empty)
         {
             return new Vector(0, -1);
         }
         if (upCell.IsRock()
             && mapToUse.GetCell(x + 1, y) == MapCell.Empty && mapToUse.GetCell(x + 1, y - 1) == MapCell.Empty)
         {
             return new Vector(1, -1);
         }
         if (upCell.IsRock()
             && (mapToUse.GetCell(x + 1, y) != MapCell.Empty || mapToUse.GetCell(x + 1, y - 1) != MapCell.Empty)
             && mapToUse.GetCell(x - 1, y) == MapCell.Empty && mapToUse.GetCell(x - 1, y - 1) == MapCell.Empty)
         {
             return new Vector(-1, -1);
         }
         if (upCell == MapCell.Lambda
             && mapToUse.GetCell(x + 1, y) == MapCell.Empty && mapToUse.GetCell(x + 1, y - 1) == MapCell.Empty)
         {
             return new Vector(1, -1);
         }
     }
     return null;
 }
Exemple #23
0
 private static void CheckNearRocks(SortedSet<Vector> updateableRocks, int x, int y, Map mapToUse)
 {
     for (var rockX = x - 1; rockX <= x + 1; rockX++)
         for (var rockY = y; rockY <= y + 1; rockY++)
         {
             var rockPos = new Vector(rockX, rockY);
             if (TryToMoveRock(rockPos, mapToUse.GetCell(rockPos), mapToUse) != null)
                 updateableRocks.Add(rockPos);
         }
 }
Exemple #24
0
 public WaveRun(Map map, Vector startPosition, int maxStepsCount = 1000)
 {
     this.map = map;
     this.startPosition = startPosition;
     this.maxStepsCount = maxStepsCount;
 }
Exemple #25
0
 public static bool IsValidMoveWithoutMovingRocks(this Map map, Vector from, Vector to)
 {
     var toCell = map.GetCell(to);
     return toCell == MapCell.Beard || toCell.IsTrampoline() || toCell == MapCell.OpenedLift || toCell == MapCell.Lambda || toCell == MapCell.Empty || toCell == MapCell.Earth || toCell == MapCell.Robot || toCell == MapCell.Razor;
 }
Exemple #26
0
 public static bool RocksFallAfterMoveTo(this Map map, Vector to)
 {
     for (int rockX = to.X - 1; rockX <= to.X + 1; rockX++)
     {
         for (int rockY = to.Y; rockY <= to.Y + 1; rockY++)
         {
             var coords = new Vector(rockX, rockY);
             if (!coords.Equals(map.TryToMoveRock(coords)))
                 return true;
         }
     }
     return false;
 }
Exemple #27
0
 public WaveCell(Vector pos, int stepNumber, WaveCell prevCell, RobotMove move, int waterproofLeft, int razorsLeft)
 {
     Pos = pos;
     StepNumber = stepNumber;
     PrevCell = prevCell;
     Move = move;
     WaterproofLeft = waterproofLeft;
     RazorsLeft = razorsLeft;
 }