-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathM0025.cpp
65 lines (54 loc) · 1.43 KB
/
M0025.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem Statement: https://www.hackerrank.com/challenges/knightl-on-chessboard/problem
*/
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
bool valid(int x, int y, int n) {
return (x >= 0 && x < n) && (y >= 0 && y < n);
}
int knightL(int a, int b, int n) {
int x, y, next_x, next_y, moves;
vector<int> x_moves, y_moves;
queue< tuple<int, int, int> > q;
vector< vector<bool> > visited(n, vector<bool>(n));
x_moves = {a, a, -a, -a, b, b, -b, -b};
y_moves = {b, -b, b, -b, a, -a, a, -a};
visited[0][0] = true;
q.push({0, 0, 0});
while (!q.empty()) {
tie(x, y, moves) = q.front();
q.pop();
for (int i = 0; i < x_moves.size(); i++) {
next_x = x + x_moves[i];
next_y = y + y_moves[i];
if (next_x == n - 1 && next_y == n - 1)
return moves + 1;
else if (valid(next_x, next_y, n) && !visited[next_x][next_y]) {
visited[next_x][next_y] = true;
q.push({next_x, next_y, moves + 1});
}
}
}
return -1;
}
vector< vector<int> > knightLOnAChessboard(int n) {
vector< vector<int> > moves(n - 1, vector<int>(n - 1));
for (int i = 1; i < n; i++)
for (int j = 1; j < n; j++)
moves[i - 1][j - 1] = knightL(i, j, n);
return moves;
}
int main() {
int n;
cin >> n;
vector< vector<int> > moves = knightLOnAChessboard(n);
for (int i = 0; i < moves.size(); i++) {
for (int j = 0; j < moves[i].size(); j++)
cout << moves[i][j] << " ";
cout << endl;
}
return 0;
}