Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string[] GetResults(int n, string[] grid)
- {
- if (n < 2) return grid;
- var plantedGrid = GetPlantedGrid(grid);
- var bombs = GetCurrentBombs(grid);
- var planted = false;
- for (var seconds = 2; seconds <= n; seconds++)
- {
- if (!planted)
- {
- planted = true;
- continue;
- }
- if (planted)
- {
- BlowBombs(plantedGrid, bombs);
- bombs = GetCurrentBombs(grid);
- planted = false;
- }
- }
- return grid;
- }
- private static List<Bomb> GetCurrentBombs(string[] grid)
- {
- var result = new List<Bomb>();
- for (var lineIndex = 0; lineIndex < grid.Length; lineIndex++)
- {
- if (!grid[lineIndex].Contains('O'))
- continue;
- for(var ind = grid[lineIndex].IndexOf('O'); ind > -1; ind = grid[lineIndex].IndexOf('O', ind + 1))
- {
- result.Add(new Bomb { X = lineIndex, Y = ind });
- }
- }
- return result;
- }
- private static string[] GetPlantedGrid(string[] grid)
- {
- string[] result = new string[grid.Length];
- for(var i = 0; i < grid.Length; i++)
- {
- result[i] = grid[i].Replace('.', 'O');
- }
- return result;
- }
- private static void BlowBombs(string[] grid, List<Bomb> bombs)
- {
- foreach(var bomb in bombs)
- {
- grid[bomb.X] = grid[bomb.X].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
- if (bomb.X != 0 && bomb.X != grid.Length - 1)
- {
- grid[bomb.X - 1] = grid[bomb.X - 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
- grid[bomb.X + 1] = grid[bomb.X + 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
- }
- else if(bomb.X == 0 && grid.Length > 1)
- {
- grid[bomb.X + 1] = grid[bomb.X + 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
- }
- else if(bomb.X == grid.Length - 1 && grid.Length - 1 > 0)
- {
- grid[bomb.X - 1] = grid[bomb.X - 1].Remove(bomb.Y, 1).Insert(bomb.Y, ".");
- }
- if(bomb.Y != 0 && bomb.Y != grid[bomb.X].Length - 1)
- {
- grid[bomb.X] = grid[bomb.X].Remove(bomb.Y - 1, 1).Insert(bomb.Y - 1, ".").Remove(bomb.Y + 1, 1).Insert(bomb.Y + 1, ".");
- }
- else if(bomb.Y == 0 && grid[bomb.X].Length > 1)
- {
- grid[bomb.X] = grid[bomb.X].Remove(bomb.Y + 1, 1).Insert(bomb.Y + 1, ".");
- }
- else if(bomb.Y == grid[bomb.X].Length - 1 && grid[bomb.X].Length > 1)
- {
- grid[bomb.X] = grid[bomb.X].Remove(bomb.Y - 1, 1).Insert(bomb.Y - 1, ".");
- }
- }
- bombs.Clear();
- }
- private class Bomb
- {
- public int X { get; set; }
- public int Y { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement