title: KNight MOVe shortest
body: <p>MY code is here: Question is to find min number of moves to go from one sq. to other in 8*8 chess board . </p>
  
  <pre><code>    #include&lt;iostream&gt;
      using namespace std;
      int n;
      int a[12][12];
      int min1=1000,xd=5,yd=2,ys,xs,xsi,ysi;
  
      int find_path(int xs,int ys)
      {
          cout&lt;&lt;xs&lt;&lt;"  "&lt;&lt;ys&lt;&lt;endl;
      if((xs==xd) &amp;&amp; (ys==yd)) {  cout&lt;&lt;"destiny schieved  "&lt;&lt;endl; return 0;}      
      if(a[xs][ys]==1 || xs&lt;0 || ys&lt;0 || xs&gt;7 || ys&gt;7) return 10000;
      a[xs][ys]=1;
      int a1=1+(find_path(xs-2,ys+1)) ;
      int b=1+(find_path(xs-2,ys-1)) ;
      int c=1+(find_path(xs-1,ys+2)) ;
      int d=1+(find_path(xs-1,ys-2)) ;
      int d=1+(find_path(xs+2,ys+1)) ;
      int e=1+(find_path(xs+2,ys-1)) ;
      int f=1+(find_path(xs+1,ys+2)) ;
      int g=1+(find_path(xs+1,ys-2)) ;
      a[xs][ys]=0;
      return min(a1,b,c,d,e,f,g);
      }
  
  
      int main()
      {
          int i,j,k;
  
          for(i=0;i&lt;8;i++)
          for(j=0;j&lt;8;j++)
          a[i][j]=0;
  
    cout&lt;&lt;"start"&lt;&lt;endl;
  
    cout&lt;&lt;find_path(0,7);
  
        system("pause");
          return 0;
          }
  </code></pre>
  
  <p>This is my code for traversing from one square to other in 8*8 chess board .
  MY code gives wrong answer for some cases :</p>
  
  <p>a[xs][ys]=1; is for preventing loops.
  for eg answer for  (0,7) ->>>> (5,2) is 4 but my algo gives 38 . MY  coordinate axis are X: from left to right and Y-axis: from top to bottom . Please help me solving my problem. </p>
  
  <p>Few solutions are:</p>
  
  <p>(7,0) ->>> (0,7)   : 6
  (0,7)  ->>> (5,2)  :4</p>
  
  <p>I have also tried another code which i later edited to get the above code:</p>
  
  <pre><code>  int find_path(int xs,int ys,int path)
      {
          cout&lt;&lt;xs&lt;&lt;"  "&lt;&lt;ys&lt;&lt;endl;
      if((xs==xd) &amp;&amp; (ys==yd)) { if(min1&gt;path) min1=path; cout&lt;&lt;"destiny schieved  "&lt;&lt;path&lt;&lt;endl; return 1;}      
      if(a[xs][ys]==1 || xs&lt;0 || ys&lt;0 || xs&gt;7 || ys&gt;7) return 0;
      a[xs][ys]=1;
      if(find_path(xs-2,ys+1,path+1)) {if(path==0) {cout&lt;&lt;"i am on start1"&lt;&lt;endl;} else return 1;}
      if(find_path(xs-2,ys-1,path+1)) {if(path==0) {cout&lt;&lt;"i am on start2"&lt;&lt;endl;} else return 1; }
      if(find_path(xs-1,ys+2,path+1)) {if(path==0) {cout&lt;&lt;"i am on start3"&lt;&lt;endl;} else return 1; }
      if(find_path(xs-1,ys-2,path+1)) {if(path==0) {cout&lt;&lt;"i am on start4"&lt;&lt;endl;} else return 1;}
      if(find_path(xs+2,ys+1,path+1)) {if(path==0) {cout&lt;&lt;"i am on start5"&lt;&lt;endl;} else return 1;}
      if(find_path(xs+2,ys-1,path+1)) {if(path==0) {cout&lt;&lt;"i am on start6"&lt;&lt;endl;} else return 1;}
      if(find_path(xs+1,ys+2,path+1)) {if(path==0) {cout&lt;&lt;"i am on start7"&lt;&lt;endl;} else return 1; }
      if(find_path(xs+1,ys-2,path+1)) {if(path==0) {cout&lt;&lt;"i am on start8"&lt;&lt;endl;} else return 1; }
      a[xs][ys]=0;
      return 0;
      }
  </code></pre>