[原]poj2243-Knight Moves-水bfs
#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
[原]poj2243-Knight Moves-水bfs的更多相关文章
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- uva439 - Knight Moves(BFS求最短路)
题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...
- 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 ...
- HDU-1372 Knight Moves (BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU1372:Knight Moves(经典BFS题)
HDU1372:Knight Moves(BFS) Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- Knight Moves(BFS,走’日‘字)
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- 【BZOJ】【4003】【JLOI2015】城池攻占
可并堆 QAQ改了一下午……最终弃疗求助zyf……居然被秒了QAQ真是弱到不行(zyf太神了Orz) 还是先考虑部分分的做法: 1.$n,m\leq 3000$:可以暴力模拟每个骑士的攻打过程,也可以 ...
- 【BZOJ】【1492】【NOI207】货币兑换Cash
DP/CDQ分治 orz Hzwer copy了下他的代码……结果在while(j<top......)这一句中把一个括号的位置打错了……找了我一个多小时才找到TAT 很神奇……顺便贴下CDQ的 ...
- ios读取通讯录信息
ios读取通讯录信息 (2012-05-22 14:07:11) 标签: ios读取通讯录 it iphone如许app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和Add ...
- C#简单windows服务
因为做后台比较多,所以经常需要写一些后台服务.一般的流程是先创建一个服务项目,加入代码.然后打包一个安装程序或者直接用dos命令安装服务.下面是详细内容: 1. 创建windows服务项目. ...
- Nodejs Express 4.X 中文API 4--- Router篇
相关阅读: Express 4.X API 翻译[一] -- Application篇 Express4.XApi 翻译[二] -- Request篇 Express4.XApi 翻译[三] -- ...
- 哪些问题困扰着我们?DevOps 使用建议
[编者按]随着 DevOps 被欲来越多机构采用,一些共性的问题也暴露出来.近日,Joe Yankel在「Devops Q&A: Frequently Asked Questions」一文中总 ...
- (转)Tips for Optimizing C/C++ Code
本来要自己翻译的,随手搜索了一下,发现五天前已经有人翻译了,我就不重复发明轮子了. 转自:http://blog.csdn.net/yutianzuijin/article/details/26289 ...
- php7 安装 及和php5的共存
http://blog.csdn.net/liuxinmingcode/article/details/50319145 LNMP FastCGI 是一个可伸缩地.高速地在HTTP server和动态 ...
- POJ 1939
#include<iostream> #include<iomanip> #define MAXN 10000 using namespace std; ]; int main ...
- Android中 ListView 详解(二)
本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...