专题二 第十二道题

来源:互联网 发布:灰原哀 知乎 编辑:程序博客网 时间:2024/05/16 19:42

1.题目编号:1015

2.简单题意:你的一个朋友正在做TKP研究,需要你在n*n正方形的封闭棋盘上寻找骑士最少的移动次数,他认为最困难的是解决两个给定的方格之间寻找最少移动次数。给出两个方块a,b,(a-h)表示列,(1-8)表示行,确定从a到b骑士移动的最短路径。

3.解题思路形成过程:又是一个运用广度搜索的题目,在国际象棋的棋盘上,一匹马一共有8个可能跳跃的方向,而且马走日字。。可以定义八个方向为(1,2)(1,-2)(-1,2)(-1,-2)(2,1)(2,-1)(-2,1)(-2,-1)然后一行相邻的两个点最少需要三步才能走到,和以往直接相加一点都不一样,看了一下其他人的博客才知道怎么找到关系。

4.感悟:以前涉及的是数学、物理方面的知识,如今又有了棋盘上的较量,国际象棋从来都没有下过,还专门找了点看了看,做发现做acm题不光学acm,还能学到好多课外知识最后一直Getting complication error information failed!可是能运行,最后才发现头文件#include<string>需要写成#include<cstring>.

5.AC的代码:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int direction[8][2]={1,2, -1,2, 1,-2, -1,-2, 2,1, -2,1, 2,-1, -2,-1};
int num[10][10];
int sx,sy,ex,ey;
struct node{
 int x,y,step;
};
int judge(int x,int y)
{
 if(x>=0&&x<8&&y>=0&&y<8)
  return 1;
 else
  return 0;
}
int bfs()
{
 node cur,next;
 queue<node>q;
 int x,y,k;
 cur.x=sx;cur.y=sy;cur.step=0;
 q.push(cur);
 num[sx][sy]=1;
 while(!q.empty())
 {
  cur=q.front();
  q.pop();
  if(cur.x==ex&&cur.y==ey)
   return cur.step;
  next.step=cur.step+1;
  for(k=0;k<8;k++)
  {
   next.x=x=cur.x+direction[k][0];
   next.y=y=cur.y+direction[k][1];
   if(judge(x,y)&&!num[x][y])
   {
    num[x][y]=1;
    q.push(next);
   }
  }
 }
 return 0;
}
int main()
{
 int count;
 char a[3],b[3];
 while(cin>>a>>b)
 {
  memset(num,0,sizeof(num));
  sx=a[0]-'a';
  sy=a[1]-'1';
  ex=b[0]-'a';
  ey=b[1]-'1';
  count=bfs();
  cout<<"To get from "<<a<<" to "<<b<<" takes "<<count<<" knight moves."<<endl;
 }
 return 0;
}

原题:

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>


 



0 0
原创粉丝点击