题目
解题思路
倒水问题为隐式bfs问题,初次见到无法马上想到利用bfs的思路解题,但是弄清楚其中过程问题便可以引刃而解。此处可类比迷宫问题。flag数组标记两个水杯中的水量状态,bfs的终止条件即第二杯中的水满足题目要求。在搜索过程中有六种转移方式。对应方式和代码表示如下:
string ans[7] = {"fill A","empty A","fill B","empty B","pour A B","pour B A"};
if(tmp.y == c)
{
for(int i = 0; i < tmp.op.size(); i++)
{
cout << ans[tmp.op[i]-'0'] << endl;
}
cout << "success" << endl;
return ;
}
if(!flag[a][tmp.y])
{
q.push(node(a,tmp.y,tmp.op+"0"));
flag[a][tmp.y] = 1;
}
if(!flag[0][tmp.y])
{
q.push(node(0,tmp.y,tmp.op+"1"));
flag[0][tmp.y] = 1;
}
if(!flag[tmp.x][b])
{
q.push(node(tmp.x,b,tmp.op+"2"));
flag[tmp.x][b] = 1;
}
if(!flag[tmp.x][0])
{
q.push(node(tmp.x,0,tmp.op+"3"));
flag[tmp.x][0] = 1;
}
if(b - tmp.y >= tmp.x)
{
if(!flag[0][tmp.y+tmp.x])
{
q.push(node(0,tmp.y+tmp.x,tmp.op+"4"));
flag[0][tmp.y+tmp.x] = 1;
}
}
else
{
int now = b - tmp.y;
if(!flag[tmp.x - now][b])
{
q.push(node(tmp.x - now,b,tmp.op+"4"));
flag[tmp.x - now][b] = 1;
}
}
if(a - tmp.x >= tmp.y)
{
if(!flag[tmp.y+tmp.x][0])
{
q.push(node(tmp.y+tmp.x,0,tmp.op+"5"));
flag[tmp.y+tmp.x][0] = 1;
}
}
else
{
int now = a - tmp.x;
if(!flag[a][tmp.y-now])
{
q.push(node(a,tmp.y-now,tmp.op+"5"));
flag[a][tmp.y-now] = 1;
}
}
只要确定好了bfs过程,之后的工作便比较容易。此处为了表示出全部倒水过程,可以在定义的结构体中添加一项op用来记录该次操作之前全部操作的序号。所以最终只要对应序号输出操作即可。
本题具体代码如下:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<cstdlib>
#include<ctime>
#include<iomanip>
#define ll long long
#define FOR(i,a,n) for(register int i=a;i<n;i++)
#define RF(i,a,n) for(register int i=a;i>n;i--)
#define NIL -1
using namespace std;
struct node{
int x;
int y;
string op;
node(int a, int b, string s)
{
x = a;
y = b;
op = s;
}
node(int a, int b)
{
x = a;
y = b;
}
};
int flag[1005][1005];
string ans[7] = {"fill A","empty A","fill B","empty B","pour A B","pour B A"};
void bfs(int a, int b, int c)
{
queue<node> q;
q.push(node(0,0));
flag[0][0] = 1;
while(!q.empty())
{
node tmp = q.front();
q.pop();
if(tmp.y == c)
{
for(int i = 0; i < tmp.op.size(); i++)
{
cout << ans[tmp.op[i]-'0'] << endl;
}
cout << "success" << endl;
return ;
}
if(!flag[a][tmp.y])
{
q.push(node(a,tmp.y,tmp.op+"0"));
flag[a][tmp.y] = 1;
}
if(!flag[0][tmp.y])
{
q.push(node(0,tmp.y,tmp.op+"1"));
flag[0][tmp.y] = 1;
}
if(!flag[tmp.x][b])
{
q.push(node(tmp.x,b,tmp.op+"2"));
flag[tmp.x][b] = 1;
}
if(!flag[tmp.x][0])
{
q.push(node(tmp.x,0,tmp.op+"3"));
flag[tmp.x][0] = 1;
}
if(b - tmp.y >= tmp.x)
{
if(!flag[0][tmp.y+tmp.x])
{
q.push(node(0,tmp.y+tmp.x,tmp.op+"4"));
flag[0][tmp.y+tmp.x] = 1;
}
}
else
{
int now = b - tmp.y;
if(!flag[tmp.x - now][b])
{
q.push(node(tmp.x - now,b,tmp.op+"4"));
flag[tmp.x - now][b] = 1;
}
}
if(a - tmp.x >= tmp.y)
{
if(!flag[tmp.y+tmp.x][0])
{
q.push(node(tmp.y+tmp.x,0,tmp.op+"5"));
flag[tmp.y+tmp.x][0] = 1;
}
}
else
{
int now = a - tmp.x;
if(!flag[a][tmp.y-now])
{
q.push(node(a,tmp.y-now,tmp.op+"5"));
flag[a][tmp.y-now] = 1;
}
}
}
return ;
}
int main()
{
int a,b,c;
while(cin >> a >> b >> c)
{
memset(flag,0,sizeof(flag));
bfs(a,b,c);
}
return 0;
}```