| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.WalkingRobotSimulation; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class WalkingRobotSimulationHashSet : IWalkingRobotSimulation |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m + n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="commands"></param> |
| | 22 | | /// <param name="obstacles"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int RobotSim(int[] commands, int[][] obstacles) |
| 7 | 25 | | { |
| 7 | 26 | | var result = 0; |
| | 27 | |
|
| 7 | 28 | | var x = 0; |
| 7 | 29 | | var y = 0; |
| | 30 | |
|
| 7 | 31 | | var direction = 0; |
| | 32 | |
|
| 7 | 33 | | var obstaclesHashSet = new HashSet<(int, int)>(); |
| | 34 | |
|
| 65 | 35 | | foreach (var obstacle in obstacles) |
| 22 | 36 | | { |
| 22 | 37 | | obstaclesHashSet.Add((obstacle[0], obstacle[1])); |
| 22 | 38 | | } |
| | 39 | |
|
| 115 | 40 | | foreach (var command in commands) |
| 47 | 41 | | { |
| 47 | 42 | | switch (command) |
| | 43 | | { |
| | 44 | | case -1: |
| 11 | 45 | | direction++; |
| | 46 | |
|
| 11 | 47 | | if (direction > 3) |
| 1 | 48 | | { |
| 1 | 49 | | direction = 0; |
| 1 | 50 | | } |
| | 51 | |
|
| 11 | 52 | | break; |
| | 53 | |
|
| | 54 | | case -2: |
| | 55 | |
|
| 6 | 56 | | direction--; |
| | 57 | |
|
| 6 | 58 | | if (direction < 0) |
| 1 | 59 | | { |
| 1 | 60 | | direction = 3; |
| 1 | 61 | | } |
| | 62 | |
|
| 6 | 63 | | break; |
| | 64 | | default: |
| 30 | 65 | | var move = Move(direction, x, y, command, obstaclesHashSet); |
| | 66 | |
|
| 30 | 67 | | x = move.X; |
| 30 | 68 | | y = move.Y; |
| | 69 | |
|
| 30 | 70 | | result = Math.Max(result, (x * x) + (y * y)); |
| | 71 | |
|
| 30 | 72 | | break; |
| | 73 | | } |
| 47 | 74 | | } |
| | 75 | |
|
| 7 | 76 | | return result; |
| 7 | 77 | | } |
| | 78 | |
|
| | 79 | | private static (int X, int Y) Move(int direction, int x, int y, int units, |
| | 80 | | HashSet<(int, int)> obstaclesHashSet) |
| 30 | 81 | | { |
| 484 | 82 | | for (var i = 0; i < units; i++) |
| 213 | 83 | | { |
| 213 | 84 | | switch (direction) |
| | 85 | | { |
| | 86 | | case 0: |
| 103 | 87 | | if (obstaclesHashSet.Contains((x, y + 1))) |
| 0 | 88 | | { |
| 0 | 89 | | return (x, y); |
| | 90 | | } |
| | 91 | |
|
| 103 | 92 | | y++; |
| 103 | 93 | | break; |
| | 94 | | case 1: |
| 68 | 95 | | if (obstaclesHashSet.Contains((x + 1, y))) |
| 1 | 96 | | { |
| 1 | 97 | | return (x, y); |
| | 98 | | } |
| | 99 | |
|
| 67 | 100 | | x++; |
| 67 | 101 | | break; |
| | 102 | | case 2: |
| 24 | 103 | | if (obstaclesHashSet.Contains((x, y - 1))) |
| 0 | 104 | | { |
| 0 | 105 | | return (x, y); |
| | 106 | | } |
| | 107 | |
|
| 24 | 108 | | y--; |
| 24 | 109 | | break; |
| | 110 | | case 3: |
| 18 | 111 | | if (obstaclesHashSet.Contains((x - 1, y))) |
| 0 | 112 | | { |
| 0 | 113 | | return (x, y); |
| | 114 | | } |
| | 115 | |
|
| 18 | 116 | | x--; |
| 18 | 117 | | break; |
| | 118 | | } |
| 212 | 119 | | } |
| | 120 | |
|
| 29 | 121 | | return (x, y); |
| 30 | 122 | | } |
| | 123 | | } |