{"id":22513666,"url":"https://github.com/emahtab/minimum-knight-moves","last_synced_at":"2026-02-07T00:33:29.219Z","repository":{"id":79525658,"uuid":"332361891","full_name":"eMahtab/minimum-knight-moves","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-05T07:18:19.000Z","size":98,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-26T12:51:49.594Z","etag":null,"topics":["bfs","breadth-first-search"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-01-24T03:50:58.000Z","updated_at":"2024-10-05T07:18:22.000Z","dependencies_parsed_at":"2025-04-14T12:40:37.752Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/minimum-knight-moves","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/minimum-knight-moves","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fminimum-knight-moves","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fminimum-knight-moves/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fminimum-knight-moves/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fminimum-knight-moves/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/minimum-knight-moves/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fminimum-knight-moves/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266801406,"owners_count":23986371,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bfs","breadth-first-search"],"created_at":"2024-12-07T03:13:57.338Z","updated_at":"2026-02-07T00:33:24.189Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Minimum Knight Moves\n\n## https://leetcode.com/problems/minimum-knight-moves\n\nIn an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0].\n\nA knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.\n\n![Knight Moves](knight.png?raw=true \"Knight Moves\")\n\nReturn the minimum number of steps needed to move the knight to the square [x, y].  It is guaranteed the answer exists.\n\n# Solution 1 : Time Limit Exceeded\n```java\nclass Solution {\n    public int minKnightMoves(int x, int y) {\n        int[][] moves = { {2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};\n        \n        Queue\u003cint[]\u003e q = new LinkedList\u003c\u003e();\n        q.add(new int[]{0, 0});\n        \n        Set\u003cString\u003e visited = new HashSet\u003c\u003e();\n        visited.add(\"0,0\");\n        \n        int minMoves = 0;\n        while(!q.isEmpty()) {\n            int size = q.size();\n            for(int i = 0; i \u003c size; i++) {\n                int[] curr = q.remove();\n                if(curr[0] == x \u0026\u0026 curr[1] == y) {\n                    return minMoves;\n                }\n            \n                for(int[] move : moves) {\n                    int posX = curr[0] + move[0];\n                    int posY = curr[1] + move[1];\n                    if(!visited.contains(posX + \",\" + posY)) {\n                        visited.add(posX + \",\" + posY);\n                        q.add(new int[]{posX, posY});\n                    }\n                }\n            }\n            \n            minMoves++;    \n        }\n        return minMoves;\n    }\n}\n```\n\n## Knight Moves Symmetry\n\n![Knight Moves Symmetry](knight-moves-symmetry.png?raw=true \"Knight Moves Symmetry\")\n\nIf you look carefully in the above diagram, knight moves are symmetric vertically, horizontally and diagonally.\n\n# Solution 2 : Using Symmetry\n\n```java\nclass Solution {\n    public int minKnightMoves(int x, int y) {\n        int[][] moves = { {2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};\n        int absX = Math.abs(x);\n        int absY = Math.abs(y);\n        \n        Queue\u003cint[]\u003e q = new LinkedList\u003c\u003e();\n        q.add(new int[]{0, 0});\n        \n        Set\u003cString\u003e visited = new HashSet\u003c\u003e();\n        visited.add(\"0,0\");\n        \n        int minMoves = 0;\n        while(!q.isEmpty()) {\n            int size = q.size();\n            for(int i = 0; i \u003c size; i++) {\n                int[] curr = q.remove();\n                if(curr[0] == absX \u0026\u0026 curr[1] == absY) {\n                    return minMoves;\n                }\n            \n                for(int[] move : moves) {\n                    int posX = curr[0] + move[0];\n                    int posY = curr[1] + move[1];\n                    if(posX \u003c 0 || posY \u003c -1) // we can also write it like (posX \u003c -1 || posY \u003c 0) continue;\n                        continue;\n                    if(!visited.contains(posX + \",\" + posY)) {\n                        visited.add(posX + \",\" + posY);\n                        q.add(new int[]{posX, posY});\n                    }\n                }\n            }\n            \n            minMoves++;    \n        }\n        return minMoves;\n    }\n}\n```\n## Why we are neglecting posX \u003c -1 and posY \u003c 0\nWe only have issue reaching cell (1,1), to reach that cell we need to visit either cell (-1, 2) or cell (2, -1). All other cells can be skipped, where posX is less than -1 or posY is less than -1. By doing this we are reducing our search space drastically and its a great improvement by utilising the fact that Knight Moves are symmetric.\n\n# Time Complexity = (8^N)\n\n# References :\n1. https://leetcode.com/problems/minimum-knight-moves/discuss/392053/Here-is-how-I-get-the-formula-(with-graphs)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fminimum-knight-moves","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fminimum-knight-moves","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fminimum-knight-moves/lists"}