Ramu Kaka’s Puzzle

Ramu Kaka is back home from his visits now. While cleaning the store room of his master, he came across a puzzle. The puzzle consists of a 5×5 frame containing 24 small identical squares. Each square has a unique number printed on top of it. The frame also has an empty position which is same as the size of a square. A square can be moved into the empty position if it were to the left, right, above or bottom of the

empty position. The aim of the puzzle is to arrange the numbers in sequence. You are required to code a program which returns the arrangement of the puzzle after a series of steps.

 

INPUT:

The first five lines of the input will consist of five digits separated by spaces. They represent the configuration of the puzzle i.e. first line corresponds to the configuration of the first row of puzzle. A value of ‘0’ indicates the empty position. The next lines define the sequence of moves. Moves are defines by characters ‘A’, ‘B’, ‘L’, ‘R’. A denotes that the square above the empty position moves; B denotes that the square below the empty position moves; L denotes that the square to the left of the empty position moves; R denotes that the square to the right of the empty position moves. There may be an illegal move. In-such a case the puzzle is assumed to have no final configuration. The input is terminated by the letter ‘Z’.

 

OUTPUT:

The output should print the configuration in the same way as the input. In-case of an illegal move, the output should be “NO FINAL CONFIGURATION POSSIBLE”.

 

SAMPLE INPUT:

5 10 20 22 23

2 56 19 49 38

34 8 31 54 44

12 0 29 2 27

47 1 9 50 33

ALARBRB

 

SAMPLE OUTPUT:

5 10 20 22 23

56 34 19 49 38

2 31 29 54 44

12 8 0 3 27

47 1 9 50 33
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(@"D:\input.txt");
            int i = 0;
            string moves = "";
            int[,] puzzle = new int[5, 5];
            int x = 0, y = 0;
            for (string str = sr.ReadLine(); str != null; str = sr.ReadLine())
            {
                if (i < 5)
                {
                    Regex reg = new Regex(" ");
                    string[] num = reg.Split(str);
                    for (int j = 0; j < 5; j++)
                    {
                        puzzle[i, j] = int.Parse(num[j]);
                        if (puzzle[i, j] == 0)
                        {
                            x = i;
                            y = j;
                        }
                    }
                }
                else
                {
                    moves = str;
                }
                i++;
            }
            sr.Close();
            Console.WriteLine("Puzzle arrangement before moves");
            for (i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write(puzzle[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine(moves);
            Console.WriteLine("Puzzle arrangement after moves");
            int flag = 0;
            for (i = 0; i < moves.Length && flag == 0; i++)
            {
                switch (moves[i])
                {
                    case ‘A’:
                        if (A(puzzle, x, y))
                        {
                            x = x – 1;
                        }
                        else
                        {
                            flag = 1;
                        }
                        break;
                    case ‘B’:
                        if (B(puzzle, x, y))
                        {
                            x = x + 1;
                        }
                        else
                        {
                            flag = 1;
                        }
                        break;
                    case ‘L’:
                        if (L(puzzle, x, y))
                        {
                            y = y – 1;
                        }
                        else
                        {
                            flag = 1;
                        }
                        break;
                    case ‘R’:
                        if (R(puzzle, x, y))
                        {
                            y = y + 1;
                        }
                        else
                        {
                            flag = 1;
                        }
                        break;
                    default: flag = 1;
                        break;
                }
            }
            if (flag == 0)
            {
                //Valid moves
                Console.WriteLine("FINAL CONFIGURATION");
                for (i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        Console.Write(puzzle[i, j] + " ");
                    }
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("NO FINAL CONFIGURATION POSSIBLE");
            }
        }
        public static bool A(int[,] puzzle, int x, int y)
        {
            if ((x – 1) < 0)
            {
                return false;
            }
            else
            {
                puzzle[x, y] = puzzle[x – 1, y];
                puzzle[x – 1, y] = 0;
                return true;
            }
        }
        public static bool B(int[,] puzzle, int x, int y)
        {
            if ((x + 1) >= puzzle.GetLength(0))
            {
                return false;
            }
            else
            {
                puzzle[x, y] = puzzle[x + 1, y];
                puzzle[x + 1, y] = 0;
                return true;
            }
        }
        public static bool L(int[,] puzzle, int x, int y)
        {
            if ((y – 1) < 0)
            {
                return false;
            }
            else
            {
                puzzle[x, y] = puzzle[x, y – 1];
                puzzle[x, y – 1] = 0;
                return true;
            }
        }
        public static bool R(int[,] puzzle, int x, int y)
        {
            if ((y + 1) >= puzzle.GetLength(1))
            {
                return false;
            }
            else
            {
                puzzle[x, y] = puzzle[x, y + 1];
                puzzle[x, y + 1] = 0;
                return true;
            }
        }
    }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s