用c++解决下面这个问题。

马走日问题1)问题描述。如下图所示:马从(0,0)出发,只能往右(右上或右下)跳,从(0,0)点到(8,4)点,这个区域内有多少种不同的路径,并打印出各种路径。

一共37种

img


#include <stdio.h>

int stackrow[100];
int stackcol[100];
int ps = 0;

int count = 0;

void horse(int row, int col);
void push(int row, int col);
void pop();
void output_result();

int main(void)
{
    horse(0, 0);

    return 0;
}

/* 函数功能:当前马调到row行col列
 *
 * 参数:row--行,col--列
 */
void horse(int row, int col) {
    push(row, col);
    if(row == 8 && col == 4)
        output_result();
    if(row < 0 ||col < 0|| row > 8 || col > 4) {
        ;
    } else {
        horse(row +2, col + 1);
        horse(row +1, col + 2);
        horse(row + 1, col -2);
        horse(row + 2, col - 1);

    }
    pop();
}

void push(int row, int col) {
    stackrow[ps] = row;
    stackcol[ps] = col;
    ps++;
}

void pop() {
    ps--;
}

void output_result() {
    count++;
    int i;
    printf("result %d\n", count);
    for(i=0; i<ps-1; i++) {
        printf("(%d,%d)->",  stackrow[i], stackcol[i]);
    }
    printf("(%d,%d)",  stackrow[ps-1], stackcol[ps-1]);
    printf("\n");

}


您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632