Advertisement
bullit3189

Ladybugs-2nd option-Arrays

Feb 6th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5.  
  6.  
  7. public class Program
  8. {
  9. public static void Main()
  10. {
  11. int fieldSize = int.Parse(Console.ReadLine());
  12. int[] field = new int[fieldSize];
  13. int[]initialIndexes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.  
  15. for (int i=0; i<initialIndexes.Length; i++)
  16. {
  17. int currIndex = initialIndexes[i];
  18.  
  19. if (currIndex>=0 && currIndex<=field.Length-1)
  20. {
  21. field[currIndex]= 1;
  22. }
  23. }
  24.  
  25. while (true)
  26. {
  27. string command = Console.ReadLine();
  28.  
  29. if (command == "end")
  30. {
  31. break;
  32. }
  33.  
  34. string[]tokens = command.Split();
  35.  
  36. int ladybugIndex = int.Parse(tokens[0]);
  37. bool isFirst = true;
  38.  
  39. while((ladybugIndex>=0 && ladybugIndex<=field.Length-1) && field[ladybugIndex]!=0)
  40. {
  41. if (isFirst)
  42. {
  43. field[ladybugIndex]=0;
  44. isFirst = false;
  45. }
  46.  
  47. string direction = tokens[1];
  48. int flyLen = int.Parse(tokens[2]);
  49.  
  50. if (direction == "right")
  51. {
  52. ladybugIndex += flyLen;
  53. }
  54. else
  55. {
  56. ladybugIndex -= flyLen;
  57. }
  58. if (ladybugIndex>=0 && ladybugIndex<=field.Length-1)
  59. {
  60. if (field[ladybugIndex]==0)
  61. {
  62. field[ladybugIndex]=1;
  63. break;
  64. }
  65. }
  66.  
  67. }
  68. }
  69. Console.WriteLine(string.Join(" ",field));
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement