帮我讲解 #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 15:11:40 浏览: 103
这是一个经典的“过河问题”,通过回溯算法求解。
代码中,定义了一个二维数组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;
}
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)