#include <iostream>
#include <queue>
using namespace std; int dir[][] = {-,-,-,,,-,,,,-,-,-,,,-,}; struct node
{
int x;
int y;
int step;
node()
{
step = ;
}
}; queue<node> coll; bool mark[][]; node beg;
node end; bool bfs(node p); int main()
{
//freopen("acm.acm","r",stdin);
char s_1[];
char s_2[]; while(cin>>s_1>>s_2)
{
memset(mark,false,sizeof(mark));
beg.x = s_1[] - 'a';
beg.y = s_1[] - '' - ; end.x = s_2[] - 'a';
end.y = s_2[] - '' - ;
coll.push(beg);
mark[beg.x][beg.y] = true;
cout<<"To get from "<<s_1<<" to "<<s_2<<" takes "; while(!coll.empty() && !bfs(coll.front()))
{
coll.pop();
}
while(!coll.empty())
{
coll.pop();
}
cout<<" knight moves."<<endl;
}
} bool bfs(node p)
{
int i;
int j;
if(p.x == end.x && p.y == end.y)
{
cout<<p.step;
return true;
}
int tem_i;
int tem_j;
node tem; for(i = ; i < ; ++ i)
{
tem_i = p.x + dir[i][];
tem_j = p.y + dir[i][];
if(tem_i >= && tem_i < && tem_j >= && tem_j < && !mark[tem_i][tem_j])
{
tem.x = tem_i;
tem.y = tem_j;
tem.step = p.step + ;
mark[tem_i][tem_j] = true;
coll.push(tem);
}
}
return false;
}

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

技术网站地址: vmfor.com

POJ 2243的更多相关文章

  1. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  2. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  3. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

  4. POJ 2243 简单搜索 (DFS BFS A*)

    题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...

  5. POJ 2243 [SDOI2011]染色 | 树链剖分+线段树

    原题链接 肯定是树链剖分的题啦 树剖怎么做可以看我上一篇博客 如果我们已经剖完了: 然后考虑怎么维护重链和查询 用线段树维护的时候当前区间的区间颜色个数应该等于左儿子+右儿子,但是当左儿子的右端点和右 ...

  6. poj2243 bfs

    O - 上一个题的加强版 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     6 ...

  7. BFS、双向BFS和A*

    BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是无用的.我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个 ...

  8. HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分

    树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...

  9. HDU 3966 & POJ 3237 & HYSBZ 2243 & HRBUST 2064 树链剖分

    树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...

随机推荐

  1. 2018.06.29 NOIP模拟 边的处理(分治+dp)

    边的处理(side.cpp) [问题描述] 有一个 n 个点的无向图,给出 m 条边,每条边的信息形如<x,y,c,r><x,y,c,r><x,y,c,r>. 给出 ...

  2. redis学习-事务命令

    multi:开启事务 exec:提交事务 discard:取消事务 1.开启事务之后,每次执行命令之后,都要先进入事务队列中,只有在执行 exec之后才开始执行 2.开启事务之后,每次执行命令之后,都 ...

  3. cmake-file

    file: File manipulation command. file(WRITE filename "message to write"... ) file(APPEND f ...

  4. python中的list和array的不同之处 及转换

    python中的list和array的不同之处list是列表,可以通过索引查找数值,但是不能对整个列表进行数值运算 In [96]: b=[1,2] In [97]: b[1]Out[97]: 2In ...

  5. java 路径、className.class.getResourceAsStream()、ClassLoader.getSystemResourceAsStream() 、FileInputStream

    className.class.getResourceAsStream 用法: 第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件c ...

  6. C++总的const使用说明

    C++总的const使用说明 1. const修饰类成员变量 程序: #include <iostream> using namespace std; class A { public: ...

  7. Ansible配置文件

    官方配置文件文档 Ansible安装完成之后默认配置文件为:/etc/asnible/ansible.cfg Ansible配置文件内容: cat ansible.cfg # config file ...

  8. Python学习-32.Python中os模块的一些方法

    首先肯定是要引入os模块了. import os getcwd方法: print(os.getcwd()) 上面的语句将会输出当前的工作目录,相当于C#中的Environment.CurrentDir ...

  9. 如何获得 Microsoft Push Notification Service(MPNS)的最佳体验

    有很多同学抱怨MPNS的各种问题,其中包括服务超时.返回各种错误代码不知如何处理等等..今天我用一点时间来为大家介绍下如何处理和操作咱们的MPNS. 首先为大家明确一个问题,Microsoft Pus ...

  10. Postgresql 創建觸發器,刪除觸發器和 禁用觸發器

    CREATE OR REPLACE FUNCTION XF_VIP_AFUPD_WX() RETURNS trigger AS $$ DECLARE i_count integer; s_wx_ope ...