Advertisement
Guest User

Untitled

a guest
Dec 8th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApplication2
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // Read the dimensions of the grid from standard input.
  11.             string[] dimensions = Console.ReadLine().Split();
  12.             int      width      = int.Parse(dimensions[0]);
  13.             int      height     = int.Parse(dimensions[1]);
  14.  
  15.             // Create a new grid with the given dimensions.
  16.             IGrid grid = new Grid(width, height);
  17.  
  18.             // Initialize the list of robots.
  19.             List<IRobot> robots = new List<IRobot>();
  20.  
  21.             // Read the initial position and instructions for each robot from standard input.
  22.             while (true)
  23.             {
  24.                 string[] position = Console.ReadLine().Split();
  25.                 if (string.IsNullOrEmpty(position[0]))
  26.                 {
  27.                     // Stop reading when we reach the end of the input.
  28.                     break;
  29.                 }
  30.  
  31.                 // Parse the initial position and orientation of the robot.
  32.                 int         x           = int.Parse(position[0]);
  33.                 int         y           = int.Parse(position[1]);
  34.                 Orientation orientation = ParseOrientation(position[2]);
  35.  
  36.                 // Read the instructions for the robot.
  37.                 string instructions = Console.ReadLine();
  38.  
  39.                 // Create a new Robot object and add it to the list of robots.
  40.                 robots.Add(new Robot(x, y, orientation, instructions, grid));
  41.             }
  42.  
  43.             // Create a new MartianRobots simulation and run it.
  44.             MartianRobots simulation = new MartianRobots(grid, robots);
  45.             simulation.Run();
  46.         }
  47.  
  48.         // ParseOrientation parses an orientation string and returns the corresponding Orientation value.
  49.         private static Orientation ParseOrientation(string str)
  50.         {
  51.             switch (str)
  52.             {
  53.                 case "N":
  54.                     return Orientation.North;
  55.                 case "S":
  56.                     return Orientation.South;
  57.                 case "E":
  58.                     return Orientation.East;
  59.                 case "W":
  60.                     return Orientation.West;
  61.                 default:
  62.                     throw new ArgumentException($"Invalid orientation: {str}");
  63.             }
  64.         }
  65.     }
  66.  
  67.     public enum Orientation
  68.     {
  69.         North,
  70.         South,
  71.         East,
  72.         West
  73.     }
  74.  
  75.     // MartianRobots is a class that represents a simulation of robots moving on a grid.
  76.     public class MartianRobots
  77.     {
  78.         // grid is the grid on which the robots are moving.
  79.         private readonly IGrid grid;
  80.  
  81.         // robots is a list of IRobot objects representing the robots in the simulation.
  82.         private readonly List<IRobot> robots;
  83.  
  84.         // Constructor creates a new MartianRobots simulation with the given grid and robots.
  85.         public MartianRobots(IGrid grid, List<IRobot> robots)
  86.         {
  87.             this.grid   = grid;
  88.             this.robots = robots;
  89.         }
  90.  
  91.         // Run runs the simulation and prints the final positions of the robots to standard output.
  92.         public void Run()
  93.         {
  94.             // Simulate the movements of the robots and print their final positions.
  95.             foreach (IRobot robot in robots)
  96.             {
  97.                 robot.Move();
  98.                 Console.WriteLine(robot);
  99.             }
  100.         }
  101.     }
  102.  
  103.     // IGrid is an interface representing a grid on which robots can move.
  104.     public interface IGrid
  105.     {
  106.         // Width and Height are the dimensions of the grid.
  107.         int Width { get; }
  108.         int Height { get; }
  109.  
  110.         // IsLost returns true if a robot has been lost at the given position on the grid.
  111.         bool IsLost(int x, int y);
  112.  
  113.         // MarkLost marks a robot as lost at the given position on the grid.
  114.         void MarkLost(int x, int y);
  115.     }
  116.  
  117.     // Grid is a class that represents a grid on which robots can move.
  118.     public class Grid : IGrid
  119.     {
  120.         // grid is a two-dimensional array of booleans representing the positions where robots
  121.         // have been lost.
  122.         private readonly bool[,] grid;
  123.  
  124.         // Width and Height are the dimensions of the grid.
  125.         public int Width { get; }
  126.         public int Height { get; }
  127.  
  128.         // Constructor creates a new grid with the given dimensions.
  129.         public Grid(int width, int height)
  130.         {
  131.             Width  = width;
  132.             Height = height;
  133.             grid   = new bool[width, height];
  134.         }
  135.  
  136.         // IsLost returns true if a robot has been lost at the given position on the grid.
  137.         public bool IsLost(int x, int y)
  138.         {
  139.             return grid[x, y];
  140.         }
  141.  
  142.         // MarkLost marks a robot as lost at the given position on the grid.
  143.         public void MarkLost(int x, int y)
  144.         {
  145.             grid[x, y] = true;
  146.         }
  147.     }
  148.  
  149.     // IRobot is an interface representing a robot that can move on a grid.
  150.     public interface IRobot
  151.     {
  152.         // PositionX and PositionY are the coordinates of the robot's current position on the grid.
  153.         int PositionX { get; }
  154.         int PositionY { get; }
  155.  
  156.         // Orientation is the robot's current orientation.
  157.         Orientation Orientation { get; }
  158.  
  159.         // IsLost returns true if the robot has been lost.
  160.         bool IsLost { get; }
  161.  
  162.         // Move instructs the robot to move according to the given instructions.
  163.         void Move();
  164.     }
  165.  
  166.     // Robot is a class that represents a robot that can move on a grid.
  167.     public class Robot : IRobot
  168.     {
  169.         // grid is the grid on which the robot is moving.
  170.         private readonly IGrid grid;
  171.  
  172.         // positionX and positionY are the coordinates of the robot's current position on the grid.
  173.         private int positionX;
  174.         private int positionY;
  175.  
  176.         // orientation is the robot's current orientation.
  177.         private Orientation orientation;
  178.         private readonly string instructions;
  179.  
  180.         // isLost is a flag indicating whether the robot has been lost.
  181.         private bool isLost;
  182.  
  183.         // PositionX and PositionY are the coordinates of the robot's current position on the grid.
  184.         public int PositionX => positionX;
  185.         public int PositionY => positionY;
  186.  
  187.         // Orientation is the robot's current orientation.
  188.         public Orientation Orientation => orientation;
  189.  
  190.         // IsLost returns true if the robot has been lost.
  191.         public bool IsLost => isLost;
  192.  
  193.         // Constructor creates a new robot with the given initial position and orientation on the given grid.
  194.         public Robot(int positionX, int positionY, Orientation orientation, string instructions, IGrid grid)
  195.         {
  196.             this.positionX    = positionX;
  197.             this.positionY    = positionY;
  198.             this.orientation  = orientation;
  199.             this.instructions = instructions;
  200.             this.grid         = grid;
  201.         }
  202.  
  203.         // Move instructs the robot to move according to the given instructions.
  204.         public void Move()
  205.         {
  206.             foreach (char c in instructions)
  207.             {
  208.                 int initialX = positionX;
  209.                 int initialY = positionY;
  210.                 switch (c)
  211.                 {
  212.                     case 'L':
  213.                         // Turn left.
  214.                         orientation = (Orientation)(((int)orientation + 3) % 4);
  215.                         break;
  216.                     case 'R':
  217.                         // Turn right.
  218.                         orientation = (Orientation)(((int)orientation + 1) % 4);
  219.                         break;
  220.                     case 'F':
  221.                         // Move forward.
  222.                         switch (orientation)
  223.                         {
  224.                             case Orientation.North:
  225.                                 positionY++;
  226.                                 break;
  227.                             case Orientation.South:
  228.                                 positionY--;
  229.                                 break;
  230.                             case Orientation.East:
  231.                                 positionX++;
  232.                                 break;
  233.                             case Orientation.West:
  234.                                 positionX--;
  235.                                 break;
  236.                         }
  237.  
  238.                         // Check if the robot has fallen off the grid.
  239.                         if (positionX < 0 || positionX >= grid.Width || positionY < 0 || positionY >= grid.Height)
  240.                         {
  241.                             // The robot has fallen off the grid.
  242.                             if (!grid.IsLost(initialX, initialY))
  243.                             {
  244.                                 // This is the first robot to be lost at this position.
  245.                                 // Leave a scent at the initial position of the robot.
  246.                                 grid.MarkLost(initialX, initialY);
  247.                             }
  248.  
  249.                             isLost = true;
  250.                         }
  251.  
  252.                         break;
  253.                 }
  254.  
  255.                 // If the robot has been lost, ignore the remaining instructions.
  256.                 if (isLost)
  257.                 {
  258.                     break;
  259.                 }
  260.             }
  261.         }
  262.  
  263.         public override string ToString()
  264.         {
  265.             // Format the output string.
  266.             string str = $"{positionX} {positionY} {orientation.ToString()[0]}";
  267.             if (isLost)
  268.             {
  269.                 str += " LOST";
  270.             }
  271.  
  272.             return str;
  273.         }
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement