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

  1. e2 e4
  2. a1 b2
  3. b2 c3
  4. a1 h8
  5. a1 h7
  6. h8 a1
  7. b1 c3
  8. 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;
}

hdu1372 BFS求最短路径长度的更多相关文章

  1. 搜索(BFS)---计算在网格中从原点到特定点的最短路径长度

    计算在网格中从原点到特定点的最短路径长度 [[1,1,0,1], [1,0,1,0], [1,1,1,1], [1,0,1,1]] 题目描述: 1表示可以经过某个地方,求解从(0,0)位置到(tr,t ...

  2. CodeForces - 987D Fair (BFS求最短路)

    题意:有N个城市,M条双向道路连接两个城市,整个图保证连通.有K种物品,但每个城市只有一种,现在它们都需要S种物品来举办展览,可以去其他城市获取该城市的物品,花费是两城市之间的最短路径长度.求每个城市 ...

  3. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

  4. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  5. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  6. 6.4.2 用BFS求最短路

    前面的篇幅占了太多,再次新开一章,讲述BFS求最短路的问题 注意此时DFS就没有BFS好用了,因为DFS更适合求全部解,而BFS适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...

  7. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  8. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  9. 利用BFS求最短路

    利用BFS求图的最短路, POJ3984 #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<string.h& ...

随机推荐

  1. C语言 创建一个 txt 文件 bin输入字符 保存文件在工作文件夹里

    int main(void) { char s[70]; FILE *fp; fp=fopen("123.txt","r"); if((fp=fopen(&qu ...

  2. WebVR

    WebVR 主要面向Web前端工程师,需要一定Javascript及three.js基础:本文主要分享内容为基于three.js开发WebVR思路及碰到的问题:有兴趣的同学,欢迎跟帖讨论. 目录:一. ...

  3. Robot FrameWork 教程链接

    1.  Robot Framework 教程: http://cgmblog.sinaapp.com/html/category/robot-framework 2.  Robot Framework ...

  4. BZOJ 2115: [Wc2011] Xor

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 2794  Solved: 1184 [Submit][Stat ...

  5. POJ 2540 Hotter Colder

    http://poj.org/problem?id=2540 题意:给你每次行走的路径,而且告诉你每次离一个点光源是远了还是近了,要求每次光源可能存在的位置的面积. 思路:如果出现"same ...

  6. POJ3683 Falsita

    http://poj.org/problem?id=3683 思路:2-SAT,输出任意一组方案,O(m+n) #include<cstdio> #include<iostream& ...

  7. leetcode_Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  8. Powershell 条件操作符

    Powershell 中的比较运算符-eq :等于-ne :不等于-gt :大于-ge :大于等于-lt :小于-le :小于等于-contains :包含-notcontains :不包含 进行比较 ...

  9. penetration testers渗透测试,hack,vnc,nat,

    penetration testers渗透测试,hack,vnc,nat,

  10. Mac系统升级到10.9(mavericks)时安装php扩展问题解决(转)

    问题一: 执行执行 phpize 报错: grep: /usr/include/php/main/php.h: No such file or directory grep: /usr/include ...