Guest User

05. Game of Life

a guest
Jan 12th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class TestProject
  5. {
  6.     private static void Main()
  7.     {
  8.         int pairs = int.Parse(Console.ReadLine());
  9.         int[] oldboard = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
  10.         int x; int y;
  11.         List<int> newboard = new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  12.  
  13.         for (int i = 0; i < pairs; i++)// minavam prez vcseki edin bit
  14.         {
  15.             //foreach (var row in newboard)
  16.             //{
  17.             //    Console.WriteLine(Convert.ToString(row, 2).PadLeft(10, '0'));
  18.             //}
  19.             //Console.WriteLine();
  20.             x = int.Parse(Console.ReadLine());
  21.             y = int.Parse(Console.ReadLine());
  22.             oldboard[x] |= (1 << y);
  23.             newboard[x] |= (1 << y);
  24.         }
  25.  
  26.         for (int red = 0; red < 10; red++) // minavam prez vcseki edin bit
  27.         {
  28.             for (int kol = 9; kol >= 0; kol--)
  29.             {
  30.                 int currentbit = (newboard[red] & (1 << kol)) == 0 ? 0 : 1;
  31.                 int checkedbit;
  32.                 int countones = 0;
  33.  
  34.                 int startred = Math.Max(red - 1, 0); // opredelqv koi susedni bitove da proverqvam
  35.                 int endred = Math.Min(red + 1, 9);
  36.                 int startkol = Math.Max(kol - 1, 0);
  37.                 int endkol = Math.Min(kol + 1, 9);
  38.  
  39.  
  40.                 for (int k = startred; k <= endred; k++)
  41.                 {
  42.                     for (int l = endkol; l >= startkol; l--)
  43.                     {
  44.                         if (k == red && l == kol)
  45.                         {
  46.                             continue;
  47.                         }
  48.                         checkedbit = (newboard[k] & (1 << l)) == 0 ? 0 : 1;
  49.                         if (checkedbit == 1)
  50.                         {
  51.                             countones++;
  52.                         }
  53.                     }
  54.                 }
  55.  
  56.                 if (currentbit == 1)
  57.                 {
  58.                     if (countones < 2 || 3 < countones)
  59.                     {
  60.                         oldboard[red] &= ~(1 << kol);
  61.                     }
  62.                 }
  63.                 else
  64.                 {
  65.                     if (countones == 3)
  66.                     {
  67.                         oldboard[red] |= (1 << kol);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.  
  73.         foreach (var row in oldboard)
  74.         {
  75.             Console.WriteLine(Convert.ToString(row, 2).PadLeft(10, '0'));
  76.         }
  77.  
  78.         //Console.WriteLine(newboard[2]); //pravq proverka kolko tretoto 4islo -zero testa dava razlika samo v nego
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment