Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1.   public static string[] GetResults(int n, string[] grid)
  2.         {
  3.             if (n < 2) return grid;
  4.  
  5.             var plantedGrid = GetPlantedGrid(grid);
  6.  
  7.             var bombs = GetCurrentBombs(grid);
  8.  
  9.             var planted = false;
  10.  
  11.             for (var seconds = 2; seconds <= n; seconds++)
  12.             {
  13.  
  14.                 if (!planted)
  15.                 {
  16.                     planted = true;
  17.                     continue;
  18.                 }
  19.  
  20.                 if (planted)
  21.                 {
  22.                     BlowBombs(plantedGrid, bombs);
  23.                     bombs = GetCurrentBombs(grid);
  24.                     planted = false;
  25.                 }
  26.             }
  27.  
  28.             return grid;
  29.         }
  30.  
  31.         private static List<Bomb> GetCurrentBombs(string[] grid)
  32.         {
  33.             var result = new List<Bomb>();
  34.  
  35.             for (var lineIndex = 0; lineIndex < grid.Length; lineIndex++)
  36.             {
  37.                 if (!grid[lineIndex].Contains('O'))
  38.                     continue;
  39.  
  40.                 for(var ind = grid[lineIndex].IndexOf('O'); ind > -1; ind = grid[lineIndex].IndexOf('O', ind + 1))
  41.                 {
  42.                     result.Add(new Bomb { X = lineIndex, Y = ind });
  43.                 }
  44.             }
  45.  
  46.             return result;
  47.         }
  48.  
  49.         private static string[] GetPlantedGrid(string[] grid)
  50.         {
  51.             string[] result = new string[grid.Length];
  52.  
  53.             for(var i = 0; i < grid.Length; i++)
  54.             {
  55.                 result[i] = grid[i].Replace('.', 'O');
  56.             }
  57.  
  58.             return result;
  59.         }
  60.  
  61.         private static void BlowBombs(string[] grid, List<Bomb> bombs)
  62.         {
  63.             foreach(var bomb in bombs)
  64.             {
  65.                 grid[bomb.X] = grid[bomb.X].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
  66.  
  67.                 if (bomb.X != 0 && bomb.X != grid.Length - 1)
  68.                 {
  69.                     grid[bomb.X - 1] = grid[bomb.X - 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
  70.                     grid[bomb.X + 1] = grid[bomb.X + 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
  71.  
  72.                 }
  73.                 else if(bomb.X == 0 && grid.Length > 1)
  74.                 {
  75.                     grid[bomb.X + 1] = grid[bomb.X + 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
  76.                 }
  77.                 else if(bomb.X == grid.Length - 1 && grid.Length - 1 > 0)
  78.                 {
  79.                     grid[bomb.X - 1] = grid[bomb.X - 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
  80.                 }
  81.  
  82.                 if(bomb.Y != 0 && bomb.Y != grid[bomb.X].Length - 1)
  83.                 {
  84.                     grid[bomb.X] = grid[bomb.X].Remove(bomb.Y - 1, 1).Insert(bomb.Y - 1, ".").Remove(bomb.Y + 1, 1).Insert(bomb.Y + 1, ".");
  85.                 }
  86.                 else if(bomb.Y == 0 && grid[bomb.X].Length > 1)
  87.                 {
  88.                     grid[bomb.X] = grid[bomb.X].Remove(bomb.Y + 1, 1).Insert(bomb.Y + 1, ".");
  89.                 }
  90.                 else if(bomb.Y == grid[bomb.X].Length - 1 && grid[bomb.X].Length > 1)
  91.                 {
  92.                     grid[bomb.X] = grid[bomb.X].Remove(bomb.Y - 1, 1).Insert(bomb.Y - 1, ".");
  93.                 }
  94.             }
  95.  
  96.             bombs.Clear();
  97.         }
  98.  
  99.         private class Bomb
  100.         {
  101.             public int X { get; set; }
  102.  
  103.             public int Y { get; set; }
  104.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement