iliya87

05.PaintBall

Mar 24th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class PaintBall
  5. {
  6.     static void Main()
  7.     {
  8.         int[] canvas = new int[10];
  9.  
  10.         for (int row = 0; row < 10; row++)
  11.         {
  12.             canvas[row] = 1023; // 10 1s in binary
  13.         }
  14.  
  15.         string currentShot = Console.ReadLine();
  16.         bool isWhite = false;
  17.  
  18.         while (currentShot != "End")
  19.         {
  20.             string[] shotDetails = currentShot.Split();
  21.  
  22.             int shotRowCenter = int.Parse(shotDetails[0]);
  23.             int shotColCenter = int.Parse(shotDetails[1]);
  24.             int shotRadius = int.Parse(shotDetails[2]);
  25.  
  26.             int startCol = Math.Max(shotColCenter - shotRadius, 0);
  27.             int endCol = Math.Min(shotColCenter + shotRadius, 9);
  28.             int startRow = Math.Max(shotRowCenter - shotRadius, 0);
  29.             int endRow = Math.Min(shotRowCenter + shotRadius, 9);
  30.  
  31.             int maskSize = endCol - startCol + 1;
  32.             int mask = (1 << maskSize) - 1;
  33.  
  34.             for (int i = startRow; i <= endRow; i++)
  35.             {
  36.                 if (isWhite)
  37.                 {
  38.                     canvas[i] |= mask << startCol;
  39.                 }
  40.                 else
  41.                 {
  42.                     canvas[i] &= ~(mask << startCol);
  43.                 }
  44.             }
  45.  
  46.             isWhite = !isWhite;
  47.             currentShot = Console.ReadLine();
  48.             //PrintMatrix(canvas);
  49.         }
  50.  
  51.         int sum = canvas.Sum();
  52.         Console.WriteLine(sum);
  53.     }
  54.  
  55.     private static void PrintMatrix(int[] canvas)
  56.     {
  57.       foreach (var num in canvas)
  58.         {
  59.             Console.WriteLine(Convert.ToString(num, 2).PadLeft(10, '0'));
  60.        }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment