{"id":22513724,"url":"https://github.com/emahtab/knights-tour","last_synced_at":"2025-03-28T01:24:42.269Z","repository":{"id":43032997,"uuid":"197231649","full_name":"eMahtab/knights-tour","owner":"eMahtab","description":"Knight's Tour Problem","archived":false,"fork":false,"pushed_at":"2020-02-29T15:17:04.000Z","size":387,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-02T03:23:49.534Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","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}},"created_at":"2019-07-16T16:40:48.000Z","updated_at":"2024-08-15T00:02:02.000Z","dependencies_parsed_at":"2022-09-09T16:52:17.026Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/knights-tour","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fknights-tour","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fknights-tour/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fknights-tour/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fknights-tour/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/knights-tour/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950897,"owners_count":20699164,"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","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":[],"created_at":"2024-12-07T03:14:09.841Z","updated_at":"2025-03-28T01:24:42.244Z","avatar_url":"https://github.com/eMahtab.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knight's Tour\nKnight's Tour Problem\n\n\n## Implementation 1 : Whether Knight's Tour is possible or not\n\n```java\nclass KnightTour {\n\tprivate static final int[] xMoves = { 2, 1, -1, -2, -2, -1, 1, 2 };\n\tprivate static final int[] yMoves = { 1, 2, 2, 1, -1, -2, -2, -1 };\n\t\n\tpublic void solveKnightTourProblem(int chessboardSize) {\t\n\t\tint[][] visited = new int[chessboardSize][chessboardSize];\n\t\tvisited[0][0] = 1;\n\t\t// start knight's tour from top left corner square (0, 0)\n\t\tif( solveProblem(visited, 2, 0, 0)) {\n\t\t\tprintSolution(visited);\n\t\t} else {\n\t\t\tSystem.out.println(\"No feasible solution found...\");\n\t\t}\t\t\t\n\t}\n\n\tpublic boolean solveProblem(int[][] visited, int moveCount, int x, int y) {\n\t\t// Base Case : We were able to move to each square exactly once\n\t\tif (moveCount \u003e visited.length * visited.length) {\n\t\t\treturn true;\n\t\t}\n\n\t\tfor (int i = 0; i \u003c xMoves.length; ++i) {\n\t\t\tint nextX = x + xMoves[i];\n\t\t\tint nextY = y + yMoves[i];\n\n\t\t\t// check if new position is a valid and not visited yet\n\t\t\tif ( isValidMove(visited, nextX, nextY) \u0026\u0026 visited[nextX][nextY] == 0) {\n\t\t\t\tvisited[nextX][nextY] = moveCount;\n\t\t\t\tif ( solveProblem(visited, moveCount + 1, nextX, nextY) ) {\n\t\t\t\t\treturn true; \n\t\t\t\t}\n\t\t\t\tvisited[nextX][nextY] = 0;\n\t\t\t}\n\t\t}\n\t   return false;\n\t}\n\n\tpublic boolean isValidMove(int[][] visited, int x, int y) {\n\t\tif (x \u003c 0 || x \u003e= visited.length || y \u003c 0 || y \u003e= visited.length) {\n\t\t\treturn false;\n\t\t} else {\n\t\t\treturn true;\t\n\t\t}\n\t}\n\t\n\tpublic void printSolution(int[][] visited) {\n\t\tfor (int i = 0; i \u003c visited.length; i++) {\n\t\t\tfor (int j = 0; j \u003c visited.length; j++) {\n\t\t\t\tSystem.out.print(visited[i][j] + \" \");\n\t\t\t}\n\t\t\tSystem.out.println();\n\t\t}\n\t}\n}\n\n```\n\n## Implementation 2 : Print all possible Knight's Tour\n\n```java\nclass KnightTour {\n\tprivate static final int[] xMoves = { 2, 1, -1, -2, -2, -1, 1, 2 };\n\tprivate static final int[] yMoves = { 1, 2, 2, 1, -1, -2, -2, -1 };\n\tprivate static int solution;\n\t\n\tpublic void solveKnightTourProblem(int chessboardSize) {\t\n\t\tint[][] visited = new int[chessboardSize][chessboardSize];\n\t\tvisited[0][0] = 1;\n\t\t// start knight's tour from top left corner square (0, 0)\n\t\tsolveProblem(visited, 2, 0, 0);\t\t\t\n\t}\n\n\tpublic void solveProblem(int[][] visited, int moveCount, int x, int y) {\n\t\t// Base Case : We were able to move to each square exactly once\n\t\tif (moveCount \u003e visited.length * visited.length) {\n\t\t\tsolution++;\n\t\t\tprintSolution(visited);\n\t\t} else {\n\t\t\tfor (int i = 0; i \u003c xMoves.length; ++i) {\n\t\t\t\tint nextX = x + xMoves[i];\n\t\t\t\tint nextY = y + yMoves[i];\n\t\t\t\t// check if new position is a valid and not visited yet\n\t\t\t\tif ( isValidMove(visited, nextX, nextY) \u0026\u0026 visited[nextX][nextY] == 0) {\n\t\t\t\t\tvisited[nextX][nextY] = moveCount;\n\t\t\t\t\tsolveProblem(visited, moveCount + 1, nextX, nextY);\n\t\t\t\t\tvisited[nextX][nextY] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic boolean isValidMove(int[][] visited, int x, int y) {\n\t\tif (x \u003c 0 || x \u003e= visited.length || y \u003c 0 || y \u003e= visited.length) {\n\t\t\treturn false;\n\t\t} else {\n\t\t\treturn true;\t\n\t\t}\n\t}\n\t\n\tpublic void printSolution(int[][] visited) {\n\t\tSystem.out.println(\"Solution \" + solution);\n\t\tfor (int i = 0; i \u003c visited.length; i++) {\n\t\t\tfor (int j = 0; j \u003c visited.length; j++) {\n\t\t\t\tSystem.out.print(visited[i][j] + \" \");\n\t\t\t}\n\t\t\tSystem.out.println(\"\");\n\t\t}\n\t\tSystem.out.println(\"\");\n\t}\n}\n\n```\n\n# References :\nhttps://www.youtube.com/watch?v=D8KFwjohDNg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fknights-tour","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fknights-tour","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fknights-tour/lists"}