农夫与奶牛 c++ 做到我脱发了

tip:太难了
/*
这道题目很有意思 是第一次遇到
一道最小化最大值的类似问题
数据1 2 4 8 9
解题思路:1一头 4一头     差值:4 - 1 = 3
       4一头 8一头     差值:8 - 4 = 4
       1一头 8一头     差值:8 - 1 = 7
   或者:1一头 4一头     差值:4 - 1 = 3
       4一头 9一头     差值:9 - 4 = 5
       1一头 9一头     差值:9 - 1 = 8
    对差值求min 第一种安排情况:min(3,4,7) = 3
               第一种安排情况:min(3,5,8) = 3
    本题要点就是两者之间的距离尽可能大所以距离是从1开始到a[n-1]/N
    一直试探下去 这样就有一个区间[1,a[n-1]/N] 这样就能找到一个差值尽可能大的情况
    这个就能用到二分查找 贪心
*/
#include <bits/stdc++.h>
using namespace std;
int a[100001],N,C;
//对一个得到的mid进行判断
int judge(int mid){
    //将牛放在牛棚里的数量 初始化
    int cow = 1;
    //当前牛 应为要判断其他牛和当前一头牛的距离差值
    int precow = a[0];
    //下一头牛 从第二头开始 但是数组下标是1
    int nextcow ;
    //进行距离测量差值
    for( nextcow = 1; nextcow < N; nextcow++){
        //判断两头牛之间的距离 大于等于就是满足题目要求:继续往下探测
        if(a[nextcow] - precow >= mid){
            cow ++ ;
        //那么当前牛棚的要更新 应为有一个a[nextnow]和precow的差值满足
        //所以precow要变为a[nextnow]
            precow = a[nextcow];
            //三头牛都有位置
            if(cow == 3)
                return 1;
        }
    }
    return 0;
}
int Binary(int a[]){
    int L = 0;
    int R = a[N-1] - a[0];
    //最大最小距离
    int maxdis;
    while(L <= R){
       //防止数据过大溢出处理 这个就是a[n-1]/N 两个牛棚距离最大的情况
       int mid = L + (R - L) / 2;
       if(judge(mid)){
       //此时的L就是满足的
            maxdis = mid;
       //这样距离就比mid更大了 在范围里一直探索
            L = mid + 1;
        }
       else
       //距离小于mid 有牛棚两者之间的距离小于mid
            R = mid - 1;
    }
    return maxdis;
}
int main()
{
    while(scanf("%d%d",&N,&C) != EOF){
        for(int i = 0; i < N; i++ )
            scanf("%d",&a[i]);
        sort(a,a+N);
        printf("%d",Binary(a));
    }
    return 0;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
农夫过河问题是一个经典的逻辑问题,可以用来演示搜索算法和状态空间的应用。以下是一个简单的C++代码示例,用于解决农夫过河问题: ```cpp #include <iostream> #include <queue> #include <set> #include <string> using namespace std; // 定义状态结构体 struct State { int farmer; // 农夫的位置,0表示在起点,1表示在终点 int wolf; // 狼的位置,0表示在起点,1表示在终点 int goat; // 山羊的位置,0表示在起点,1表示在终点 int cabbage; // 卷心菜的位置,0表示在起点,1表示在终点 // 重载==运算符,用于判断两个状态是否相等 bool operator==(const State& other) const { return farmer == other.farmer && wolf == other.wolf && goat == other.goat && cabbage == other.cabbage; } // 重载<运算符,用于在set容器中进行排序 bool operator<(const State& other) const { if (farmer != other.farmer) return farmer < other.farmer; if (wolf != other.wolf) return wolf < other.wolf; if (goat != other.goat) return goat < other.goat; return cabbage < other.cabbage; } }; // 判断当前状态是否合法 bool isValidState(const State& state) { // 如果山羊和狼在同一边,而农夫不在,或者山羊和卷心菜在同一边,而农夫不在,返回false if ((state.goat == state.wolf && state.farmer != state.goat) || (state.goat == state.cabbage && state.farmer != state.goat)) { return false; } return true; } // 判断当前状态是否为目标状态 bool isGoalState(const State& state) { // 所有物品都在终点时,返回true return state.farmer == 1 && state.wolf == 1 && state.goat == 1 && state.cabbage == 1; } // 农夫过河问题的求解函数 void solve() { queue<State> states; // 存储待搜索的状态 set<State> visitedStates; // 存储已访问过的状态 // 初始状态,农夫、狼、山羊、卷心菜都在起点 State initialState = {0, 0, 0, 0}; states.push(initialState); visitedStates.insert(initialState); while (!states.empty()) { State currentState = states.front(); states.pop(); // 判断当前状态是否为目标状态 if (isGoalState(currentState)) { cout << "找到解答:" << endl; cout << "农夫\t狼\t山羊\t卷心菜" << endl; cout << (currentState.farmer == 0 ? "起点" : "终点") << "\t" << (currentState.wolf == 0 ? "起点" : "终点") << "\t" << (currentState.goat == 0 ? "起点" : "终点") << "\t" << (currentState.cabbage == 0 ? "起点" : "终点") << endl; break; } // 生成下一步可能的状态 for (int i = 0; i < 4; i++) { State nextState = currentState; // 农夫移动 nextState.farmer = 1 - currentState.farmer; // 根据当前移动的物品,更新对应的位置 switch (i) { case 0: // 农夫独自过河 break; case 1: // 农夫带狼过河 nextState.wolf = 1 - currentState.wolf; break; case 2: // 农夫带山羊过河 nextState.goat = 1 - currentState.goat; break; case 3: // 农夫带卷心菜过河 nextState.cabbage = 1 - currentState.cabbage; break; } // 判断下一步状态是否合法且未访问过 if (isValidState(nextState) && visitedStates.find(nextState) == visitedStates.end()) { states.push(nextState); visitedStates.insert(nextState); } } } if (states.empty()) { cout << "未找到解答!" << endl; } } int main() { solve(); return 0; } ``` 这段代码使用了广度优先搜索算法来解决农夫过河问题。它通过遍历所有可能的状态,并判断状态是否合法和是否为目标状态来找到解答。在搜索过程中,使用队列来存储待搜索的状态,使用集合来存储已访问过的状态,以避免重复搜索。最终找到解答时,输出每个状态下农夫、狼、山羊、卷心菜的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值