The House Of Santa Claus

In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you remember that the importance was on drawing the house in a stretch without lifting the pencil and not drawing a line twice? As a reminder it has to look like shown in Figure 1.

 
Figure1: The House of Santa Claus

Well, a couple of years later, like now, you have to “draw” the house again but on the computer. As one possibility is not enough, we require all the possibilities when starting in the lower left corner. Follow the example in Figure 2 while defining your stretch.

 
Figure2: This Sequence would give the Outputline 153125432

All the possibilities have to be listed in the outputfile by increasing order.

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Collections;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            Hashtable ht = new Hashtable();

            string str = "112233455";

            int[,] connectionMatrix = {

                {0, 1, 1, 0, 1},

                {1, 0, 1, 0, 1},

                {1, 1, 0, 1, 1},

                {0, 0, 1, 0, 1},

                {1, 1, 1, 1, 0}

            };

            int[] pointer = new int[str.Length];

            int i = 0;

            for (i = 0; i < pointer.Length; i++)

            {

                pointer[i] = i;

            }

            pointer[–i]–;

            do

            {

                i = pointer.Length – 1;

            label1:

                pointer[i]++;

                if (pointer[i] == pointer.Length && i != 0)

                {

                    pointer[i] = 0;

                    i–;

                    goto label1;

                }

                else if (pointer[i] == pointer.Length && i == 0)

                {

                    break;

                }

                else

                {

                    //Check pointer validity

                    int[] counter = new int[pointer.Length];

                    for (int j = 0; j < counter.Length; j++)

                    {

                        counter[j] = 0;

                    }

                    int flag = 0;

                    for (int j = 0; j < counter.Length; j++)

                    {

                        counter[pointer[j]]++;

                        if (counter[pointer[j]] > 1)

                        {

                            flag = 1;

                            break;

                        }

                    }

                    if (flag == 0)

                    {

                        int[,] tempMatrix = new int[connectionMatrix.GetLength(0), connectionMatrix.GetLength(1)];

                        for (int j = 0; j < connectionMatrix.GetLength(0); j++)

                        {

                            for (int k = 0; k < connectionMatrix.GetLength(1); k++)

                            {

                                tempMatrix[j, k] = connectionMatrix[j, k];

                            }

                        }

                        for (int j = 0; j < str.Length; j++)

                        {

                            if (j < str.Length – 1)

                            {

                                if (str[pointer[j]] == str[pointer[j + 1]])

                                {

                                    flag = 1;

                                    break;

                                }

                                if (tempMatrix[int.Parse(str[pointer[j]].ToString()) – 1, int.Parse(str[pointer[j + 1]].ToString()) – 1] != 1)

                                {

                                    flag = 1;

                                    break;

                                }

                                if (tempMatrix[int.Parse(str[pointer[j]].ToString()) – 1, int.Parse(str[pointer[j + 1]].ToString()) – 1] == 1)

                                {

                                    tempMatrix[int.Parse(str[pointer[j]].ToString()) – 1, int.Parse(str[pointer[j + 1]].ToString()) – 1] = 0;

                                    tempMatrix[int.Parse(str[pointer[j + 1]].ToString()) – 1, int.Parse(str[pointer[j]].ToString()) – 1] = 0;

                                }

                            }

                        }

                    }

                    if (flag == 0)

                    {

                        string temp = "";

                        for (int j = 0; j < pointer.Length; j++)

                        {

                            temp = temp + str[pointer[j]];

                        }

                        if (!ht.Contains(temp))

                        {

                            ht.Add(temp, 1);

                        }

                    }

                }

            } while (pointer[0] != pointer.Length);

            Array sequence = Array.CreateInstance(typeof(string), ht.Count);

            ht.Keys.CopyTo(sequence, 0);

            Array.Sort(sequence);

            StreamWriter sw = new StreamWriter(@"D:\Output.txt", true);

            foreach (string tempStr in sequence)

            {

                sw.WriteLine(tempStr);

                Console.WriteLine(tempStr);

            }

            sw.Close();

        }

    }

}

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