Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication2
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- // Read the dimensions of the grid from standard input.
- string[] dimensions = Console.ReadLine().Split();
- int width = int.Parse(dimensions[0]);
- int height = int.Parse(dimensions[1]);
- // Create a new grid with the given dimensions.
- IGrid grid = new Grid(width, height);
- // Initialize the list of robots.
- List<IRobot> robots = new List<IRobot>();
- // Read the initial position and instructions for each robot from standard input.
- while (true)
- {
- string[] position = Console.ReadLine().Split();
- if (string.IsNullOrEmpty(position[0]))
- {
- // Stop reading when we reach the end of the input.
- break;
- }
- // Parse the initial position and orientation of the robot.
- int x = int.Parse(position[0]);
- int y = int.Parse(position[1]);
- Orientation orientation = ParseOrientation(position[2]);
- // Read the instructions for the robot.
- string instructions = Console.ReadLine();
- // Create a new Robot object and add it to the list of robots.
- robots.Add(new Robot(x, y, orientation, instructions, grid));
- }
- // Create a new MartianRobots simulation and run it.
- MartianRobots simulation = new MartianRobots(grid, robots);
- simulation.Run();
- }
- // ParseOrientation parses an orientation string and returns the corresponding Orientation value.
- private static Orientation ParseOrientation(string str)
- {
- switch (str)
- {
- case "N":
- return Orientation.North;
- case "S":
- return Orientation.South;
- case "E":
- return Orientation.East;
- case "W":
- return Orientation.West;
- default:
- throw new ArgumentException($"Invalid orientation: {str}");
- }
- }
- }
- public enum Orientation
- {
- North,
- South,
- East,
- West
- }
- // MartianRobots is a class that represents a simulation of robots moving on a grid.
- public class MartianRobots
- {
- // grid is the grid on which the robots are moving.
- private readonly IGrid grid;
- // robots is a list of IRobot objects representing the robots in the simulation.
- private readonly List<IRobot> robots;
- // Constructor creates a new MartianRobots simulation with the given grid and robots.
- public MartianRobots(IGrid grid, List<IRobot> robots)
- {
- this.grid = grid;
- this.robots = robots;
- }
- // Run runs the simulation and prints the final positions of the robots to standard output.
- public void Run()
- {
- // Simulate the movements of the robots and print their final positions.
- foreach (IRobot robot in robots)
- {
- robot.Move();
- Console.WriteLine(robot);
- }
- }
- }
- // IGrid is an interface representing a grid on which robots can move.
- public interface IGrid
- {
- // Width and Height are the dimensions of the grid.
- int Width { get; }
- int Height { get; }
- // IsLost returns true if a robot has been lost at the given position on the grid.
- bool IsLost(int x, int y);
- // MarkLost marks a robot as lost at the given position on the grid.
- void MarkLost(int x, int y);
- }
- // Grid is a class that represents a grid on which robots can move.
- public class Grid : IGrid
- {
- // grid is a two-dimensional array of booleans representing the positions where robots
- // have been lost.
- private readonly bool[,] grid;
- // Width and Height are the dimensions of the grid.
- public int Width { get; }
- public int Height { get; }
- // Constructor creates a new grid with the given dimensions.
- public Grid(int width, int height)
- {
- Width = width;
- Height = height;
- grid = new bool[width, height];
- }
- // IsLost returns true if a robot has been lost at the given position on the grid.
- public bool IsLost(int x, int y)
- {
- return grid[x, y];
- }
- // MarkLost marks a robot as lost at the given position on the grid.
- public void MarkLost(int x, int y)
- {
- grid[x, y] = true;
- }
- }
- // IRobot is an interface representing a robot that can move on a grid.
- public interface IRobot
- {
- // PositionX and PositionY are the coordinates of the robot's current position on the grid.
- int PositionX { get; }
- int PositionY { get; }
- // Orientation is the robot's current orientation.
- Orientation Orientation { get; }
- // IsLost returns true if the robot has been lost.
- bool IsLost { get; }
- // Move instructs the robot to move according to the given instructions.
- void Move();
- }
- // Robot is a class that represents a robot that can move on a grid.
- public class Robot : IRobot
- {
- // grid is the grid on which the robot is moving.
- private readonly IGrid grid;
- // positionX and positionY are the coordinates of the robot's current position on the grid.
- private int positionX;
- private int positionY;
- // orientation is the robot's current orientation.
- private Orientation orientation;
- private readonly string instructions;
- // isLost is a flag indicating whether the robot has been lost.
- private bool isLost;
- // PositionX and PositionY are the coordinates of the robot's current position on the grid.
- public int PositionX => positionX;
- public int PositionY => positionY;
- // Orientation is the robot's current orientation.
- public Orientation Orientation => orientation;
- // IsLost returns true if the robot has been lost.
- public bool IsLost => isLost;
- // Constructor creates a new robot with the given initial position and orientation on the given grid.
- public Robot(int positionX, int positionY, Orientation orientation, string instructions, IGrid grid)
- {
- this.positionX = positionX;
- this.positionY = positionY;
- this.orientation = orientation;
- this.instructions = instructions;
- this.grid = grid;
- }
- // Move instructs the robot to move according to the given instructions.
- public void Move()
- {
- foreach (char c in instructions)
- {
- int initialX = positionX;
- int initialY = positionY;
- switch (c)
- {
- case 'L':
- // Turn left.
- orientation = (Orientation)(((int)orientation + 3) % 4);
- break;
- case 'R':
- // Turn right.
- orientation = (Orientation)(((int)orientation + 1) % 4);
- break;
- case 'F':
- // Move forward.
- switch (orientation)
- {
- case Orientation.North:
- positionY++;
- break;
- case Orientation.South:
- positionY--;
- break;
- case Orientation.East:
- positionX++;
- break;
- case Orientation.West:
- positionX--;
- break;
- }
- // Check if the robot has fallen off the grid.
- if (positionX < 0 || positionX >= grid.Width || positionY < 0 || positionY >= grid.Height)
- {
- // The robot has fallen off the grid.
- if (!grid.IsLost(initialX, initialY))
- {
- // This is the first robot to be lost at this position.
- // Leave a scent at the initial position of the robot.
- grid.MarkLost(initialX, initialY);
- }
- isLost = true;
- }
- break;
- }
- // If the robot has been lost, ignore the remaining instructions.
- if (isLost)
- {
- break;
- }
- }
- }
- public override string ToString()
- {
- // Format the output string.
- string str = $"{positionX} {positionY} {orientation.ToString()[0]}";
- if (isLost)
- {
- str += " LOST";
- }
- return str;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement