#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. 读书笔记:<世界是数字的>

    世界是数字的?第一次听到这个名词不由的感到惊讶,我有听过地球是海洋的,那世界不应该是人类共同拥有的吗或者也可以说世界是大自然的?为什么世界偏偏是数字的呢?我带着满心的疑问去看了那本<世界是数字的 ...

  2. 待验证的一些IOS问题

    1.images.assert中的图片格式必须是png.(jpg格式的图片不行)

  3. 纯CSS3实现3D动画导航,html5 webRTC技术实现免费网页电话拨打

    花了一周的时间完成了 说吧 (免费网页电话) 的前端开发工作,先将技术点总结如下: 免费电话采用最新的html5 webRTC 技术 实现互联网和电信MIS网互通实现网页电话,目前只有 google ...

  4. POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...

  5. DirectShow 最简单的入门 -- 播放一段视频

    #include <dshow.h> #pragma comment(lib,"strmbase.lib") #pragma comment(lib,"qua ...

  6. 解决vsftpd日志时间问题

    解决vsftpd日志时间问题 发布时间:August 29, 2008 分类:Linux <你必须承认土也是一种艺术> <Linux下查看Apache的请求数> 最近发现vsf ...

  7. Lock wait timeout exceeded; try restarting transaction

    What gives this away is the word transaction. It is evident by the statement that the query was atte ...

  8. MySQL Date 函数

    MySQL Date 函数 下面的表格列出了 MySQL 中最重要的内建日期函数: 函数 描述 NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 ...

  9. Spark源码分析环境搭建

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3868718.html 本文主要分享一下如何构建Spark源码分析环境.以前主要使用eclipse来阅读源 ...

  10. 李洪强iOS开发之OC[009] -OC无参方法的声明实现和调用