C++实现棋盘路径查询功能(搜索和回溯算法)

今天闲着没事刷某音,看到如下问题。

 

 想着最近学搜索和回溯算法,可以试试做一做,于是便有了以下代码。

#include <iostream>
using namespace std;
const int need = 3; //需要的路径数目

int p[30][30];                                    //棋盘通行点
int path[1000][2];                                //路径点
int no[][2] = {{0, 1}, {0, 4}};                   //不能走的点
int nt[][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; //可走的位移
int tx = 6, ty = 6, nocount = 2;                  //棋盘宽高,不能走的点的个数
int total = 0;                                    //总路径数
int start[2] = {0, 0};                            //启动点


void onfoundRoute()//查询到路线时调用的方法
{
    for (size_t i = 0; i < tx * ty - nocount - 1; i++)
    {
        cout << "x:" << path[i][1] << " y:" << path[i][0] << "->";
    }
    cout << "done!" << endl<<endl;
}
void dfs(int step, int x, int y)
{
    //所有找到的路线数量与需要的数量一致时完成
    if (total == need)
        return;

    //走的步数遍布所有可走点时完成
    if (step == tx * ty - nocount - 1)
    {
        onfoundRoute();
        total++;
        return;
    }
    int nx, ny;
    for (size_t i = 0; i < 4; i++)
    {

        //产生下一步的点
        nx = x + nt[i][1];
        ny = y + nt[i][0];
        //判断下一步的点是否在棋盘内
        if (nx < 0 || nx >= tx)
            continue;
        if (ny < 0 || ny >= ty)
            continue;
        //判断下一步的点是否可走
        if (p[ny][nx] == 1)
            continue;
        p[ny][nx] = 1;//记录下一步点已走

        //记录路径
        path[step + 1][0] = ny;
        path[step + 1][1] = nx;
        dfs(step + 1, nx, ny);//进入下一步点的进一步探索
        //下一步点推测完后恢复当前点,为接下来的预测点做好准备
        p[ny][nx] = 0;
        path[step + 1][0] = -1;
        path[step + 1][1] = -1;
    }
}
//重置棋盘
void reset()
{
    memset(p, 0, sizeof(p));
    memset(path, -1, sizeof(path));
    for (int i = 0; i < nocount; i++)
    {
        p[no[i][0]][no[i][1]] = 1;
    }
    total = 0;
}

int main()
{
    for (size_t i = 0; i < ty; i++)
    {
        for (size_t j = 0; j < tx; j++)
        {
            reset();
            if (p[i][j] == 1)
                continue;
            cout << "start as x:" << j << " y:" << i << endl;
            start[0] = i;
            start[1] = j;
            path[0][0] = start[0];
            path[0][1] = start[1];
            p[i][j] = 1;
            dfs(0, start[1], start[0]);
            if (total == 0)
            {
                cout << "can not find a route!" << endl;
            }
            if (total == need)
                break;
        }
        if (total == need)
            break;
    }

    return 0;
}

运行结果如下:

start as x:0 y:0
x:0 y:0->x:0 y:1->x:0 y:2->x:0 y:3->x:0 y:4->x:0 y:5->x:1 y:5->x:1 y:4->x:1 y:3->x:1 y:2->x:1 y:1->x:2 y:1->x:2 y:0->x:3 y:0->x:3 y:1->x:3 y:2->x:2 y:2->x:2 y:3->x:2 y:4->x:2 y:5->x:3 y:5->x:3 y:4->x:3 y:3->x:4 y:3->x:4 y:4->x:4 y:5->x:5 y:5->x:5 y:4->x:5 y:3->x:5 y:2->x:4 y:2->x:4 y:1->x:5 y:1->done!

x:0 y:0->x:0 y:1->x:0 y:2->x:0 y:3->x:0 y:4->x:0 y:5->x:1 y:5->x:1 y:4->x:1 y:3->x:1 y:2->x:1 y:1->x:2 y:1->x:2 y:0->x:3 y:0->x:3 y:1->x:3 y:2->x:2 y:2->x:2 y:3->x:2 y:4->x:2 y:5->x:3 y:5->x:4 y:5->x:5 y:5->x:5 y:4->x:4 y:4->x:3 y:4->x:3 y:3->x:4 y:3->x:5 y:3->x:5 y:2->x:4 y:2->x:4 y:1->x:5 y:1->done!

x:0 y:0->x:0 y:1->x:0 y:2->x:0 y:3->x:0 y:4->x:0 y:5->x:1 y:5->x:1 y:4->x:1 y:3->x:1 y:2->x:1 y:1->x:2 y:1->x:2 y:0->x:3 y:0->x:3 y:1->x:3 y:2->x:2 y:2->x:2 y:3->x:3 y:3->x:3 y:4->x:2 y:4->x:2 y:5->x:3 y:5->x:4 y:5->x:5 y:5->x:5 y:4->x:4 y:4->x:4 y:3->x:5 y:3->x:5 y:2->x:4 y:2->x:4 y:1->x:5 y:1->done!

其中一条路径的示意图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
马走日问题是一个经典的搜索回溯算法问题,其目的是求出马从棋盘的某一位置出发,恰好行走k步,经过所有棋盘格子的路径数目。 C++实现马走日问题的搜索回溯算法步骤如下: 1. 首先定义棋盘的大小和马的起始位置。 2. 定义一个二维数组visited来记录每个位置是否被走过。 3. 定义一个数组dx和dy来记录马在x和y方向上的可行移动距离。 4. 定义一个计数器count来记录走过的路径数。 5. 定义一个递归函数dfs,其中参数x和y表示当前马所在的位置,step表示已经走过的步数。 6. 在dfs函数中,首先检查当前位置是否越界或者已经走过,如果是,则直接返回。 7. 如果当前已经走过了k步,则count加1,表示找到了一条符合要求的路径。 8. 否则,从当前位置开始尝试所有可行的移动方式,即往上下左右和斜向上下左右八个方向移动。 9. 对于每个可行的移动,将当前位置标记为已经走过,并递归调用dfs函数。 10. 在递归调用结束后,将当前位置标记为未走过,以便进行下一次搜索C++代码实现如下: ```c++ #include <iostream> #include <cstring> using namespace std; const int N = 10; int n, m, k; int cnt; int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2}; int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1}; bool visited[N][N]; void dfs(int x, int y, int step) { if (step == k) { cnt++; return; } for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny]) { visited[nx][ny] = true; dfs(nx, ny, step + 1); visited[nx][ny] = false; } } } int main() { cin >> n >> m >> k; int x, y; cin >> x >> y; visited[x][y] = true; dfs(x, y, 0); cout << cnt << endl; return 0; } ``` 这里我们使用了搜索回溯算法的思想,在递归过程中不断尝试所有可能的移动方式,直到找到一条符合要求的路径。同时,我们使用了一个visited数组来记录每个位置是否已经被走过,以避免重复搜索

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值