c# beat 100%
\npublic class Solution {\n    public int MinimumMoves(int[][] grid) {\n        \n        Queue<int> q = new Queue<int>();\n        Queue<bool> pos = new Queue<
```\npublic class Solution {\n    public int MinimumMoves(int[][] grid) {\n        \n        Queue<int> q = new Queue<int>();\n        Queue<bool> pos = new Queue<bool>();\n        \n        HashSet<int> v = new HashSet<int>();\n        HashSet<int> h = new HashSet<int>();\n        \n        int n = grid.Length;\n        int start = n * 0 + 1;\n        \n        // horizontal = true / vertical = false\n        q.Enqueue(start);\n        pos.Enqueue(true);\n        \n        int[] rows = new int[4] { 0, -1, 0, 1 };\n        int[] cols = new int[4] { -1, 0, 1, 0 };\n        \n        int move = 0;\n        while(q.Count() > 0)\n        {\n            int count = q.Count();\n            for (int i = 0; i < count; i++)\n            {\n                int num = q.Dequeue();\n                bool d = pos.Dequeue();\n                int r = num / n;\n                int c = num % n;\n                \n                if (r == n -1 && c == n - 1 && d == true)\n                {\n                    return move;\n                }\n                \n                if (d == true) // horizontal\n                {\n                    if (c < n - 1 && grid[r][c + 1] == 0 && !h.Contains(r * n + c + 1))\n                    {\n                        q.Enqueue(r * n + c + 1);\n                        pos.Enqueue(true);\n                        h.Add(r * n + c + 1);\n                    }\n                    \n                    if (r < n - 1 && grid[r + 1][c] == 0 && grid[r + 1][c - 1] == 0 && !h.Contains((r + 1) * n + c))\n                    {\n                        q.Enqueue((r + 1) * n + c);\n                        pos.Enqueue(true);\n                        h.Add((r + 1) * n + c);\n                    }\n                    \n                    if (r < n - 1 && grid[r + 1][c] == 0 && grid[r + 1][c - 1] == 0 && !v.Contains((r + 1) * n + c - 1))\n                    {\n                        q.Enqueue((r + 1) * n + c - 1);\n                        pos.Enqueue(false);\n                        v.Add((r + 1) * n + c - 1);\n                    }\n                }\n                else\n                {\n                    if (c < n - 1 && grid[r - 1][c + 1] == 0 && grid[r][c + 1] == 0 && !v.Contains(r * n + c + 1))\n                    {\n                        q.Enqueue(r * n + c + 1);\n                        pos.Enqueue(false);\n                        v.Add(r * n + c + 1);\n                    }\n                    \n                    if (r < n - 1 && grid[r + 1][c] == 0 && !v.Contains((r + 1) * n + c))\n                    {\n                        q.Enqueue((r + 1) * n + c);\n                        pos.Enqueue(false);\n                        v.Add((r + 1) * n + c);\n                    }\n                    \n                    if (c < n - 1 && grid[r - 1][c + 1] == 0 && grid[r][c + 1] == 0 && !h.Contains((r - 1) * n + c + 1))\n                    {\n                        q.Enqueue((r - 1) * n + c + 1);\n                        pos.Enqueue(true);\n                        h.Add((r - 1) * n + c + 1);\n                    }\n                }\n            }\n            \n            move++;\n        }\n        \n        return -1;\n    }\n}\n```
[]