C - 广搜 基础

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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 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 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 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.
代码:

/*
简单BFS,寻找最短路径长度
*/
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const double eps=1e-8;
const double pi=acos(-1.0);
int m[10][10];//用来标记,避免重复,bfs常见剪枝
int ax,ay,bx,by;
struct nod
{
    int x,y;
    int step;
};
int f[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};
queue<nod> q;
int ans;
void  bfs()
{
    nod t;
    while(!q.empty())
    {
        t=q.front();
        m[t.x][t.y]=1;
        q.pop();
        if(t.x==bx&&t.y==by)
        {
            ans=t.step;
           return;
        }
        for(int i=0;i<8;i++)
        {
            int xx=t.x+f[i][0];
            int yy=t.y+f[i][1];
            nod n;
            n.x=xx,n.y=yy,n.step=t.step+1;
            if(xx>=1&&xx<=8&&yy>=1&&yy<=8&&!m[xx][yy])
                q.push(n);
        }
    }
}
int main()
{
    char a,c;
    int b,d;
    while(scanf("%c%d %c%d",&a,&b,&c,&d)!=EOF)//输入的时候需要注意,如果一个一个输入,中间加空格,也可以用两个字符串格式化输入(空格截止)
    {
        getchar();
        memset(m,0,sizeof(m));
         ax=b,ay=a-'a'+1;
         bx=d,by=c-'a'+1;//sb的我重复定义
        //cout<<ax<<" "<<ay<<" "<<bx<<" "<<by<<endl;
        nod k;
        k.x=ax,k.y=ay,k.step=0;
        q.push(k);
        bfs();
        cout<<"To get from "<<a<<b<<" to "<<c<<d<<" takes "<<ans<<" knight moves."<<endl;
            while(!q.empty())
        q.pop();//对于有多组数据情况,一定要记得清空队列
    }
    return 0;
}

最新文章

  1. extracting fasta records from a multi-fasta file based on a list using awk
  2. 后台进程管理supervisor
  3. php原子操作,文件锁flock,数据库事务
  4. C# 中如何判断某个字符串是否为空的方法
  5. 轻量级开源内存数据库SQLite性能测试
  6. PAT 1001
  7. wireshark的使用
  8. hdu1505(dp求最大子矩阵)
  9. java判断字符串是否为乱码
  10. Android .mk文件语法解析
  11. 【linux】---常用命令整理
  12. docker~yml里使用现有网络
  13. Pycharm 常用快捷键与设置
  14. WinForm外包公司 WInform外包项目监控案例展示
  15. nump库的简单函数介绍
  16. HGOI20181030 模拟题解
  17. RX库中的IDisposable对象
  18. TRUNC 截取日期或数字,返回指定的值。
  19. 微信小程序开发-概述
  20. 【kruscal】【最小生成树】【搜索】bzoj1016 [JSOI2008]最小生成树计数

热门文章

  1. 在TCP协议下的数据传送
  2. CREATE DATABASE
  3. Python新手学习基础之函数-return语句与函数调用
  4. Android用gif做启动页
  5. 树莓派设置固定ip
  6. Xcode6中自动布局autolayout和sizeclass的使用
  7. 用windows live writer写博客
  8. Android Activity 生命周期的透彻理解
  9. PowerShell 中使用json对象的性能比较
  10. 在线服装零售商Betabrand获得650万美元风投 - 投资风向 - 创业邦