今天做到了bfs的练习,顺便写下心得。。。

bfs能解决搜索和最短路径的问题。

下面是学习心得:

typedef struct point                //定义点
{
int x;
int y;
}P;
bfs()
{
int level[N]; //记录队列中元素层数,即从起点到该点最短路径距离
P father[NX][NY],queue[N]; //father用来记录父节点,queue用来记录队列
int top=0; //top用来记录队列长度,并指向最后节点
for(int i=0;i<nx;i++)
for(int j=0;j<ny;j++)
{
father[i][j].x=-1; //初始化father
father[i][j].y=-1;
}
queue[top].x=x0; //对起点进行赋值
queue[top].y=y0;
father[x0][y0].x=x0;
father[x0][y0].y=y0;
top++;
for(int i=0;i<top;i++)
{
for(遍历第i个元素的邻接点)
{
if(邻接点没标记) //若邻接点father为-1
{
父节点记为第i个元素;
queue[top]赋值为该邻接点; //把元素接到队列上
level[top]=level[i]+1; //元素层次为父节点层次+1
top++; //队列长度+1
}
if(邻接点满足条件)
{
return ...;
}
}
}
}

上一道训练题:

Description

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of
the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy. 

Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. 



Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input

The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

我的题解(很水,有改进空间):

#include<stdio.h>
#include<string.h>
int dx[8]={-1,-1,-2,-2,1,1,2,2}; //对应的xy变化表
int dy[8]={2,-2,1,-1,2,-2,1,-1};
typedef struct point
{
int x;
int y;
}P;
int bfs(char* s1,char* s2)
{
int x1=(int)(s1[0]-'a'+1); //转化为数字
int x2=(int)(s2[0]-'a'+1);
int y1=(int)(s1[1]-'0');
int y2=(int)(s2[1]-'0');
int level[80],top=0,flag; //top用来记录队列长度,并可用于指向队尾
P father[10][10],queue[80]; //father记录父节点,queue记录队列
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
father[i][j].x=-1; //父节点初始化为-1
father[i][j].y=-1;
}
level[top]=0;
father[x1][y1].x=x1;
father[x1][y1].y=y1;
queue[top].x=x1;
queue[top++].y=y1;
if(x1==x2&&y1==y2)
return 0;
for(int i=0;i<top;i++)
{
flag=0;
for(int j=0;j<8;j++) //标记所有邻接点
{
int tempx,tempy;
tempx=queue[i].x+dx[j];
tempy=queue[i].y+dy[j];
if(tempx<=8&&tempx>0&&tempy<=8&&tempy>0&&father[tempx][tempy].x==-1)
{
father[tempx][tempy].x=queue[i].x;
father[tempx][tempy].y=queue[i].y;
queue[top].x=tempx;
queue[top].y=tempy;
level[top++]=level[i]+1; //level用来记录当前元素层次
}
if(tempx==x2&&tempy==y2)
{
flag=1;
break;
}
}
if(flag==1)
return level[top-1];
}
}
int main()
{
char s1[3],s2[3];
int num;
while(scanf("%s%s",s1,s2)!=EOF)
{
num=bfs(s1,s2); //输入
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,num);
}
return 0;
}

bfs学习的更多相关文章

  1. 广度优先搜索 BFS 学习笔记

    广度优先搜索 BFS 学习笔记 引入 广搜是图论中的基础算法之一,属于一种盲目搜寻方法. 广搜需要使用队列来实现,分以下几步: 将起点插入队尾: 取队首 \(u\),如果 $u\to v $ 有一条路 ...

  2. 简单的BFS学习笔记

    什么是BFS传送门. 今天学习BFS,加油! 先定义个数组: struct Node{ int a=0; int b=0; int step=0; }; int map[5][4]={//地图 0,0 ...

  3. BFS学习 Codeforces 301_div.2_Ice Cave

    C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  4. DFS BFS 学习总结

    DFS 深度优先搜索 基本思路: if(true) 返回 典型例题: 1.马走日(非常典型) #include<iostream> #include<cstring> usin ...

  5. 算法学习之BFS、DFS入门

    算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...

  6. 记录----第一次使用BFS(广度搜索)学习经验总结

    学习经验记录与分享—— 最近在学习中接触到了一种解决最短路径的实用方法----BFS(广度搜索),在这里总结并分享一下第一次学习的经验. 首先第一个要了解的是"queue"(队列函 ...

  7. Day1 BFS算法的学习和训练

    ​ 因为自己的原因,之前没有坚持做算法的相应学习,总是觉得太难就半途而废,真的算是一个遗憾了,所以现在开始,定一个30天入门学习算法计划. ​ 我是根据<算法图解>的顺序进行安排的,自己对 ...

  8. 2019-01-31 Python学习之BFS与DFS实现爬取邮箱

    今天学习了python网络爬虫的简单知识 首先是一个爬取百度的按行读取和一次性爬取 逐行爬取 for line in urllib.request.urlopen("http://www.b ...

  9. 带你学习BFS最小步数模型

    最小步数模型 一.简介 最小步数模型和最短路模型的区别? 最短路模型:某一个点到另一个点的最短距离(坐标与坐标之间) 最小步数模型:不再是点(坐标),而是状态到另一个状态的转变 BFS难点所在(最短路 ...

随机推荐

  1. 开源API测试工具 Hitchhiker v0.7更新 - Schedule的对比diff

    Hitchhiker 是一款开源的支持多人协作的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持脚本定制请求,可以轻松部署到本地,和你的team成员一起协作测试Ap ...

  2. 程序员的自我救赎---10.1:APP版本控制系统

    <前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...

  3. 7.18 DP考试解题报告

    今天的考试真的是天崩地裂,写了的三个题全炸...然而谁叫我弱+不注意细节呢???真的要扇耳光... T1:题意:一段区间的高度为这个区间中高度的最小值,给定n个宽度,求每个宽度的期望高度 40% :算 ...

  4. Java 浮点型与双精度数值比较

    对于双精度与浮点数之间的比较存在潜在的转化

  5. locust 参数,数据详解

    参数    说明-h, –help    查看帮助-H HOST, –host=HOST    指定被测试的主机,采用以格式:http://10.21.32.33–web-host=WEB_HOST  ...

  6. 常用API接口汇总

    下面列举了100多个国内常用API接口,并按照 笔记.出行.词典.电商.地图.电影.即时通讯.开发者网站.快递查询.旅游.社交.视频.天气.团队协作.图片与图像处理.外卖.消息推送.音乐.云.语义识别 ...

  7. C# 给枚举类型增加一个描述特性

    前言 相信很多人对枚举并不陌生,枚举可以很方便和直观的管理一组特定值.如果我们在页面上直接输出我们希望匹配的汉语意思或则其他满足我们需求的语句就更好了,当然,通常小伙伴们都会再页面上if(enum== ...

  8. ubuntu环境下python虚拟环境的安装

    一. 虚拟环境搭建 在开发中安装模块的方法: pip install 模块名称 之前我们安装模块都是直接在物理环境下安装,这种安装方法,后面一次安装的会覆盖掉前面一次安装的.那如果一台机器上面开发多个 ...

  9. Nginx集群之WCF分布式局域网应用

    目录 1       大概思路... 1 2       Nginx集群WCF分布式局域网结构图... 1 3       关于WCF的BasicHttpBinding. 1 4       编写WC ...

  10. [异常解决] 奇巧淫技——VirtualBox中的linux无显示启动,并在win7上远程控制

    楼主是资深技术宅(癖),由于感觉手上的老笔记本太卡,遂狠心买了个性能至强的主机同时配了个投影仪(满足躺着打代码的意淫场景).但是体验了大概一个月发现还是坐着打代码舒服,但是如下图坐着打代码总是要抬头看 ...