archangelmihail

Neurons

Nov 30th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Neurons
  8. {
  9.     class Neurons
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int all = 0;
  14.             long[] grid = new long[32];
  15.            
  16.             int counter = 0;
  17.             // initiation of numbers
  18.             do
  19.             {
  20.                 grid[counter] = long.Parse(Console.ReadLine());
  21.                 if (grid[counter] == -1)
  22.                 {
  23.                     all = -1;
  24.                 }
  25.                 counter++;
  26.             } while (all == 0);
  27.  
  28.             // print input
  29.             int rows = 0;
  30.             for (int i = 0; i < grid.Length; i++)
  31.             {
  32.                 if (grid[i] != -1)
  33.                 {
  34.                     //Console.WriteLine("{0}", Convert.ToString(grid[i], 2).PadLeft(32, '0'));
  35.                 }
  36.                 else
  37.                 {
  38.                     rows = i;
  39.                     break;
  40.                 }
  41.             }
  42.             long[] neurons = new long[rows];
  43.  
  44.             //define variables;
  45.             bool top = false;
  46.             bool left = false;
  47.             bool right = false;
  48.             bool bottom = false;
  49.            
  50.            
  51.  
  52.             // initiate matrix
  53.             int[,] matrix = new int[rows, 32];
  54.             for (int row = 0; row < rows; row++)
  55.             {
  56.                 for (int j = 0; j < 32; j++)
  57.                 {
  58.                     if (grid [row] == -1)
  59.                     {
  60.                         break;
  61.                     }
  62.                     int mask = 1<<j;
  63.                     if ( (grid[row] & mask) != 0 )
  64.                     {
  65.                         matrix[row, 31-j] = 1;
  66.                     }
  67.                 }
  68.             }
  69.  
  70.             // chaking  top, left, right, bottom
  71.             for (int row = 0; row < rows; row++)
  72.             {
  73.                 for (int col = 0; col < 32; col++)
  74.                 {
  75.                     if (matrix [row,col] == 0)
  76.                     {
  77.                         for (int j = col; j < 31 ; j++)
  78.                         {
  79.                             if (matrix [row,j+1] == 1)
  80.                             {
  81.                                 right = true;
  82.                                
  83.                             }
  84.                         }
  85.                         for (int j = col; j >= 1; j--)
  86.                         {
  87.                             if (matrix[ row, j-1] ==1)
  88.                             {
  89.                                 left = true;
  90.                                
  91.                             }
  92.                         }
  93.                         for (int j = row; j >= 1; j--)
  94.                         {
  95.                             if (matrix [j-1, col] == 1)
  96.                             {
  97.                                 top = true;
  98.                                
  99.                             }
  100.                         }
  101.                         for (int j = row; j < rows-1; j++)
  102.                         {
  103.                             if (matrix [j+1,col] == 1)
  104.                             {
  105.                                 bottom = true;
  106.                                
  107.                             }
  108.                         }
  109.                         if (top == true && bottom == true && left == true && right == true)
  110.                         {
  111.                             neurons[row] = neurons[row] | (1 << (31-col));
  112.                         }
  113.                         top = false;
  114.                         right = false;
  115.                         left = false;
  116.                         bottom = false;
  117.                     }
  118.                 }
  119.             }
  120.  
  121.             //print output
  122.            
  123.             for (int i = 0; i < rows; i++)
  124.             {
  125.                 if (neurons[i] != -1)
  126.                 {
  127.                     Console.WriteLine(neurons[i]);
  128.                     //Console.WriteLine("{0}", Convert.ToString(neurons[i], 2).PadLeft(32, '0'));
  129.                 }
  130.                 else
  131.                 {
  132.                     break;
  133.                 }
  134.             }
  135.            
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment