public void Load()
        {
            int counter = 0;
            int c;

            setWalls();

            while (counter < sizeX * sizeY)
            {
                c = Reader.Read();
                if (!allowedChars.Contains((char)c))
                {
                    continue;
                }

                switch (c)
                {
                case 'S': sk = new MoveableObject(counter % sizeX + 1, counter / sizeX + 1);
                    break;

                case 'B': box = new MoveableObject(counter % sizeX + 1, counter / sizeX + 1);
                    break;
                }

                plan[counter % sizeX + 1, counter / sizeX + 1] = c;
                counter++;
            }
        }
        public int Solve()
        {
            int[, , ,] wasIHere = new int[sizeX + 1, sizeY + 1, sizeX + 1, sizeY + 1];
            Queue <MoveableObject> queueSk  = new Queue <MoveableObject>();
            Queue <MoveableObject> queueBox = new Queue <MoveableObject>();

            queueSk.Enqueue(sk);
            queueBox.Enqueue(box);

            while (queueSk.Count != 0)
            {
                MoveableObject skQueued  = queueSk.Dequeue();
                MoveableObject boxQueued = queueBox.Dequeue();
                int            previous  = wasIHere[skQueued.x, skQueued.y, boxQueued.x, boxQueued.y];

                for (int i = 0; i < vectors.Length / 2; i++)
                {
                    sk  = skQueued.Clone();
                    box = boxQueued.Clone();

                    sk.Move(vectors[i, 0], vectors[i, 1]);
                    if (sk.x == box.x && sk.y == box.y)
                    {
                        box.Move(vectors[i, 0], vectors[i, 1]);
                    }

                    if (plan[sk.x, sk.y] == 'X' || plan[box.x, box.y] == 'X')
                    {
                        continue;
                    }

                    if (plan[box.x, box.y] == 'C')
                    {
                        return(previous + 1);
                    }

                    if (wasIHere[sk.x, sk.y, box.x, box.y] != 0)
                    {
                        continue;
                    }

                    wasIHere[sk.x, sk.y, box.x, box.y] = 1 + previous;
                    queueSk.Enqueue(sk.Clone());
                    queueBox.Enqueue(box.Clone());
                }
            }
            return(-1);
        }
Пример #3
0
        public void Load()
        {
            int counter = 0;
            int c;
            setWalls();

            while (counter < sizeX * sizeY)
            {
                c = Reader.Read();
                if (!allowedChars.Contains((char)c))
                    continue;

                switch (c)
                {
                    case 'S': sk = new MoveableObject(counter % sizeX + 1, counter / sizeX + 1);
                        break;
                    case 'B': box = new MoveableObject(counter % sizeX + 1, counter / sizeX + 1);
                        break;
                }

                plan[counter % sizeX + 1, counter / sizeX + 1] = c;
                counter++;
            }
        }
Пример #4
0
        public int Solve()
        {
            int[, , ,] wasIHere = new int[sizeX + 1, sizeY + 1, sizeX + 1, sizeY + 1];
            Queue<MoveableObject> queueSk = new Queue<MoveableObject>();
            Queue<MoveableObject> queueBox = new Queue<MoveableObject>();
            queueSk.Enqueue(sk);
            queueBox.Enqueue(box);

            while (queueSk.Count != 0)
            {
                MoveableObject skQueued = queueSk.Dequeue();
                MoveableObject boxQueued = queueBox.Dequeue();
                int previous = wasIHere[skQueued.x, skQueued.y, boxQueued.x, boxQueued.y];

                for (int i = 0; i < vectors.Length / 2; i++)
                {
                    sk = skQueued.Clone();
                    box = boxQueued.Clone();

                    sk.Move(vectors[i, 0], vectors[i, 1]);
                    if (sk.x == box.x && sk.y == box.y)
                        box.Move(vectors[i, 0], vectors[i, 1]);

                    if (plan[sk.x, sk.y] == 'X' || plan[box.x, box.y] == 'X')
                        continue;

                    if (plan[box.x, box.y] == 'C')
                        return previous + 1;

                    if (wasIHere[sk.x, sk.y, box.x, box.y] != 0)
                        continue;

                    wasIHere[sk.x, sk.y, box.x, box.y] = 1 + previous;
                    queueSk.Enqueue(sk.Clone());
                    queueBox.Enqueue(box.Clone());
                }
            }
            return -1;
        }