农夫过河问题、用C语言解决。。最好能有详细代码和注释。。。感激不尽,。。。 10
2个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STEP 20
//index: 0 - 狼,1-羊,2-菜,3-农夫,value:0-本岸,1-对岸
int a[MAX_STEP][4];
int b[MAX_STEP];
char *name[] = { "空手", "带狼", "带羊", "带菜" };
void search(int iStep) { int i; if (a[iStep][0] + a[iStep][1] + a[iStep][2] + a[iStep][3] == 4) { for (i = 0; i < iStep; i++) { if (a[i][3] == 0) { printf("%s到对岸\n", name[b[i] + 1]); } else { printf("%s回本岸\n", name[b[i] + 1]); } } printf("\n"); return; } for (i = 0; i < iStep; i++) { if (memcmp(a[i], a[iStep], sizeof(a[i])) == 0) { return; } } if (a[iStep][1] != a[iStep][3] && (a[iStep][2] == a[iStep][1] || a[iStep][0] == a[iStep][1])) { return; } for (i = -1; i <= 2; i++) { b[iStep] = i; memcpy(a[iStep + 1], a[iStep], sizeof(a[iStep + 1])); a[iStep + 1][3] = 1 - a[iStep + 1][3]; if (i == -1) { search(iStep + 1); } else if (a[iStep][i] == a[iStep][3]) { a[iStep + 1][i] = a[iStep + 1][3]; search(iStep + 1); } } } int main() { search(0); return 0; }
#include <stdlib.h>
#include <string.h>
#define MAX_STEP 20
//index: 0 - 狼,1-羊,2-菜,3-农夫,value:0-本岸,1-对岸
int a[MAX_STEP][4];
int b[MAX_STEP];
char *name[] = { "空手", "带狼", "带羊", "带菜" };
void search(int iStep) { int i; if (a[iStep][0] + a[iStep][1] + a[iStep][2] + a[iStep][3] == 4) { for (i = 0; i < iStep; i++) { if (a[i][3] == 0) { printf("%s到对岸\n", name[b[i] + 1]); } else { printf("%s回本岸\n", name[b[i] + 1]); } } printf("\n"); return; } for (i = 0; i < iStep; i++) { if (memcmp(a[i], a[iStep], sizeof(a[i])) == 0) { return; } } if (a[iStep][1] != a[iStep][3] && (a[iStep][2] == a[iStep][1] || a[iStep][0] == a[iStep][1])) { return; } for (i = -1; i <= 2; i++) { b[iStep] = i; memcpy(a[iStep + 1], a[iStep], sizeof(a[iStep + 1])); a[iStep + 1][3] = 1 - a[iStep + 1][3]; if (i == -1) { search(iStep + 1); } else if (a[iStep][i] == a[iStep][3]) { a[iStep + 1][i] = a[iStep + 1][3]; search(iStep + 1); } } } int main() { search(0); return 0; }
追问
哥们儿、你这也太乱了点儿、还没有注释啊。
追答
呃.......
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询