博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Knight Moves
阅读量:4658 次
发布时间:2019-06-09

本文共 2388 字,大约阅读时间需要 7 分钟。

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.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
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.

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.

Output

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

Sample Input

e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.To get from a1 to b2 takes 4 knight moves.To get from b2 to c3 takes 2 knight moves.To get from a1 to h8 takes 6 knight moves.To get from a1 to h7 takes 5 knight moves.To get from h8 to a1 takes 6 knight moves.To get from b1 to c3 takes 1 knight moves.To get from f6 to f6 takes 0 knight moves.
#include<stdio.h>
#include<string.h>
struct lmx{
 int x;
 int y;
 int dep;
};
int c[8]={-1,-2,-2,-1,1,2,2,1};
int d[8]={-2,-1,1,2,2,1,-1,-2};
int x1,y1,x2,y2;
lmx ss[10000];
bool gcd(int x,int y)
{
 return (x>=0&&x<8&&y>=0&&y<8);
}
int flag[20][20];
int  bfs()
{
 lmx s,q;
    int head=0,rear=0,i;
 s.x=x1;s.y=y1;
 s.dep=0;
 ss[rear++]=s;
 flag[x1][y1]=1;
 while(head<rear)
 {
  lmx sss=ss[head];
  if(sss.x==x2&&sss.y==y2)  return sss.dep;
  else
  {
   for(i=0;i<8;i++)
   {
      int xx=sss.x+c[i];
      int yy=sss.y+d[i];
     if(gcd(xx,yy)&&flag[xx][yy]==0)
     {
                  q.x=xx;
      q.y=yy;
      q.dep=sss.dep+1;
      ss[rear++]=q;
      flag[xx][yy]=1;
     }
   }
  }
  head++;
 }
}
int main()
{
char a,b,c,d;
while(scanf("%c%c %c%c",&a,&b,&c,&d)!=EOF)
{
 getchar();
 memset(flag,0,sizeof(flag));
 x1=a-'a';
 y1=b-'1';
 x2=c-'a';
 y2=d-'1';
 printf("To get from %c%c to %c%c takes %d knight moves.\n",a,b,c,d,bfs());
}
 return 0;
}

转载于:https://www.cnblogs.com/ffhuguang/archive/2013/03/06/2947192.html

你可能感兴趣的文章
LoadRunner常用术语
查看>>
关于jedis2.4以上版本的连接池配置,及工具类
查看>>
记忆讲师石伟华微信公众号2017所有文章汇总(待更新)
查看>>
FactoryBean
查看>>
Coolite动态加载CheckboxGroup,无法在后台中获取
查看>>
C3P0连接池工具类使用
查看>>
SVN常用命令备注
查看>>
孩子教育
查看>>
解决Cacti监控图像断断续续问题
查看>>
结构体的传参理解成员的存储方式
查看>>
python 进程与线程(理论部分)
查看>>
什么是API
查看>>
强名称程序集(strong name assembly)——为程序集赋予强名称
查看>>
1028. List Sorting (25)
查看>>
BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
查看>>
ubuntu 重启命令,ubuntu 重启网卡方法
查看>>
Linux的学习:
查看>>
JavaScript中的原型继承原理
查看>>
Python logger模块
查看>>
jquery控制css的display(控制元素的显示与隐藏)
查看>>