真正二分找错环节

来源:互联网 发布:bartender vb 编辑:程序博客网 时间:2024/05/01 23:03

Knight Moves

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 8
Problem Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.<br>Of course you know that it is vice versa. So you offer him to write a program that solves the &quot;difficult&quot; part. <br><br>Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b. <br>
 

Input
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard. <br>
 

Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.". <br>
 

Sample Input
e2 e4<br>a1 b2<br>b2 c3<br>a1 h8<br>a1 h7<br>h8 a1<br>b1 c3<br>f6 f6<br>
 

Sample Output
To get from e2 to e4 takes 2 knight moves.<br>To get from a1 to b2 takes 4 knight moves.<br>To get from b2 to c3 takes 2 knight moves.<br>To get from a1 to h8 takes 6 knight moves.<br>To get from a1 to h7 takes 5 knight moves.<br>To get from h8 to a1 takes 6 knight moves.<br>To get from b1 to c3 takes 1 knight moves.<br>To get from f6 to f6 takes 0 knight moves.<br>
 

Source

University of Ulm Local Contest 1996

哈哈哈

象棋跳马

宝宝心很苦

#include<iostream>  #include<queue>  using namespace std;    /* 搜索节点 */  typedef struct square  {      int i;   //棋盘格子横坐标      int j;   //棋盘格子纵坐标      int num; //行至该格子经历的步数  }Square;    /************************************* * 主函数 *************************************/  int main()  {      queue<Square> q;//广搜队列      Square d;      int i,j,si,sj,ei,ej,ti,tj,tnum;      char s1[3],s2[3];      bool reach[8][8];//reach[i][j]表示格子(i,j)是否被搜索过      //针对每一组用例进行计算      while(cin>>s1>>s2)      {          for(i=0;i<=7;i++)              for(j=0;j<=7;j++)                  reach[i][j]=false;              while(!q.empty())                  q.pop();              //获得马的起始位置坐标(si,sj)以及步数0压入广搜队列              si=s1[0]-'a';//起始位置横坐标              sj=s1[1]-'1';//起始位置纵坐标              ei=s2[0]-'a';//目的位置横坐标              ej=s2[1]-'1';//目的位置纵坐标              d.i=si;              d.j=sj;              d.num=0;              q.push(d);//起始位置节点压入广搜队列              //进行广度优先搜索              while(!q.empty())              {                  //弹出队头节点d                  d=q.front();                  q.pop();                  ti=d.i;                  tj=d.j;                  tnum=d.num;                  //该节点已经到达目的格子                  if(ti==ei&&tj==ej)                  {                      cout<<"To get from ";                      cout<<s1<<" to "<<s2;                      cout<<" takes "<<tnum<<" knight moves."<<endl;                      break;                  }                  //该节点代表的格子已经被搜索过                  if(reach[ti][tj])                      continue;                  reach[ti][tj]=true;                  //搜索当前节点位置的左上纵位置入广搜队列                  if(ti-2>=0&&tj-1>=0)                  {                      d.i=ti-2;                      d.j=tj-1;                      d.num=tnum+1;                      q.push(d);                  }                  //搜索当前节点位置的右上纵位置入广搜队列                  if(ti-2>=0&&tj+1<=7)                  {                      d.i=ti-2;                      d.j=tj+1;                      d.num=tnum+1;                      q.push(d);                  }                  //搜索当前节点位置的左上横位置入广搜队列                  if(ti-1>=0&&tj-2>=0)                  {                      d.i=ti-1;                      d.j=tj-2;                      d.num=tnum+1;                      q.push(d);                  }                  //搜索当前节点位置的右上横位置入广搜队列                  if(ti-1>=0&&tj+2<=7)                  {                      d.i=ti-1;                      d.j=tj+2;                      d.num=tnum+1;                      q.push(d);                  }                  //搜索当前节点位置的左下横位置入广搜队列                  if(ti+1<=7&&tj-2>=0)                  {                      d.i=ti+1;                      d.j=tj-2;                      d.num=tnum+1;                      q.push(d);                  }                  //搜索当前节点位置的右下横位置入广搜队列                  if(ti+1<=7&&tj+2<=7)                  {                      d.i=ti+1;                      d.j=tj+2;                      d.num=tnum+1;                      q.push(d);                  }                  //搜索当前节点位置的左下纵位置入广搜队列                  if(ti+2<=7&&tj-1>=0)                  {                      d.i=ti+2;                      d.j=tj-1;                      d.num=tnum+1;                      q.push(d);                  }                  //搜索当前节点位置的右下纵位置入广搜队列                  if(ti+2<=7&&tj+1<=7)                  {                      d.i=ti+2;                      d.j=tj+1;                      d.num=tnum+1;                      q.push(d);                  }              }      }        return 0;  }  



0 0
原创粉丝点击