-
Notifications
You must be signed in to change notification settings - Fork 1
/
KnightL.cpp
64 lines (46 loc) · 1.14 KB
/
KnightL.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
// problem: "https://www.hackerrank.com/challenges/knightl-on-chessboard/problem"
#include<bits/stdc++.h>
#define Pair pair<int,int>
using namespace std;
struct node
{
int x,y,s;
node(int x,int y,int s): x(x),y(y),s(s) {}
};
int minSteps(int a,int b,int n)
{
queue<node>Q;
Q.push(node(0,0,0));
vector<vector<int>>V(n,vector<int>(n,-1));
while(!Q.empty())
{
node p = Q.front();
Q.pop();
if(p.x == n-1 && p.y == n-1)
return p.s;
if(p.x<0 || p.x>=n || p.y<0 || p.y>=n || V[p.x][p.y] != -1)
continue;
V[p.x][p.y] = 1;
Q.push(node(p.x+a,p.y+b,p.s+1));
Q.push(node(p.x+a,p.y-b,p.s+1));
Q.push(node(p.x-a,p.y+b,p.s+1));
Q.push(node(p.x-a,p.y-b,p.s+1));
Q.push(node(p.x+b,p.y+a,p.s+1));
Q.push(node(p.x+b,p.y-a,p.s+1));
Q.push(node(p.x-b,p.y+a,p.s+1));
Q.push(node(p.x-b,p.y-a,p.s+1));
}
return -1;
}
int main()
{
int n,i,j;
cin>>n;
for(i=1;i!=n;i++)
{
for(j=1;j!=n;j++)
cout<<minSteps(i,j,n)<<' '; // define
cout<<'\n';
}
return 0;
}