A Colony of Eight Houses Represented as Cells in C

Write an algorithm to output the state of cells after the given number of days, without any extra memory (data structure).

Trouble Statement

A colony of viii houses, represented as cells, are bundled in a straight line. Each twenty-four hour period evry cell competes with its side by side cells (neighbours). An integer value of 1 represents an active cell and value of 0 represents equally inactive cell. If both the neighbours are either active or inactive, the cell becomes inactive the next mean solar day, otherwise it becomes active on the next day.

The two cells on the ends have a single adjacent cell, so the other side by side jail cell tin be causeless to exist always inactive. Even after updating the jail cell land, its previous country is considered for updating the country of other cells. The cell information of all cells should exist updated simultatiously.

Write an algorithm to output the land of the cells later on the given number of days.

Input

The input to the function consists of two arguments:

  • states, a listing of integers representing the current land of cells.
  • days, an integer representing the number of days.

Output

Return a list of integers representing the state of the cells afterwards the given number of days.

Annotation:

The elements of the list states contains 0s and 1st simply.

Approach

We are not using whatever extra retention in the given solution (except few local variables), and so the space complexity is O(n) where n is the number of cells. Hither it is 8.

Fourth dimension complexity is O(northward 10 m) where northward is the number of cells and 1000 is the number of days.

Program

          using Arrangement;  namespace CellCompetition {     form CellCompetitionProblem     {         static void Master(string[] args)         {             int[] arr = { ane, 0, 0, 0, 0, ane, 0, 0 };              Console.WriteLine("Instance one");             Console.Write("Input  : ");             Print(arr);             arr = cellCompete(arr, ane);             Console.Write("Output : ");             Print(arr);              Console.WriteLine("\nExample 2");             int[] arr2 = { ane, 1, 1, 0, 1, ane, 1, 1 };             Panel.Write("Input  : ");             Print(arr2);             arr2 = cellCompete(arr2, 2);             Console.Write("Output : ");             Print(arr2);              Console.ReadKey();         }          /// <summary>         /// Method to print the given array         /// </summary>         /// <param name="arr">given assortment</param>         static void Print(int[] arr)         {             for (int i = 0; i < arr.Length; i++)             {                 Console.Write(arr[i] + "\t");             }              Console.WriteLine();         }          /// <summary>         /// Method to get the state pf the cells after given number of days.         /// </summary>         /// <param name="states">current country of the cells</param>         /// <param proper noun="days">number of days</param>         /// <returns>state of cells after given number of days</returns>         static int[] cellCompete(int[] states, int days)         {             // if the number of cells is not equals to 8              // or number of days is less than i             if (states.Length != 8 || days < 1)             {                 render states;             }              // local variables             int index, previousValue, nextValue;              // loop for each day             for (int i = 0; i < days; i++)             {                 // index of current cell                 alphabetize = 0;                 // value of the previous cell                 previousValue = 0;                 // value of the next prison cell                 nextValue = 0;                  // loop for each cell in the assortment                  while (index < states.Length)                 {                     // if the current index is not last index of the given assortment,                     // set the value of nextValue, else it would remain 0.                     if (index < states.Length - 1)                     {                         nextValue = states[index + 1];                     }                     else if (index == states.Length - ane)                     {                         nextValue = 0;                     }                      // if nextValue is aforementioned every bit previousValue                     if (nextValue == previousValue)                     {                         // save the current alphabetize value for adjacent iteration of loop                         previousValue = states[index];                         // set current index value                         states[alphabetize] = 0;                     }                     else                     {                         // salvage the current index value for next iteration of loop                         previousValue = states[alphabetize];                         // fix electric current index value                         states[index] = i;                     }                      // next detail in the loop                     index++;                 }             }              // render states array             return states;         }     } }        

Output

          Instance 1 Input  : 1      0       0       0       0       1       0       0 Output : 0      i       0       0       one       0       i       0  Example 2 Input  : 1      one       1       0       ane       one       1       one Output : 0      0       0       0       0       ane       1       0                  

mansfieldhiceivien77.blogspot.com

Source: https://www.knowsh.com/Notes/250480/Active-And-Inactive-Cells-After-K-Days

0 Response to "A Colony of Eight Houses Represented as Cells in C"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel