题解
2024-08-01 10:47:49
发布于:湖南
36阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
struct node1{
int opr;//1:倒入 2:清空 3:把水倒入另一个桶
bool tmp;
};
struct node{
vector <node1> v;
int x = 0, y = 0, step = 0;
};
bool vis[105][105];
int n, m, k;
node bfs(){
node xxxxx;
queue <node> q;
q.push(xxxxx);
while(!q.empty()){
node head = q.front();
q.pop();
if(head.x == k || head.y == k) return head;
//开始痛苦
node tmp = head;
tmp.x = n;//FILL1
if(!vis[tmp.x][tmp.y]){
vis[tmp.x][tmp.y] = 1;
tmp.step++, tmp.v.push_back({1, 0});
q.push(tmp);
}
tmp = head;
tmp.y = m;//FILL2
if(!vis[tmp.x][tmp.y]){
vis[tmp.x][tmp.y] = 1;
tmp.step++, tmp.v.push_back({1, 1});
q.push(tmp);
}
tmp = head;
tmp.x = 0;//DROP1
if(!vis[tmp.x][tmp.y]){
vis[tmp.x][tmp.y] = 1;
tmp.step++, tmp.v.push_back({2, 0});
q.push(tmp);
}
tmp = head;
tmp.y = 0;//DROP2
if(!vis[tmp.x][tmp.y]){
vis[tmp.x][tmp.y] = 1;
tmp.step++, tmp.v.push_back({2, 1});
q.push(tmp);
}
tmp = head;
if(tmp.x + tmp.y <= n){//POUR12
tmp.x += tmp.y, tmp.y = 0;
}else{
tmp.y -= (n - tmp.x), tmp.x = n;
}
if(!vis[tmp.x][tmp.y]){
vis[tmp.x][tmp.y] = 1;
tmp.step++, tmp.v.push_back({3, 0});
q.push(tmp);
}
tmp = head;
if(tmp.x + tmp.y <= m){//POUR21
tmp.y += tmp.x, tmp.x = 0;
}else{
tmp.x -= (m - tmp.y), tmp.y = m;
}
if(!vis[tmp.x][tmp.y]){
vis[tmp.x][tmp.y] = 1;
tmp.step++, tmp.v.push_back({3, 1});
q.push(tmp);
}
}
return xxxxx;
}
int main(){
cin >> n >> m >> k;
node ans = bfs();
if(ans.step == 0){
cout << "impossible";
return 0;
}
cout << ans.step << endl;
for(int i = 0; i < ans.v.size(); i++){
if(ans.v[i].opr == 1){
printf("FILL(%d)\n", ans.v[i].tmp + 1);
}
else if(ans.v[i].opr == 2){
printf("DROP(%d)\n", ans.v[i].tmp + 1);
}else{
printf("POUR(%d,%d)\n", 2 - ans.v[i].tmp, ans.v[i].tmp + 1);
}
}
return 0;
}
这里空空如也
有帮助,赞一个