題目大意:给出2个水杯和目标的容量,输出经过fill drop pour三种操作的最少步数。
这题思路还是比较清晰的,可是就是不知为什么mle了,找不出来问题,先码着放这里,有空回来看。
mle:
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
int a,b,c,cnt[105][105],faedge[105][105],mins,ans;
bool vis[105][105];
struct tie{
int ra,rb;
tie(int aa,int bb):ra(aa),rb(bb){}
tie(){}
};
map<int,map<int,tie> > fanode;
void print(tie t){
map<int,string> m;
stack<tie> s;
m[0]="FILL(1)";
m[1]="FILL(2)";
m[2]="DROP(1)";
m[3]="DROP(2)";
m[4]="POUR(1,2)";
m[5]="POUR(2,1)";
for(;;){
if(t.ra==a&&t.rb==b) break;
s.push(t);
t=fanode[t.ra][t.rb];
}
while(!s.empty()){
tie temp=s.top();
s.pop();
cout<<m[faedge[temp.ra][temp.rb]]<<endl;
}
}
bool bfs(tie t){
queue<tie> q;
q.push(t);
vis[t.ra][t.rb]=true;
cnt[t.ra][t.rb]=0;
while(!q.empty()){
tie temp=q.front();
q.pop();
if(a-temp.ra==c||b-temp.rb==c){
cout<<cnt[temp.ra][temp.rb]<<endl;
print(temp);
return true;
}
for(int i=0;i<6;i++){
if(i==0){///fill(1)
if(temp.ra){
int rra=0;
int rrb=temp.rb;
if(!vis[rra][rrb]){
vis[rra][rrb]=true;
cnt[rra][rrb]=cnt[temp.ra][temp.rb]+1;
faedge[rra][rrb]=i;
fanode[rra][rrb].ra=temp.ra;
fanode[rra][rrb].rb=temp.rb;
q.push(tie(rra,rrb));
}
}
}
else if(i==1){///fill(2)
if(temp.rb){
int rrb=0;
int rra=temp.ra;
if(!vis[rra][rrb]){
vis[rra][rrb]=true;
cnt[rra][rrb]=cnt[temp.ra][temp.rb]+1;
faedge[rra][rrb]=i;
fanode[rra][rrb].ra=temp.ra;
fanode[rra][rrb].rb=temp.rb;
q.push(tie(rra,rrb));
}
}
}
else if(i==2){///drop(1)
if(temp.ra!=a){
int rrb=temp.rb;
int rra=a;
if(!vis[rra][rrb]){
vis[rra][rrb]=true;
cnt[rra][rrb]=cnt[temp.ra][temp.rb]+1;
faedge[rra][rrb]=i;
fanode[rra][rrb].ra=temp.ra;
fanode[rra][rrb].rb=temp.rb;
q.push(tie(rra,rrb));
}
}
}
else if(i==3){///drop(2)
if(temp.rb!=b){
int rrb=b;
int rra=temp.ra;
if(!vis[rra][rrb]){
vis[rra][rrb]=true;
cnt[rra][rrb]=cnt[temp.ra][temp.rb]+1;
faedge[rra][rrb]=i;
fanode[rra][rrb].ra=temp.ra;
fanode[rra][rrb].rb=temp.rb;
q.push(tie(rra,rrb));
}
}
}
else if(i==4){///pour(1,2)
int rra,rrb;
if(temp.ra!=a&&temp.rb){///a不为空且b有空位
int aw=a-temp.ra;///a瓶中的酒
if(aw>temp.rb){
rrb=0;
rra=temp.ra+temp.rb;
}
else{
rra=a;
rrb=temp.rb-aw;
}
if(!vis[rra][rrb]){
vis[rra][rrb]=true;
cnt[rra][rrb]=cnt[temp.ra][temp.rb]+1;
faedge[rra][rrb]=i;
fanode[rra][rrb].ra=temp.ra;
fanode[rra][rrb].rb=temp.rb;
q.push(tie(rra,rrb));
}
}
}
else if(i==5){///pour(2,1)
int rra,rrb;
if(temp.rb!=b&&temp.ra){///b不为空且a有空位
int bw=b-temp.rb;///b瓶中的酒
if(bw>temp.ra){
rra=0;
rrb=temp.rb+temp.ra;
}
else{
rrb=b;
rra=temp.ra-bw;
}
vis[rra][rrb]=true;
cnt[rra][rrb]=cnt[temp.ra][temp.rb]+1;
faedge[rra][rrb]=i;
fanode[rra][rrb].ra=temp.ra;
fanode[rra][rrb].rb=temp.rb;
q.push(tie(rra,rrb));
}
}
}
}
return false;
}
int main(){
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
memset(vis,0,sizeof(vis));
memset(cnt,-1,sizeof(cnt));
memset(faedge,0,sizeof(faedge));
tie t;
t.rb=b;
t.ra=a;
if(bfs(t));
else
cout<<"impossible"<<endl;
}
return 0;
}
ac(爬的别人的,感觉差不多啊):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 105;
const double eps = 1e-7;
bool vis[maxn][maxn];
const int dir[4][2]= {1, 0, 0, 1, -1, 0, 0, -1};
char map[maxn][maxn];
int a, b, k;
struct node
{
int vola, volb, step;
char str[maxn][maxn];
};
bool bfs()
{
memset(vis, false, sizeof(vis));
queue<node> que;
node p, q;
p.vola = 0, p.volb = 0, p.step = 0;
que.push(p);
vis[0][0] = 1;
///vis[p.vola][p.volb] = true;
while(!que.empty())
{
p = que.front();
que.pop();
if(p.vola==k || p.volb == k)
{
cout<<p.step<<endl;
for(int i=1; i<=p.step; i++)
///cout<<p.str[i]<<endl;
printf("%s\n",p.str[i]);
return true;
}
///倒满 a
if(p.vola == 0)
{
q = p;
q.vola = a;
q.step++;
strcpy(q.str[q.step], "FILL(1)");
if(!vis[q.vola][q.volb])
{
vis[q.vola][q.volb] = true;
que.push(q);
}
}
///把 a 中的水倒出来
else if(p.vola <= a)
{
q = p;
q.vola = 0;
q.step++;
strcpy(q.str[q.step], "DROP(1)");
if(!vis[q.vola][q.volb])
{
vis[q.vola][q.volb] = true;
que.push(q);
}
///a -> b
if(p.volb < b)
{
q = p;
if(q.volb + q.vola <= b)
{
q.volb += q.vola;
q.vola = 0;
}
else
{
q.vola = (q.vola+q.volb)-b;
q.volb = b;
}
q.step++;
strcpy(q.str[q.step], "POUR(1,2)");
if(!vis[q.vola][q.volb])
{
vis[q.vola][q.volb] = true;
que.push(q);
}
}
}
///把 b 倒满
if(p.volb == 0)
{
q = p;
q.volb = b;
q.step++;
strcpy(q.str[q.step], "FILL(2)");
if(!vis[q.vola][q.volb])
{
vis[q.vola][q.volb] = true;
que.push(q);
}
}
///把 b 中的水倒出来
else if(p.volb <= b)
{
q = p;
q.volb = 0;
q.step++;
strcpy(q.str[q.step],"DROP(2)");
if(!vis[q.vola][q.volb])
{
vis[q.vola][q.volb] = true;
que.push(q);
}
if(p.vola < a)
{
q = p;
if(q.vola + q.volb <= a)
{
q.vola += q.volb;
q.volb = 0;
}
else
{
q.volb = (q.vola+q.volb)-a;
q.vola = a;
}
q.step++;
strcpy(q.str[q.step], "POUR(2,1)");
if(!vis[q.vola][q.volb])
{
vis[q.vola][q.volb] = true;
que.push(q);
}
}
}
}
return false;
}
int main()
{
while(cin>>a>>b>>k)
{
bool ok = bfs();
if(!ok)
puts("impossible");
}
return 0;
}