Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions the knight's tour problems.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
vector<vector<int>> knightTour(int n) {
vector<vector<int>> board(n, vector<int>(n, -1));

int dx[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};

// ✅ Corrected: proper lambda closing and return type
auto isSafe = [&](int x, int y) -> bool {
return (x >= 0 && y >= 0 && x < n && y < n && board[x][y] == -1);
};

function<bool(int, int, int)> solve = [&](int x, int y, int step) -> bool {
if (step == n * n) return true;

for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];

if (isSafe(nx, ny)) {
board[nx][ny] = step;
if (solve(nx, ny, step + 1)) return true;
board[nx][ny] = -1; // backtrack
}
}
return false;
};

board[0][0]