Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int x1,y1,x2,y2;
- int ans;
- int grid[10][10],vis[10][10];
- int dx[]={-2,-2,-1,-1,1,1,2,2};
- int dy[]={ -1,1,-2,2,-2,2,-1,1};
- int cnt=0;
- void DFS(int i,int j){
- //cout<<3<<endl;
- if(i>8 || i<1 || j>8 || j<1 || vis[i][j])
- return;
- if((i==x2 && j==y2) ){
- ans=min(cnt,ans);
- cnt=0;
- return;
- }
- vis[i][j]=1;
- cnt++;
- for(int k=0; k<8; k++){
- int x=i+dx[k];
- int y=j+dy[k];
- DFS(x,y);
- }
- }
- int main()
- {
- char str[4],str2[4];
- while(cin>>str>>str2){
- x1=str[0]-96;
- y1=str[1]-'0';
- x2=str2[0]-96;
- y2=str2[1]-'0';
- ans=INT_MAX;
- cnt=0;
- //cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
- DFS(x1,y1);
- cout<<ans<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement