水杯倒水问题 bfs

刚开始还是弄不好各个状态之间怎么转换,e而且也不懂怎么去储存已经遍历过的状态,后来看到了哈希,感觉挺好的,这道题也就是深搜了。。基本的都看得懂

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue> 
#include<cstring>
using namespace std;
typedef struct 
{
   int cur[3];
   int v[3];
   int step;
}data;
queue<data> Q;
data begin, end;
//vector<int> vis;
int vis[1000010];
int ok;
int finl(data u, data v)
{
    if(u.cur[0]==v.cur[0]&&u.cur[1]==v.cur[1]&&u.cur[2]==v.cur[2])
    return 1;
    return 0;
}
int hash(data u)
{
    return  10000 * u.cur[0] + 100 * u.cur[1] + u.cur[2];
}
int i, j;

int main()
{
    int N;
    scanf("%d",&N);
   while(N--)
     {
         scanf("%d%d%d", &begin.v[0], &begin.v[1], &begin.v[2]);
         begin.cur[0] = begin.v[0];
         begin.cur[1] = begin.cur[2] = 0;
         begin.step = 0;
         scanf("%d%d%d", &end.cur[0], &end.cur[1], &end.cur[2]);
         ok = 0;
         memset(vis, 0, sizeof(vis));
         Q.push(begin);
         vis[hash(begin)] = 1;
         while(!Q.empty())
         {
             data u = Q.front();
             Q.pop();
             if(finl(u, end))
             {
                 printf("%d\n", u.step);
                 ok = 1;
                 break;
             }
             for(i = 0; i <= 2; i++)
                 for(j = 0; j <= 2; j++)
                     if(i != j)
                     {
                        int min = u.v[j] - u.cur[j];
                         if(u.cur[i] < min)
                             min = u.cur[i];
                         data v = u;
                         v.cur[i] -= min;
                         v.cur[j] += min;
                         v.step = u.step +1;
                         if( !vis[hash(v)])
                         {
                             vis[hash(v)] = 1;
                             Q.push(v);
                         }
                     }
         }
         if(!ok)
         {
             printf("-1\n");
         }
         while(!Q.empty())
         {
             Q.pop();
         }
     }
   system("pause");
   return 0;
}


倒水问题通常是指有几个不同容量的杯子,通过倒来倒去达到某个目标水量的问题。一般可以使用广度优先搜索(BFS)算法来解决此类问题。 ### 算法思路 1. **状态表示**:使用一个元组来表示每个杯子中的水量。 2. **队列**:使用队列来进行广度优先搜索,队列中存储每个状态。 3. **访问集合**:使用集合来记录已经访问过的状态,避免重复搜索。 4. **操作**:定义所有可能的倒水操作,如从一个杯子倒到另一个杯子、将一个杯子倒满、将一个杯子倒空等。 5. **搜索**:从初始状态开始,不断进行操作,将新的状态加入队列,并记录到访问集合中,直到找到目标状态或队列为空。 ### 代码示例 ```cpp #include <iostream> #include <queue> #include <set> #include <vector> using namespace std; // 表示状态的结构体 struct State { vector<int> volumes; int steps; State(const vector<int>& v, int s) : volumes(v), steps(s) {} }; // 判断是否达到目标状态 bool isTarget(const State& state, int target) { for (int volume : state.volumes) { if (volume == target) { return true; } } return false; } // 倒水操作 vector<State> getNextStates(const State& state, const vector<int>& capacities) { vector<State> nextStates; int n = capacities.size(); // 倒空操作 for (int i = 0; i < n; ++i) { if (state.volumes[i] > 0) { vector<int> newVolumes = state.volumes; newVolumes[i] = 0; nextStates.emplace_back(newVolumes, state.steps + 1); } } // 倒满操作 for (int i = 0; i < n; ++i) { if (state.volumes[i] < capacities[i]) { vector<int> newVolumes = state.volumes; newVolumes[i] = capacities[i]; nextStates.emplace_back(newVolumes, state.steps + 1); } } // 倒水操作 for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != j && state.volumes[i] > 0 && state.volumes[j] < capacities[j]) { vector<int> newVolumes = state.volumes; int pour = min(state.volumes[i], capacities[j] - state.volumes[j]); newVolumes[i] -= pour; newVolumes[j] += pour; nextStates.emplace_back(newVolumes, state.steps + 1); } } } return nextStates; } // 广度优先搜索 int bfs(const vector<int>& capacities, int target) { queue<State> q; set<vector<int>> visited; vector<int> initialVolumes(capacities.size(), 0); State initialState(initialVolumes, 0); q.push(initialState); visited.insert(initialVolumes); while (!q.empty()) { State current = q.front(); q.pop(); if (isTarget(current, target)) { return current.steps; } vector<State> nextStates = getNextStates(current, capacities); for (const State& nextState : nextStates) { if (visited.find(nextState.volumes) == visited.end()) { q.push(nextState); visited.insert(nextState.volumes); } } } return -1; } int main() { vector<int> capacities = {3, 5}; int target = 4; int result = bfs(capacities, target); if (result != -1) { cout << "Minimum steps: " << result << endl; } else { cout << "No solution found." << endl; } return 0; } ``` ### 代码解释 1. **State结构体**:用于表示每个状态,包含每个杯子的水量和当前的步数。 2. **isTarget函数**:判断当前状态是否达到目标状态。 3. **getNextStates函数**:根据当前状态和杯子的容量,生成所有可能的下一步状态。 4. **bfs函数**:使用广度优先搜索算法,从初始状态开始搜索,直到找到目标状态或队列为空。 5. **main函数**:定义杯子的容量和目标水量,调用bfs函数进行搜索,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值