You are viewing a single comment's thread. Return to all comments →
int travel (int n, int a, int b) { vector<vector<bool>> visited(n, vector<bool>(n, false)); queue<vector<pair<int, int>>> Q; Q.push({{0, 0}}); while (!Q.empty()) { vector<pair<int, int>> T = Q.front(); Q.pop(); int x = T.back().first; int y = T.back().second; vector<int> one = {-1, 1}; set<pair<int, int>> neighbours;; for (int i : one) { for (int j : one) { neighbours.insert({x+i*a, y+j*b}); neighbours.insert({x+i*b, y+j*a}); } } for (auto p : neighbours){ int x2 = p.first, y2 = p.second; if (x2 == n-1 and y2 == n-1) return T.size(); if (0 <= x2 and x2 < n and 0 <= y2 and y2 < n and !visited[x2][y2]) { vector<pair<int, int>> history = T; history.push_back({x2, y2}); Q.push(history); visited[x2][y2] = true; } } } return -1; } vector<vector<int>> knightlOnAChessboard(int n) { vector<vector<int>> answer(n-1, vector<int>(n-1)); for (int i=1; i < n; i++) { for (int j=1; j < n; j++) { answer[i-1][j-1] = travel(n, i, j); } } return answer; }
Seems like cookies are disabled on this browser, please enable them to open this website
KnightL on a Chessboard
You are viewing a single comment's thread. Return to all comments →