问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求助,用C语言并用回溯算法编写的有关蛇吃苹果走迷宫的程序,题目如下:

发布网友 发布时间:2022-10-08 05:05

我来回答

1个回答

热心网友 时间:2023-11-04 06:04

/*
* snake
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DEBUG 0

#define printpos() \
printf("File: %s\tLine: %d\n", __FILE__, __LINE__); fflush(stdout);

#define CALLOC(ARRAY, NUM, TYPE)\
ARRAY = (TYPE*) calloc(NUM, sizeof(TYPE));\
if (ARRAY == NULL) {\
printf("File: %s, Line: %d: ", __FILE__, __LINE__); \
printf("Allocating memory failed.\n");\
exit(0);\
}

#define REALLOC(ARRAY, NUM, TYPE)\
ARRAY = (TYPE*) realloc(ARRAY, (NUM)*sizeof(TYPE));\
if (ARRAY == NULL) {\
printf("File: %s, Line: %d: ", __FILE__, __LINE__); \
printf("Allocating memory failed.\n");\
exit(0);\
}

const int START = -1;
const int HOME = -2;
#if DEBUG
int m=4, n=4;
int a[4][4] = {{7, 0, 4, 18}, {4, 0, 1, 1}, {15, 7, 11, -1}, {0, 12, -2, 0}};
#else
int m=0, n=0;
int **a=NULL;
#endif

struct pos {
int x;
int y;
};
typedef struct pos pos;

struct node {
pos p;
int mv;
int n;
};
typedef struct node node;

const pos mv[4] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

/*
* get m, n, a and check them
*/
int setup()
{
int nstart=0, nhome=0;
int i, j;

#if DEBUG
#else
//get the dimension of the matrix and allocate memory
printf("Please input the number of rows of the matrix: ");
scanf("%d", &m);
if (m<=0) {
printf("Number of rows must be greater than 0.\n");
exit(0);
}
a = (int**) calloc(m, sizeof(int*));
if (a == NULL) {
printf("Allocate memory failed.\n");
exit(1);
}

printf("Please input the number of columns of the matrix: ");
scanf("%d", &n);
if (n<=0) {
printf("Number of columns must be greater than 0.\n");
exit(0);
}
for (i=0; i<m; i++) {
a[i] = (int*) calloc(n, sizeof(int));
if (a[i] == NULL) {
printf("Allocate memory failed.\n");
exit(1);
}
}

//get the matrix
printf("Please input the matrix, entities seperated by blank:\n");
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
scanf("%d", &a[i][j]);
}
}
#endif

//check the matrix
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
if (a[i][j] == START) {
nstart++;
if (nstart > 1) {
printf("More than 1 starting point.\n");
exit(0);
}
} else if (a[i][j] == HOME) {
nhome++;
if (nhome > 1) {
printf("More than 1 home point.\n");
exit(0);
}
} else if (a[i][j] < 0) {
printf("a[%d][%d] = %d has no meaning.\n", i, j, a[i][j]);
exit(0);
}
}
}
if (nstart == 0) {
printf("No starting point.\n");
exit(0);
}
if (nhome == 0) {
printf("No home point.\n");
exit(0);
}

//output the matrix
printf("The matrix (%d X %d):\n", m, n);
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}

return 0;
}

int solve(node** optpath)
{
pos dest;//destinating point
node* curpath = NULL;//current path
node** sol = NULL;
int nsol = 0;
int nsteps;//number of steps
int i, j;
int curmv = -1;
int sucmv = 0;//sucessfully moved
int sum;
int maxsum=0;

//setup starting point
for (i=0; i<m; i++) {
for (j=0; j<n; j++) {
if (a[i][j] == START) {
dest.x = i;
dest.y = j;
break;
}
}
}

nsteps = 0;
CALLOC(curpath, nsteps+1, node);
curpath[0].p.x = dest.x;
curpath[0].p.y = dest.y;
curpath[0].mv = -1;
a[dest.x][dest.y] = 0;

curmv = 0;
while (1) {
for (sucmv=0, curmv=curpath[nsteps].mv+1; curmv<4; curmv++) {
dest.x = curpath[nsteps].p.x + mv[curmv].x;
dest.y = curpath[nsteps].p.y + mv[curmv].y;
if (dest.x < 0 || dest.x >= m || dest.y < 0 || dest.y >= n) {
curpath[nsteps].mv = curmv;
continue;
}
if (a[dest.x][dest.y] == 0) {
curpath[nsteps].mv = curmv;
continue;
}
nsteps++;
REALLOC(curpath, nsteps+1, node);
curpath[nsteps].p.x = dest.x;
curpath[nsteps].p.y = dest.y;
curpath[nsteps-1].mv = curmv;
curpath[nsteps].mv = -1;
curpath[nsteps].n = a[dest.x][dest.y];
a[dest.x][dest.y] = 0;
sucmv = 1;
break;
}
if (sucmv) {
if (curpath[nsteps].n == HOME) {
nsol++;
REALLOC(sol, nsol, node*);
CALLOC(sol[nsol-1], nsteps+1, node);
memcpy(sol[nsol-1], curpath, (nsteps+1)*sizeof(node));
//back
a[curpath[nsteps].p.x][curpath[nsteps].p.y] = curpath[nsteps].n;
nsteps--;
if (nsteps == -1 && curpath[0].mv == 3)break;
REALLOC(curpath, nsteps+1, node);
} else {
continue;
}
} else {
a[curpath[nsteps].p.x][curpath[nsteps].p.y] = curpath[nsteps].n;
nsteps--;
if (nsteps == -1 && curpath[0].mv == 3)break;
REALLOC(curpath, nsteps+1, node);
}
}

//printf("number of solutions: %d\n", nsol);
for (maxsum=0, i=0; i<nsol; i++) {
//printf("Solution %d \n", i);
//printf("\tPath: ");
sum = -1*HOME;
for (j=0; ; j++) {
//printf("(%d, %d)\t", sol[i][j].p.x, sol[i][j].p.y);
sum += sol[i][j].n;
if (sol[i][j].mv == -1) break;
}
//printf("\n\tSum of apples: %d\n", sum);
if (sum>maxsum) {
maxsum = sum;
*optpath = sol[i];
}
}

return 0;
}

int output(node* path)
{
int i=0, sum=0;

printf("Path: ");
sum = -1*HOME;
for (i=0; ; i++) {
printf("(%d, %d)\t", path[i].p.x, path[i].p.y);
sum += path[i].n;
if (path[i].mv == -1) break;
}
printf("\nSum of apples: %d\n", sum);

return 0;
}

int main()
{
node* path=NULL;

setup();
solve(&path);
output(path);

return 0;
}

编译、链接、运行程序,输入与输出如下:
:!gcc -Wall tmp.c -o tmp
:! ./tmp
Please input the number of rows of the matrix: 5
Please input the number of columns of the matrix: 5
Please input the matrix, entities seperated by blank:
1 7 9 7 0
-2 8 10 8 7
0 10 8 2 -1
4 3 0 7 0 9
1 2 5 1 0 7
The matrix (5 X 5):
1 7 9 7 0
-2 8 10 8 7
0 10 8 2 -1
4 3 0 7 0
9 1 2 5 1
Path: (2, 4) (1, 4) (1, 3) (0, 3) (0, 2) (1, 2) (2, 2) (2, 3) (3, 3) (4, 3) (4, 2) (4, 1) (4, 0) (3, 0) (3, 1) (2, 1) (1, 1) (0, 1) (0, 0) (1, 0)
Sum of apples: 108

:!gcc -Wall tmp.c -o tmp
:! ./tmp
Please input the number of rows of the matrix: 4
Please input the number of columns of the matrix: 4
Please input the matrix, entities seperated by blank:
7 0 4 18
4 0 1 1
15 7 11 -1
0 12 -2 0
The matrix (4 X 4):
70418
4011
15711-1
012-20
Path: (2, 3)(1, 3)(0, 3)(0, 2)(1, 2)(2, 2)(2, 1)(3, 1)(3, 2)
Sum of apples: 54
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
女生多大后可以不在长身高? 如何不用软件把手机投屏到电脑上手机屏幕怎样投放到电脑上 战时拒绝、故意延误军事订货罪既遂的处罚? 战时故意延误军事订货罪处罚标准 名师1+1导读方案:汤姆·索亚历险记目录 三星sm-g7200打开微信慢,无法正常收看,网速不慢。 笔记本电脑如何调亮屏幕亮度 大伙说说洗衣机要不要带烘干好 热烘干洗衣机怎么样 ef英语哪个好 这首歌谁能翻译成英文?求英文高手来。不要用翻译机!! Getting Over You 歌词 英语翻译问题 会加分 我们的爱就像个圆圈没有开头也没有结尾用英文怎么说 用什么东西可以把硬纸板粘在墙上且不掉落 压缩纸板用什么粘椄 喇叭:什么是音圈?还有纸盆叫振膜吗? 华为手机游戏的账号怎么换到AiAO手机? 马姓的姓氏名望 劳动用工形式有哪几种? 如今在海上看星星,巴金回忆起以前,有什么样的情感? 车牌识别系统车牌录入的操作步骤 债券基金有风险吗,债券基金买如何判断和选购? 最能触动你的内心深处的歌曲? 内心深处那首歌七年级作文 内心深处那首歌600字作文 建设一个国际货运代理平台需要哪些技术支持 狗狗为什么老是喜欢咬鸡原因是什么 狗为什么总咬鸡原因是什么 冯绍峰,坦言对倪妮动过真情,6个绯闻女友个个娇美如花 安徽省亳州市利辛县缴纳五险一金怎么缴 对学校的寄语 美图手机返厂换新机为什么要收费? 我对象想给我买一套定制的美发剪刀,可以刻字的,求四个字表达爱意的短语两对,一定要四个字的短语! 谢谢您详细的说明,让我了解了一些美发和理发的知识。 美发师在手上纹身有什么好处有什么坏处 汽车贴乱入受付中什么意思 乱入什么意思 乱入解释 i9300手机内存如何格式化 手机9300如何格式化? 三星i9300怎么格式化,求解 三星i9300手机格式化 大兴机场部分航站楼暂停运营,各地机场的防疫政策是怎样的? 2022年win11现在建议升级吗 未出账费用包括套餐费吗 为什么会产生地震 白色橄榄树沈蓓是谁 白色橄榄树沈蓓阿瓒为什么分手 全国民族大学排名 饿了么怎么核销消费券