Saturday, October 5, 2013

Game of Life

The game of life is a game which is played on a field of cells. Each cell has eight neighbours, i.e. adjacent cells. Each cell in the field either is occupied by an organism or is blank. Thus, given that a cell is occupied by an organism, this organism could have eight neighbours or less. The objective of the game is to simulate the life of the organisms present in the field, generation after generation. The only work of the organism is to either reproduce or die! The rules of reproduction are given as follows:

  1. If an organism has 0 or 1 neighbours, it will die out of loneliness in the next generation.
  2. If an organism has 4 or more neighbours, it will die of crowding in the next generation.
  3. If an organism has 2 or 3 neighbours, it will prosper and survive into the next generation.
  4. If an unoccupied cell has exactly three neighbours, then an organism takes birth in the next generation.
You are required to simulate the life of the organisms and output the state of the field after 'N' generations.



Input Specification:

  • The first line of input will be two integers, R and C, separated by a space, specifying the number of rows and columns that the field has. Assume that the maximum number of rows and columns will never be greater than 30.
  • The next R lines of input contain C characters, consisting of '#' and '@' respectively. '#' means a blank cell, while '@' indicates an organism.
  • The last line of input contains one integer N, which is the number of generations that are supposed to be simulated.

Output Specification:

The output of your program should be a single integer indicating the number of living organisms at the end of simulation, i.e., after N generations.

Sample Input and Output:

Input:
33
#@#
#@@
@#@
3
Output:
3
Input:
33
#@#
#@@
@#@
4
Output:
4

Logic:

Available soon .....

 Solution:

  #include <stdio.h>
#include <stdlib.h>
#define MAX    99

void main()
{
    int mat[MAX+1][MAX+1],mat2[MAX+1][MAX+1];
    int sum,life;
    int i,j,k;
    int r,c,gen,gen_temp;
    char ch;
   
    scanf("%d %d",&r,&c);   
   
    //Reading list
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            //ch=getchar();
            scanf(" %c",&ch);
            if(ch=='@')
            mat[i][j]=1;
            else if(ch=='#')
            mat[i][j]=0;
        }
    }
   
    //reading generations
    scanf(" %d",&gen);
    //printf("\n\t%d\n",gen);
    for(j=0;j<=MAX;j++)
    {mat[MAX][j]=0;
    mat[j][MAX]=0;}
   
    for(gen_temp=0;gen_temp<gen;gen_temp++)
    {
        for(i=0;i<r;i++)    //Checking row
          {
            for(j=0;j<c;j++)    //Column checking
            {
                life=mat[i][((j-1)<0)?MAX:(j-1)]+mat[i][((j+1)>=c)?MAX:(j+1)]+mat[((i-1)<0)?MAX:(i-1)][((j-1)<0)?MAX:(j-1)]+mat[((i-1)<0)?MAX:(i-1)][j]+mat[((i-1)<0)?MAX:(i-1)][((j+1)>=c)?MAX:(j+1)]+mat[((i+1)>=r)?MAX:(i+1)][((j-1)<0)?MAX:(j-1)]+mat[((i+1)>=r)?MAX:(i+1)][j]+mat[((i+1)>=r)?MAX:(i+1)][((j+1)>=c)?MAX:(j+1)];

                //Checking life
                if(life<2 || life>3)    //die
                mat2[i][j]=0;

                else if(life==2 || life==3)    //Survive and birth
                {
                    if(life==3)    //Birth   
                    mat2[i][j]=1;
                    else
                    mat2[i][j]=mat[i][j];   
                }
            }    //End column checking
          }    //End row checking

          //Copying matrix 2 to matrix 1
          for(i=0;i<r;i++)
          {   
            for(j=0;j<c;j++)
            {mat[i][j]=mat2[i][j];}
          }
           
    }    //End checking generations
   
    //Adding the number of Animals
    sum=0;
    for(i=0;i<r;i++)
      {   
        for(j=0;j<c;j++)
        {sum=sum+mat[i][j];}
      }
    //Printing
    printf("%d\n",sum);
}

No comments:

Post a Comment