首页 > 试题广场 >

农夫、羊、菜和狼的故事

[编程题]农夫、羊、菜和狼的故事
  • 热度指数:4200 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一个农夫带一只羊、一筐菜和一只狼过河。如果没有农夫看管,则狼要吃羊,羊要吃菜。但是船很小,只够农夫带一样东西过河。问农夫该如何解此难题?

输入描述:
题目没有任何输入。


输出描述:
题目可能有多种解决方法,请输出步骤最少的任意一种解决方法,
按顺序输出农夫想把羊、菜、狼全部运过河需要哪几个步骤。
如果需要将羊带过河去则输出一行“sheep_go”。
如果需要将羊带回来则输出一行“sheep_come”。
如果需要将菜带过河去则输出一行“vegetable_go”。
如果需要将菜带回来则输出一行“vegetable_come”。
如果需要将狼带过河去则输出一行“wolf_go”。
如果需要将狼带回来则输出一行“wolf_come”。
如果需要空手返回则输出一行“nothing_come”。
如果需要空手过河则输出一行“nothing_go”。
输出任意一种可行的最简方案后,请在最后输出一行“succeed”。
示例1

输入

输出

按题目要求进行输出即可。
头像 牛客940073040号
发表于 2023-02-18 17:38:23
#include<iostream> #include<vector> #include<string> #include<cmath> #include<string.h> using namespace std; string ite 展开全文
头像 拥抱未知
发表于 2022-02-08 12:53:35
投机取巧方法 先列出所有可能性 羊为关键变量 控制变量羊 之后的蔬菜和狼任一个优先 有两种可能性 #include<stdio.h> #include<string.h> int main() { printf("sheep_go\nnothing_come\nv 展开全文
头像 lck_lu
发表于 2024-02-20 08:48:43
#include "bits/stdc++.h" using namespace std; int main() { /* * 有一个农夫带一只羊、一筐菜和一只狼过河。如果没有农夫看管,则狼要吃羊,羊要吃菜。但是船很小,只够农夫带一样东西过河。问农夫该如何解此难 展开全文
头像 木何
发表于 2023-03-11 09:22:26
深度优先遍历 #include<iostream> #include<vector> using namespace std; void result_output(vector<int> result) { int k; int status 展开全文
头像 周威zq
发表于 2021-02-22 21:27:14
#include<iostream> #include<vector> #include<set> using namespace std; int arry[4]={0};//0代表农夫,1代表羊,2代表菜,3代表狼;元素0代表在河这边,元素1代表在河那边; 展开全文
头像 木何
发表于 2023-03-01 10:40:57
限制了长度的深度优先搜索 #include<iostream> #include<vector> using namespace std; bool jude(int b[4], int k) { //判断当前状态下农夫是否可以带着k(k=1、2、 展开全文
头像 燃烧的橘子
发表于 2023-02-26 21:43:15
#include <iostream> using namespace std; int main() { cout<<"sheep_go"<<endl; cout<<"nothing_come"<<endl; c 展开全文
头像 iHUST
发表于 2023-01-30 12:10:54
#include <iostream> using namespace std; int main() { cout << "sheep_go" << endl; cout << "nothing_come" << end 展开全文
头像 爱吃洋芋的喵酱
发表于 2024-09-23 22:53:02
#include <iostream> #include<stack> using namespace std; int status[4][2] = {0}; int a[4]; bool f[20][2] = {false}; string Move[2] = {&q 展开全文
头像 pinkbin
发表于 2023-02-20 17:17:33
#include <iostream> #include <vector> #include <queue> using namespace std; class FSM { private: vector<bool>* status; 展开全文