-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path361.cpp
More file actions
36 lines (32 loc) · 969 Bytes
/
Copy path361.cpp
File metadata and controls
36 lines (32 loc) · 969 Bytes
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
#include<bits/stdc++.h>
using namespace std;
bool is_valid(int x,int y,int N){
if(x>=1 && x<=N && y>=1 && y<=N) return true;
return false;
}
int f(int x,int y,int end_x,int end_y,int N,int d,vector<vector<int>> &vis){
if(is_valid(x,y,N) && vis[x][y]!=1){
vis[x][y]=1;
if(x==end_x && y==end_y) {
return true;
}
else{
if(f(x-2,y+1,end_x,end_y,N,d+1,vis)) return true;
if(f(x-1,y+2,end_x,end_y,N,d+1,vis)) return true;
if(f(x+1,y+2,end_x,end_y,N,d+1,vis)) return true;
if(f(x+2,y+1,end_x,end_y,N,d+1,vis)) return true;
if(f(x+2,y-1,end_x,end_y,N,d+1,vis)) return true;
if(f(x+1,y-2,end_x,end_y,N,d+1,vis)) return true;
if(f(x-1,y-2,end_x,end_y,N,d+1,vis)) return true;
if(f(x-2,y-1,end_x,end_y,N,d+1,vis)) return true;
}
}
return false;
}
int main(){
int N=6;
int x=4,y=5,end_x=1,end_y=1;
vector<vector<int>> vis(N+1,vector<int>(N+1,0));
cout<<f(x,y,end_x,end_y,N,0,vis);
return 0;
}