c语言跳马问题
1个回答
2015-11-14
展开全部
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int x, y;
struct node *nextStep;
}stepNode;
stepNode *q;
stepNode* arrProcess[100];
int count = 0;
int func(int, int, stepNode*);
int main(){
int destX, destY;
int i, flag;
stepNode *p;
printf("请输入要到达点的坐标(0<x<9, 0<y<5):");
scanf("%d%d", &destX, &destY);
flag = func(destX, destY, NULL);
if(flag == 0){
printf("亲,跳不到点(%d,%d)哟!\n", destX, destY);
}else{
printf("共有%d种走法\n", count);
for(i=0; i<count; i++){
printf("第%d中方法:\n", i+1);
for(p=arrProcess[i]; p!=NULL; p=p->nextStep) printf("(%d,%d)\t", p->x, p->y);
printf("\n");
}
}
}
int func(int x, int y, stepNode *p){
int flag1, flag2, flag3, flag4;
if(x<0 || x>8 || y<0 || y>4) return 0;
q = (stepNode*)malloc(sizeof(stepNode));
q->x = x;
q->y = y;
q->nextStep = p;
p = q;
if(x==0 && y==0){
arrProcess[count++] = q;
return 1;
}
flag1 = func(x-1, y-2, p);
flag2 = func(x-2, y-1, p);
flag3 = func(x-2, y+1, p);
flag4 = func(x-1, y+2, p);
if(flag1+flag2+flag3+flag4 == 0){
free(p);
return 0;
}else{
return 1;
}
return 0;
}
供参考
#include<stdlib.h>
typedef struct node{
int x, y;
struct node *nextStep;
}stepNode;
stepNode *q;
stepNode* arrProcess[100];
int count = 0;
int func(int, int, stepNode*);
int main(){
int destX, destY;
int i, flag;
stepNode *p;
printf("请输入要到达点的坐标(0<x<9, 0<y<5):");
scanf("%d%d", &destX, &destY);
flag = func(destX, destY, NULL);
if(flag == 0){
printf("亲,跳不到点(%d,%d)哟!\n", destX, destY);
}else{
printf("共有%d种走法\n", count);
for(i=0; i<count; i++){
printf("第%d中方法:\n", i+1);
for(p=arrProcess[i]; p!=NULL; p=p->nextStep) printf("(%d,%d)\t", p->x, p->y);
printf("\n");
}
}
}
int func(int x, int y, stepNode *p){
int flag1, flag2, flag3, flag4;
if(x<0 || x>8 || y<0 || y>4) return 0;
q = (stepNode*)malloc(sizeof(stepNode));
q->x = x;
q->y = y;
q->nextStep = p;
p = q;
if(x==0 && y==0){
arrProcess[count++] = q;
return 1;
}
flag1 = func(x-1, y-2, p);
flag2 = func(x-2, y-1, p);
flag3 = func(x-2, y+1, p);
flag4 = func(x-1, y+2, p);
if(flag1+flag2+flag3+flag4 == 0){
free(p);
return 0;
}else{
return 1;
}
return 0;
}
供参考
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询