马走日问题1)问题描述。如下图所示:马从(0,0)出发,只能往右(右上或右下)跳,从(0,0)点到(8,4)点,这个区域内有多少种不同的路径,并打印出各种路径。
2条回答 默认 最新
CSDN专家-深度学习进阶 2022-06-21 09:47关注一共37种

#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"); }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用