帮我讲解 #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; }

时间: 2023-09-20 19:11:40 浏览: 34
这是一个经典的“过河问题”,通过回溯算法求解。 代码中,定义了一个二维数组a,用于记录每个状态下,狼、羊、菜、农夫分别在本岸或对岸。另外,还定义了一个一维数组b,用于记录每个状态下,农夫携带的物品。 函数search()用于搜索所有可能的状态,参数iStep表示当前状态的步骤数。当所有物品都到达对岸时,输出所有状态,并返回。 在每个状态中,使用memcmp()函数判断当前状态是否已经存在过,如果存在,则直接返回。同时,还需要满足以下两个条件才能继续搜索: 1. 如果羊和菜在同一侧,且狼也在同一侧,则不符合要求,返回。 2. 如果将某个物品带到对岸后,它和农夫不在同一侧,且羊和菜在同一侧,则不符合要求,返回。 最后,在每个状态中,依次尝试将每个物品带到对岸或带回本岸,然后递归搜索下一步状态。 在main()函数中,调用search(0)开始搜索。
相关问题

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <pthread.h> #include <semaphore.h> void * pthread_odd_function(void * arg); void * pthread_even_function(void * arg); pthread_mutex_t work_mutex; pthread_cond_t work_cond; #define MAX_COUNT 10 int count = 0; int main(int argc, char const *argv[]) { pthread_t pthread_odd; pthread_t pthread_even; pthread_attr_t pthread_attr; int res; res = pthread_attr_init(&pthread_attr);//init pthread attribute,step 1 if (res != 0){ perror("pthread_attr_init failed!"); exit(EXIT_FAILURE); } res = pthread_cond_init(&work_cond,NULL); if (res != 0){ perror("pthread_cond_init failed!"); exit(EXIT_FAILURE); } res = pthread_mutex_init(&work_mutex,NULL); if (res != 0){ perror("pthread_mutex_init failed!"); exit(EXIT_FAILURE); } pthread_attr_setdetachstate(&pthread_attr,PTHREAD_CREATE_DETACHED);//design pthread attribute step 2 res = pthread_create(&pthread_odd,&pthread_attr,pthread_odd_function,NULL);//step 3 if (res != 0){ perror("pthread_create failed!"); exit(EXIT_FAILURE); } res = pthread_create(&pthread_even,&pthread_attr,pthread_even_function,NULL); if (res != 0){ perror("pthread_create failed!"); exit(EXIT_FAILURE); } while(count < MAX_COUNT) ; //wait the two sons threads finished pthread_mutex_destroy(&work_mutex); pthread_cond_destroy(&work_cond); pthread_exit(NULL); return 0; } void * pthread_odd_function(void *arg)//step 4 { pthread_mutex_lock(&work_mutex); while(count < MAX_COUNT){ if (count % 2 == 1){ printf("the odd count is : %d\n", count); ++count; pthread_cond_signal(&work_cond);//in order to release the thread of even } else pthread_cond_wait(&work_cond,&work_mutex);//the pthread is blocked,wait for the condition } pthread_mutex_unlock(&work_mutex); } void * pthread_even_function(void *arg)//step 5 { pthread_mutex_lock(&work_mutex); while(count < MAX_COUNT){ if (count % 2 == 0){ printf("the even count is : %d\n", count); ++count; pthread_cond_signal(&work_cond);//in order to release the thread of odd } else pthread_cond_wait(&work_cond,&work_mutex);//wait the condition satisfied } pthread_mutex_unlock(&work_mutex); }给我讲一下这段代码

这段代码是一个线程同步的例子,它创建了两个线程,一个线程打印出所有奇数,另一个线程打印出所有偶数。具体来说,代码中使用了互斥锁和条件变量来保证线程同步。互斥锁的作用是保护共享资源,只允许一个线程访问该资源。条件变量用于线程之间的通信,当一个线程需要等待某个条件满足时,它会进入阻塞状态,等待其他线程发送信号通知它条件已满足。 在主函数中,先初始化了互斥锁和条件变量,然后创建了两个线程,并使用pthread_attr_setdetachstate函数将线程属性设置为PTHREAD_CREATE_DETACHED,表示线程被创建后马上就进入分离状态,不需要等待其他线程回收资源。接着主线程进入一个循环,等待两个子线程执行完毕。 在子线程中,先获取互斥锁,进入while循环,判断count是否小于最大值,如果是,则进入条件判断。奇数线程打印出奇数并将count加1,然后调用pthread_cond_signal函数发送信号通知偶数线程可以执行了。偶数线程同样进入条件判断,打印出偶数,将count加1,然后调用pthread_cond_signal函数通知奇数线程可以执行了。如果条件不满足,则调用pthread_cond_wait函数将线程阻塞,等待其他线程发送信号通知它条件已满足。最后释放互斥锁,线程结束。 值得注意的是,条件变量的使用必须与互斥锁一起使用,否则可能会出现竞争条件,导致程序出现错误。

c语言代码题 全排列1_dfs 【问题描述】任意输入n个不重复的整数序列,输出序列的全排列。 【输入形式】测试数据有多组,第一行是整数t(0<t<20),代表测试组数。每组测试数据有两行,第一行是整数的个数n(0<n<6),第二行是n个不重复的整数。

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_N 6 int t, n; int nums[MAX_N]; int used[MAX_N]; int ans[MAX_N]; void dfs(int step) { if (step == n) { for (int i = 0; i < n; i++) { printf("%d ", ans[i]); } printf("\n"); return; } for (int i = 0; i < n; i++) { if (!used[i]) { used[i] = 1; ans[step] = nums[i]; dfs(step + 1); used[i] = 0; } } } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &nums[i]); } memset(used, 0, sizeof(used)); dfs(0); } return 0; }

相关推荐

最新推荐

recommend-type

WX小程序源码小游戏类

WX小程序源码小游戏类提取方式是百度网盘分享地址
recommend-type

grpcio-1.47.2-cp310-cp310-musllinux_1_1_x86_64.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

cryptography-42.0.3-cp37-abi3-musllinux_1_1_x86_64.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

2021131528+谢镕汕.zip

2021131528+谢镕汕.zip
recommend-type

sja1301.i386.tar.gz

SQLyong 各个版本,免费下载 SQLyog是业界著名的Webyog公司出品的一款简洁高效、功能强大的图形化MySQL数据库管理工具。使用SQLyog可以快速直观地让您从世界的任何角落通过网络来维护远端的MySQL数据库。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。