-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy path07_minimum_step_by_knight.cpp
170 lines (134 loc) · 3.8 KB
/
07_minimum_step_by_knight.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
link: https://practice.geeksforgeeks.org/problems/steps-by-knight5927/1
sol: https://www.geeksforgeeks.org/minimum-steps-reach-target-knight/
*/
// ----------------------------------------------------------------------------------------------------------------------- //
/*
TC: O(N^2)
SC: O(N^2)
*/
// C++ program to find minimum steps to reach to
// specific cell in minimum moves by Knight
#include <bits/stdc++.h>
using namespace std;
// structure for storing a cell's data
struct cell {
int x, y;
int dis;
cell() {}
cell(int x, int y, int dis)
: x(x), y(y), dis(dis)
{
}
/* we can also use this (implicit pointer) for value updation
cell(int x, int y, int dis)
{
this->x = x;
this->y = y;
this->dis = dis;
}
*/
};
// Utility method returns true if (x, y) lies
// inside Board
bool isInside(int x, int y, int N)
{
if (x >= 1 && x <= N && y >= 1 && y <= N)
return true;
return false;
}
// Method returns minimum step
// to reach target position
int minStepToReachTarget(
int knightPos[], int targetPos[],
int N)
{
// x and y direction, where a knight can move
int dx[] = { -2, -1, 1, 2, -2, -1, 1, 2 };
int dy[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
// queue for storing states of knight in board
queue<cell> q;
// push starting position of knight with 0 distance
q.push(cell(knightPos[0], knightPos[1], 0));
cell t;
int x, y;
bool visit[N + 1][N + 1];
// make all cell unvisited
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
visit[i][j] = false;
// visit starting state
visit[knightPos[0]][knightPos[1]] = true;
// loop untill we have one element in queue
while (!q.empty()) {
t = q.front();
q.pop();
// if current cell is equal to target cell,
// return its distance
if (t.x == targetPos[0] && t.y == targetPos[1])
return t.dis;
// loop for all reachable states
for (int i = 0; i < 8; i++) {
x = t.x + dx[i];
y = t.y + dy[i];
// If reachable state is not yet visited and
// inside board, push that state into queue
if (isInside(x, y, N) && !visit[x][y]) {
visit[x][y] = true;
q.push(cell(x, y, t.dis + 1));
}
}
}
}
// Driver code to test above methods
int main()
{
int N = 30;
int knightPos[] = { 1, 1 };
int targetPos[] = { 30, 30 };
cout << minStepToReachTarget(knightPos, targetPos, N);
return 0;
}
// ----------------------------------------------------------------------------------------------------------------------- //
// [accepted]
bool isValid(int x, int y, int N) {
if (x >= 1 && x <= N && y >= 1 && y <= N) {
return true;
}
return false;
}
struct cell {
int x, y;
int dis;
cell() {}
cell(int x, int y, int dis)
{
this->x = x;
this->y = y;
this->dis = dis;
}
};
int minStepToReachTarget(vector<int>& KnightPos, vector<int>& TargetPos, int N)
{
int dx[] = { -2, -1, 1, 2, -2, -1, 1, 2 };
int dy[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
queue<cell> q;
q.push(cell(KnightPos[0], KnightPos[1], 0));
cell t;
int x, y;
vector<vector<bool>> vis(N + 1, vector<bool>(N + 1, false));
vis[KnightPos[0]][KnightPos[1]] = true;
while (!q.empty()) {
t = q.front();
q.pop();
if (t.x == TargetPos[0] && t.y == TargetPos[1]) return t.dis;
for (int i = 0;i < 8;i++) {
x = t.x + dx[i];
y = t.y + dy[i];
if (isValid(x, y, N) && !vis[x][y]) {
vis[x][y] = true;
q.push(cell(x, y, t.dis + 1));
}
}
}
}