这题是第一次当时接触bfs,按照板子写的,好像记得没什么坑。
板子如下
#include <iostream>
#include <queue>
#include <string.h>
#include <stdio.h>
using namespace std;
int step;
char s1[3],s2[3];//C语言用字符数组表示字符串
int go[8][2] = {-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};//
int map[10][10];//标志数组
typedef struct
{
int x,y;
int step;
}pos;
int bfs()
{ int i;
pos next,q0,last;
queue<pos>q;//准备一个结构体的队列Q
pos start;
start.x=s1[0]-'a';
start.y=s1[1]-'1';
start.step=0;
last.x=s2[0]-'a';
last.y=s2[1]-'1';
memset(map,0,sizeof(map));//标志数组每次都初始化
q.push(start);
map[start.x][start.y] = 1;//将结构体(起点,0)入队,将起点标记为已访问
while (!q.empty())
{
q0=q.front();//取出头结点q0
q.pop();
if(q0.x==last.x&&q0.y&#