Advertisement
hkshakib

Untitled

May 29th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int x1,y1,x2,y2;
  4. int ans;
  5. int grid[10][10],vis[10][10];
  6. int dx[]={-2,-2,-1,-1,1,1,2,2};
  7. int dy[]={ -1,1,-2,2,-2,2,-1,1};
  8. int cnt=0;
  9. void DFS(int i,int j){
  10.     //cout<<3<<endl;
  11.  
  12.     if(i>8 || i<1 || j>8 || j<1 || vis[i][j])
  13.         return;
  14.          if((i==x2 && j==y2) ){
  15.         ans=min(cnt,ans);
  16.         cnt=0;
  17.         return;
  18.     }
  19.  
  20.     vis[i][j]=1;
  21.     cnt++;
  22.     for(int k=0; k<8; k++){
  23.         int x=i+dx[k];
  24.         int y=j+dy[k];
  25.         DFS(x,y);
  26.     }
  27. }
  28. int main()
  29. {
  30.     char str[4],str2[4];
  31.     while(cin>>str>>str2){
  32.         x1=str[0]-96;
  33.         y1=str[1]-'0';
  34.         x2=str2[0]-96;
  35.         y2=str2[1]-'0';
  36.         ans=INT_MAX;
  37.         cnt=0;
  38.         //cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
  39.         DFS(x1,y1);
  40.         cout<<ans<<endl;
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement