求助!过河卒
查看原帖
求助!过河卒
645451
封禁用户楼主2022/11/23 19:05
#include <iostream>
using namespace std;
int counta=0,bx,by,hx,hy;
int onelist[8][2] = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
void solve(int nowx,int nowy)
{

    if (nowx > bx || nowy > by) // 跳出
    {
        return;
    }

    if (nowx == bx && nowy == by) // 第一种情况
    {
        counta++;
        return;
    }

    int updatex=nowx+1;
    int updatey=nowy; // 向下走更新
    bool flag=false; // 设置找到的标志
    for (int i=0;i<8;i++) // 循环查表
    {
        if (updatex == hx+onelist[i][0] && updatey == hy+onelist[i][1]) // 查到内容
        {
            flag=true; // 设置flag
            break; // 跳出循环
        }
    }
    if (!flag) // 如果没有被找到
    {
        solve(updatex,updatey); // 继续递归
    }



    updatex=nowx,updatey=nowy+1; // 第二轮循环

    flag=false; // 重置flag
    for (int i=0;i<8;i++) // 循环查表
    {
        if (updatex == hx+onelist[i][0] && updatey == hy+onelist[i][1]) // 查到
        {
            flag=true; // 设置flag
            break; // 跳出
        }
    }
    if (!flag) // 如果没有激活flag
    {
        solve(updatex,updatey); // 新一轮递归
    }
    
}
signed main()
{
    cin >> bx >> by >> hx >> hy;
    solve(0,0);
    cout << counta << endl;
    return 0;
}

2022/11/23 19:05
加载中...