// Online C++ compiler to run C++ program online
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<exception>
#include<map>
#include<cmath>
#include<unordered_map>
#include<set>
#include<climits>
#include<ctype.h>
#include<queue>
#include<stack>
#include<list>
#include<string>
using namespace std;
int min_times;
// m0, n0 分别表示剩余的羊、狼个数, x为船容量
// m1, n1 分别表示运输到对岸的羊、狼个数,times为次数
int transport(int m0, int n0, int x, int m1, int n1, int times) {
//若可以一次性运走,结束了,注意等于号。。。
if (x >= m0 + n0) {
if (times + 1 < min_times) {
min_times = times + 1;
}
return times + 1;
}
//尝试运一部分狼一部分羊
//要上船的羊数量不可以超过岸上数量、也不可以超过船的容量
for (int i = 0; i <= m0 && i <= x; i++) {
//要上船的狼的数量不可以超过岸上数量、也不可以超过船装了羊后的剩余的容量
for (int j = 0; j <= n0 && i + j <= x; j++) {
//不可以不运
if (i + j == 0) {
continue;
}
//船离岸后,原来这岸,要么没有羊,要么羊比狼多,才可以运;对岸也要检查,不考虑回程带动物
if ((m0 - i == 0 || m0 - i > n0 - j) && (m1 + i == 0 || m1 + i > n1 + j)) {
//运一次
int result = transport(m0 - i, n0 - j, x, m1 + i, n1 + j, times + 1);
//如果获取了结果,和minTime比较,但是不结束,继续检查
if (result < min_times && result != 0) {
min_times = result;
}
}
}
}
//没有方案了。。返回0
return 0;
}
int ship(int sheepA, int wolfA, int sheepB, int wolfB, int volum, int times)
{
int res = INT_MAX;
if ((sheepA + wolfA) <= volum)
{
return times;
}
for (int i = 0; (i <= sheepA && i <= volum); ++i)
{
for (int k = 0; (k <= wolfA && (k + i) <= volum); ++k)
{
if (k + i == 0) continue;
int leftSheep = sheepA - i;
int leftWolf = wolfA - k;
int bankSheep = sheepB + i;
int bankWolf = wolfB + k;
if (((leftSheep == 0) || (leftSheep > leftWolf)) && ((bankSheep == 0) || (bankSheep > bankWolf)))
{
int val = ship(leftSheep, leftWolf, bankSheep, bankWolf, volum, times+1);
if (val < res)
{
res = val;
}
}
}
}
return res;
}
int main() {
// 处理输入
string input_str;
getline(cin, input_str);
//空格分割
vector<string> v;
while (input_str.find(" ") != string::npos) {
int found = input_str.find(" ");
v.push_back(input_str.substr(0, found));
input_str = input_str.substr(found + 1);
}
v.push_back(input_str);
int M = stoi(v[0]);
int N = stoi(v[1]);
int X = stoi(v[2]);
min_times = INT_MAX;
// 表示已运输到对岸的羊、狼个数
int m_temp = 0;
int n_temp = 0;
//transport(M, N, X, m_temp, n_temp, 0);
min_times = ship(M, N, m_temp, n_temp, X, 1);
cout << min_times;
return 0;
}
华为OD机试 - 羊、狼、农夫过河
最新推荐文章于 2023-04-30 13:58:17 发布