[原]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 ...
随机推荐
- RegExp.exec和String.match深入理解
今天在重新阅读<JavaScript权威指南>的RegExp和String的时候,看到了2个比较容易混淆的函数:RegExp的exec和String的match 这2个函数都是从指定的字符 ...
- C#读书笔记之并行任务
这两天遇到一个多线程任务优化的问题,现在解决了,把心得用例子的形式记下来. 假设有四个任务: 任务1:登陆验证(CheckUser) 任务2:验证成功后从Web服务获取数据(GetDataFromWe ...
- 06.Hibernate实体类生命周期
前言:Session接口是Hibernate向应用程序提供的操作数据库的主要接口,它提供了基本的增删查改方法,而且Session具有一个缓存它是Hibernate的一级缓存.站在持久化层的角度 ...
- Dynamic Programming - Part2
实现如下: public static void main(String[] args) { String squence1 = "ABCBDAB"; String squence ...
- GDI+的常用类
VisualStyleRenderer 提供用于绘制和获取有关 System.Windows.Forms.VisualStyles.VisualStyleElement 的信息的方法. VisualS ...
- swfObject 使用说明
1.Embed your SWF with JavaScript 使用方法 swfobject.embedSWF(swfUrl, id, width, height, version, expres ...
- Openstack os-networks API create network 方法
官方文档在请求方法和地址上有错误: http://api.openstack.org/api-ref.html#ext-os-networks 正确的地址为: /v2/{tenant_id}/os-n ...
- 通过 Mesos、Docker 和 Go,使用 300 行代码创建一个分布式系统
[摘要]虽然 Docker 和 Mesos 已成为不折不扣的 Buzzwords ,但是对于大部分人来说它们仍然是陌生的,下面我们就一起领略 Mesos .Docker 和 Go 配合带来的强大破坏力 ...
- javascript实现KMP算法(没啥实用价值,只供学习)
简单粗暴上代码 KMP的原理我就不讲了,想转过弯儿来不容易,建议大家先学会了怎么推导出next数组规律,然后准备两张纸,大纸上写上一行你要匹配的目标字符串,并分别写出位置编号,小纸上写上一行,也写上位 ...
- HDU 4572 Bottles Arrangement(找规律,仔细读题)
题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1 ...