[原]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 ...
随机推荐
- redis.conf配置
# Redis configuration file example # Redis示例配置文件 # Note on units: when memory size is needed, it is ...
- 序列化Color对象
如下: public class Class2 { [XmlIgnore] public Color Color1 { get { return color1; } set { color1 = va ...
- Poj 1032 分类: Translation Mode 2014-04-04 09:09 111人阅读 评论(0) 收藏
Parliament Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16521 Accepted: 6975 Descr ...
- SQL SERVER 导出到Oracle 问题与技巧
工具: 使用SQL SERVER自带的导入, 导出工具 技巧: 一. 使用Oracle Provider for OLE DB(强制推荐) 1. 速度快 2. 要安装Oracle驱动(Oracle ...
- vi/vim使用指北 ---- Introducting the ex Editor
本章介绍ex编辑器,为什么要介绍这样一个新的编辑器呢:其实ex编辑器不能算是一个新的编辑器,vi只是它的visual model,它已经是一个更普遍,基于行的编辑器.ex提供更大机动和范围的编辑命令. ...
- RDLC报表BC4000错误
如果RDLC报表报 BC4000错误,那是因为在矩阵外上面添加了文本框的缘故,导致系统不能读到RDLC报表表体,告诉你报表没有定义: 解决办法: 把表头文字放入矩阵中.
- HDU4836 The Query on the Tree(树状数组&&LCA)
由于智力的问题,百度之星完全lu不动..开场看第一题根据题目给的条件我觉得一定是可以构造出来的,题目给的意思颇有鸽巢原理的感觉,于是觉得开场第一题应该就是智力构造题了,想了半个小时,发现完全想不动,于 ...
- 相对布局RelativeLayout
一. public class RelativeLayout extends ViewGroup java.lang.Object ↳ android.view.View ↳ an ...
- ***codeigniter 2.2 affected_rows()返回值不准确
http://blog.icodeu.com/?p=596 问题描述今天在完成一个项目调用想要检验一下计划插入的数据是否都正常插入了.调用insert_batch()方法插入一百多条数据的时候发现af ...
- ExtJs之Element.select函数
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...