#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std;
#define INF 0x7f
struct node{
int x, y;
int cont;
}; bool inq[8][8];
node cb[9][9];
char s1[3],s2[3];
int sx, sy, ex, ey, dx, dy; int d[8][2]= {{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,1},{-2,-1}}; void init(){
for(int i = 0; i <= 8; i++)
for(int j = 0; j <= 8; j++){
cb[i][j].x = i;
cb[i][j].y = j;
cb[i][j].cont = 0;
}
} int change(char w){
int b;
if(w == 'a') b = 1;
if(w == 'b') b = 2;
if(w == 'c') b = 3;
if(w == 'd') b = 4;
if(w == 'e') b = 5;
if(w == 'f') b = 6;
if(w == 'g') b = 7;
if(w == 'h') b = 8;
return b;
}
bool check(int x, int y){
if(x > 0&&x <= 8&&y > 0&&y <= 8&&!inq[x][y]){
return true;
}
else return false;
} void bfs(){
memset(inq, false, sizeof(inq));
queue<node> q;
init();
q.push(cb[sx][sy]);
node rec;
inq[sx][sy] = true;
while(!q.empty()){
rec = q.front();
q.pop();
if(rec.x == ex&&rec.y == ey) break;
for(int i = 0; i < 8; i++){
dx = rec.x + d[i][0];
dy = rec.y + d[i][1];
if(check(dx,dy)) {
inq[dx][dy] = true;
cb[dx][dy].cont = rec.cont+1;
q.push(cb[dx][dy]);
}
}
}
} int main(){ while(scanf("%s%s",&s1,&s2) != EOF){
sx = change(s1[0]);
ex = change(s2[0]); sy = s1[1] - '0';
ey = s2[1] - '0';
bfs(); printf("To get from %s to %s takes %d knight moves.\n",s1,s2,cb[ex][ey].cont);
}
return 0;
}
水的模板BFS,题意就不多解释了,只要知道“马走日”这一规则就可以了,一定会走到终点。
写完后我才发现我逗比地写了一个change()函数,T^T算了,今天逗比了好多次。
末,多谢小建建耐心地帮忙找bug~~~T^T
作者:u011652573 发表于2014-3-26 16:56:09 原文链接
阅读:43 评论:0 查看评论

[原]poj2243-Knight Moves-水bfs的更多相关文章

  1. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

  2. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  3. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  5. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

  6. ZOJ 1091 (HDU 1372) Knight Moves(BFS)

    Knight Moves Time Limit: 2 Seconds      Memory Limit: 65536 KB A friend of you is doing research on ...

  7. HDU-1372 Knight Moves (BFS)

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

  8. HDOJ/HDU 1372 Knight Moves(经典BFS)

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

  9. HDU1372:Knight Moves(经典BFS题)

    HDU1372:Knight Moves(BFS)   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  10. Knight Moves(BFS,走’日‘字)

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

随机推荐

  1. Poj 1904 King's Quest 强连通分量

    题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...

  2. 将通过find命令找到的文件拷贝到一个新的目录中

    将通过find命令找到的文件拷贝到一个新的目录中 有这样的一个需求,需要将一部分符合条件的文件从一个目录拷贝到另一个目录中,我通过find命令从源目录查找到符合条件的文件然后使用cp命令拷贝到目标目录 ...

  3. fullscreen DXGI DX11

    these days i am fullfilling full screen https://github.com/rufelt/simpled3d11window put this one  in ...

  4. Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service

    起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...

  5. “System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常。{转}

    <connectionStrings> <add name="ConnectionStringName" providerName="System.Da ...

  6. jquery ajax清除缓存的方法

    function cityListChange(cityCode){ //{lon=121.491121, name=上海, province=上海市, telPrefix=021, province ...

  7. 判断一个字符串在至多删除k个字符后是否为回文串

    转自: http://www.careercup.com/question?id=6287528252407808 问题描述: A k-palindrome is a string which tra ...

  8. ****RESTful API 设计最佳实践(APP后端API设计参考典范)

    http://blog.jobbole.com/41233/ 背景 目前互联网上充斥着大量的关于RESTful API(为方便,下文中“RESTful API ”简写为“API”)如何设计的文章,然而 ...

  9. UVA 11481 - Arrange the Numbers 数学

    Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You ca ...

  10. Codeforces Round #337 (Div. 2) A. Pasha and Stick 水题

    A. Pasha and Stick   Pasha has a wooden stick of some positive integer length n. He wants to perform ...