-
Notifications
You must be signed in to change notification settings - Fork 9
/
Knight Kills King.c
228 lines (214 loc) · 4.66 KB
/
Knight Kills King.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
A knight in a chess game is determined to kill the enemy king.
The chess board is of the size N*N. The knight can move through the empty cells but he cannot
land on a cell where friendly units are placed. The knight is represented as K,
the friendly units are represented as F, the enemy king is
represented as E and the empty cells are represented as X.
Find the minimum number of moves needed for the knight to reach the enemy king.
The knight moves to a square that is two squares away horizontally and one square vertically,
or two squares vertically and one square horizontally. The complete move therefore looks like the letter L.
Input Format:
The first line contains N.
The next N lines represent the chess board.
Output Format:
The first line contains the minimum number of moves required to reach the enemy king by the knight.
Example Input/Output 1:
Input:
5
KXXXX
XXXXX
XXXXX
XXXEX
XXXXX
Output:
2
Example Input/Output 2:
Input:
7
EXXXXXX
XXXXXXX
FFXXXXX
XXKXXXX
FXXXXFX
XXXXXXX
XXXXXXX
Output:
3
#include <iostream>
#include<queue>
using namespace std;
struct state
{
int x,y,dis;
state() {}
state(int x,int y,int dis):x(x),y(y),dis(dis) {}
};
bool inside(int x,int y,int n)
{
if(x>=0&&y>=0&&x<n&&y<n)
return true;
return false;
}
int main(int argc, char** argv)
{
int n;
cin>>n;
int chess[n][n],src[2],des[2];;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
char t;
cin>>t;
if(t=='K')
{
src[0]=i;
src[1]=j;
chess[i][j]=1;
}
else if(t=='E')
{
des[0]=i;
des[1]=j;
chess[i][j]=0;
}
else if(t=='F')
chess[i][j]=1;
else
chess[i][j]=0;
}
int x=src[0],y=src[1],dis=0;
int dx[8]={-2,-2,2,2,-1,-1,1,1};
int dy[8]={-1,1,-1,1,-2,2,-2,2};
queue<state> q;
q.push(state(x,y,dis));
state st;
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==des[0]&&st.y==des[1])
break;
for(int i=0;i<8;i++)
{
x=st.x+dx[i];
y=st.y+dy[i];
if(inside(x,y,n)&&!chess[x][y])
{
q.push(state(x,y,st.dis+1));
chess[x][y]=1;
}
}
}
cout<<st.dis;
}
(or)
#include <iostream>
#include <map>
#include <queue>
#include <climits>
using namespace std;
char a[1000][1000];
int row[] = {2,2,-2,-2,1,1,-1,-1};
int col[] = {-1,1,1,-1,2,-2,2,-2};
bool valid(int x,int y,int n){
if(x<0||y<0||x>=n||y>=n||a[x][y]=='F'){
return false;
}
return true;
}
struct Node{
int x,y,dist;
Node(int x,int y,int dist=0):x(x),y(y),dist(dist){}
bool operator<(const Node& o) const{
return x < o.x || (x == o.x && y < o.y);
}
};
int solve(Node s,Node d,int n){
map<Node,bool> visted;
queue<Node> q;
q.push(s);
while(!q.empty()){
Node node=q.front();
q.pop();
int x=node.x;
int y=node.y;
int dist=node.dist;
if(x==d.x&&y==d.y){
return dist;
}
if(!visted.count(node)){
visted[node]=true;
for(int i=0;i<8;i++){
int x1=x+row[i];
int y1=y+col[i];
if(valid(x1,y1,n)){
q.push({x1,y1,dist+1});
}
}
}
}
}
int main(){
int n,x1,x2,y1,y2;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
if(a[i][j]=='K'){
x1=i;
x2=j;
}
if(a[i][j]=='E'){
y1=i;
y2=j;
}
}
}
cout<<solve({x1,x2},{y1,y2},n);
}
(or)
#include <iostream>
using namespace std;
int n,mini=40,a[100][100];
int mx[8]={1,1,2,2,-2,-2,-1,-1},my[8]={-2,2,-1,1,-1,1,2,-2};
int issafe(int a,int b)
{
return (a>=0 && a<n && b>=0 && b<n);
}
void solve(string s[],int x,int y,int m)
{
if(mini<=m)
return;
for(int i=0;i<8;i++)
if(issafe(x+mx[i],y+my[i]))
{
if(s[x+mx[i]][y+my[i]]=='X' && a[x+mx[i]][y+my[i]]>m)
{
a[x+mx[i]][y+my[i]]=m;
s[x+mx[i]][y+my[i]]='K';
solve(s,x+mx[i],y+my[i],m+1);
s[x+mx[i]][y+my[i]]='X';
}
if(s[x+mx[i]][y+my[i]]=='E' && mini>m)
{
mini=m+1;
return;
}
}
}
int main(){
int i,j,x,y;
cin>>n;
string s[n];
for(i=0;i<n;i++)
{
cin>>s[i];
for(j=0;j<n;j++)
{
if(s[i][j]=='K')
x=i,y=j;
a[i][j]=100;
}
}
solve(s,x,y,0);
cout<<mini;
}