1091 zoj Knight Moves的BFS算法和DFS

简介: 1091 zoj Knight Moves的BFS算法和DFS

Knight Moves的BFS算法和DFS

可以理解成八叉树

进行层次遍历

#include <iostream>
#include<string>
#include<queue>
using namespace std;
#define MAXSTEP 0x7fffffff;
int step=MAXSTEP;//用于DFS中的计算步数
int visit[8][8];//记录点的访问
int rules[8][2] = { {-1,2},{-2,1},{-2,-1},{-1,-2},{1,2},{2,1},{2,-1},{1,-2} };//从左上到左下,再从右上到右下
struct point{
  int x;//x坐标
  int y;//y坐标
  int step;//点所在八叉树的层数,也是起始点到当前点的步数
};
bool operator ==(const point& l, const point& r) {
  return (l.x == r.x && l.y == r.y) ? true : false;
}
bool isLegal(point p) {//判断当前点是否合法
  return p.x >= 0 && p.x <= 7 && p.y >= 0 && p.y <= 7 ? true : false;
}
void BFS(point start, point end) {
  start.step = 0;//一开始的点就是起始点,起始点到自己的步数当然是0
  //到终点了
  if (start == end) {
    if (start.step < step) {
      step = start.step;
    }
    return;
  }
  //不合法
  if (!isLegal(start) || !isLegal(end)) return;
  queue<point> q; 
  q.push(start);
  while (!q.empty()) {
    point current = q.front();
    q.pop();
    if (!isLegal(current)) continue;
    if (current == end) {
      if (current.step < step) {
        step = current.step;
      }
      return;
    }
    for (int n = 0; n < 8; n++) {
      point newStart;
      newStart.x = current.x + rules[n][0];
      newStart.y = current.y + rules[n][1];
      newStart.step = current.step + 1;
      q.push(newStart);
    }
  }
}
int main()
{
  for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; j++) {
      visit[i][j] = 0;
    }
   }
  string a, b;
  point origin, fin;
  while (cin >> a >> b) {
    origin.x = a[1] - '1';
    origin.y = a[0] - 'a';
    fin.x = b[1] - '0' - 1;
    fin.y = b[0] - 'a';
    BFS(origin, fin);
    cout << "To get from " << a << " to " << b << " takes " << step << " knight moves." << endl;
    step = MAXSTEP;
  }
}

DFS

#include<cstdio>
#include<iostream>
using namespace std;
int maps[8][8] = {0};
static int dir[2][8] = {{-1, -1, +1, +1, -2, -2, +2, +2},
                        {-2, +2, -2, +2, -1, +1, -1, +1}};
int x2, y2;
int minClue;
bool isThere(int x, int y){
    if(x < 0 || x > 7){
        return false;
    }
    if(y < 0 || y > 7){
        return false;
    }
    return true;
}
int DFS(int x, int y, int l)
{
    if(x==x2&&y==y2 && minClue > l){
            //printf("x:%d y:%d
", x2, y2);
        minClue = l;
        return 0;
    }
    if(maps[x][y] > l && isThere(x, y)){
        maps[x][y] = l;
        for(int i=0; i<8; i++){
            DFS(x+dir[0][i], y+dir[1][i], l+1);
        }
    }
}
void init()
{
    minClue = 10000;
    for(int i=0; i<8; i++){
        for(int j=0; j<8; j++){
            maps[i][j]=100;
        }
    }
    return;
}
int main()
{
    char s1[5];
    char s2[5];
    int x1,y1;
    while(cin>>s1>>s2){
        init();
        x1 = (int)(s1[0]-'a');
        y1 = (int)(s1[1]-'1');
        x2 = (int)(s2[0]-'a');
        y2 = (int)(s2[1]-'1');
        DFS(x1, y1, 0);
        printf("To get from %s to %s takes %d knight moves.
",s1,s2, minClue);
    }
    return 0;
}
相关文章
|
3月前
|
算法
Hierholzer算法dfs找欧拉回路模板
Hierholzer算法dfs找欧拉回路模板
36 0
|
5月前
|
算法 Python
Python算法——深度优先搜索(DFS)
Python算法——深度优先搜索(DFS)
261 8
|
11天前
|
算法 前端开发
前端算法 岛屿的最大面积 DFS(深度优先搜索)
前端算法 岛屿的最大面积 DFS(深度优先搜索)
11 0
|
3月前
|
存储 算法 定位技术
图论算法dijkstra dfs bfs以及动态规划
图论算法dijkstra dfs bfs以及动态规划
34 0
|
4月前
|
算法 Python
Python 数据结构和算法:解释深度优先搜索(DFS)和广度优先搜索(BFS)。
Python 数据结构和算法:解释深度优先搜索(DFS)和广度优先搜索(BFS)。
|
4月前
|
存储 算法 程序员
【算法训练-搜索算法 一】【DFS网格搜索框架】岛屿数量、岛屿的最大面积、岛屿的周长
【算法训练-搜索算法 一】【DFS网格搜索框架】岛屿数量、岛屿的最大面积、岛屿的周长
74 0
|
5月前
|
算法
算法学习--DFS
算法学习--DFS
|
6月前
|
机器学习/深度学习 算法 测试技术
C++算法:深度优先搜索(BFS)的原理和实现
C++算法:深度优先搜索(BFS)的原理和实现
|
6月前
|
存储 算法 容器
精选算法题(1)——枚举符合要求的算术表达式(DFS、回溯法)
精选算法题(1)——枚举符合要求的算术表达式(DFS、回溯法)
|
8月前
|
算法 前端开发 C++
宽度优先搜索算法(BFS)详解(超级详细讲解,附有大图)
宽度优先搜索算法(BFS)详解(超级详细讲解,附有大图)
546 1