A - Jugs ZOJ - 1005 (模拟)

时间:2023-03-09 03:38:17
A - Jugs ZOJ - 1005 (模拟)

题目链接:https://cn.vjudge.net/contest/281037#problem/A

题目大意:给你a,b,n。a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作。让你用尽可能少的步骤将第二个杯子的当前的水的体积转换成n。

具体思路:就是队列模拟啊,,,,打比赛的时候脑子瓦特了,没读完题目就开始读了,,,这个毛病确实得改了,,

AC代码:

 #include<iostream>
#include<stack>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
# define ll long long
const int maxn = 2e6+;
int head[maxn];
struct node
{
int op;
int a;
int b;
} q[maxn];
int ans[maxn],flag=;
int vis[+][+];
void print(int t)
{
if(t>)
{
print(head[t]);
ans[++flag]=q[t].op;
}
}
void cal(int t1,int t2,int g)
{
head[]=-;
int l=,r=,tt;
int num=;
q[].a=,q[].b=,q[].op=;
vis[][]=;
while(l<=r)
{
int tmp=l;
vis[q[tmp].a][q[tmp].b]=;
l++;
if(vis[t1][q[tmp].b]==){
head[++r]=tmp;
q[r].a=t1;
q[r].b=q[tmp].b;
q[r].op=;
if(q[r].b==g)
{
print(r);
return ;
}
}
if(vis[q[tmp].a][t2]==)
{
head[++r]
=tmp;
q[r].a=q[tmp].a;
q[r].b=t2;
q[r].op=;
if(q[r].b==g)
{
print(r);
return ;
}
}
if(vis[][q[tmp].b]==)
{
head[++r]
=tmp;
q[r].a=;
q[r].b=q[tmp].b;
q[r].op=;
if(q[r].b==g)
{
print(r);
return ;
}
}
if(vis[q[tmp].a][]==)
{
head[++r]
=tmp;
q[r].a=q[tmp].a;
q[r].b=;
q[r].op=;
if(q[r].b==g)
{
print(r);
return ;
}
}
int s1=q[tmp].a-min(q[tmp].a,t2-q[tmp].b);
int s2=q[tmp].b+q[tmp].a-s1;
if(vis[s1][s2]==)
{
head[++r]
=tmp;
q[r].a=s1;
q[r].b=s2;
q[r].op=;
if(q[r].b==g)
{
print(r);
return ;
}
}
s1=q[tmp].b-min(q[tmp].b,t1-q[tmp].a);
s2=q[tmp].a+q[tmp].b-s1;
if(vis[s1][s2]==)
{
head[++r]
=tmp;
tt=q[tmp].b;
q[r].b=s1;
q[r].a=s2;
q[r].op=;
if(q[r].b==g)
{
print(r);
return ;
}
}
}
}
int main()
{
int t1,t2,n;
while(~scanf("%d %d %d",&t1,&t2,&n))
{
memset(vis,,sizeof(vis));
flag=;
cal(t1,t2,n);
for(int i=; i<=flag; i++)
{
if(ans[i]==)
{
printf("fill A\n");
}
else if(ans[i]==)
{
printf("fill B\n");
}
else if(ans[i]==)
{
printf("empty A\n");
}
else if(ans[i]==)
{
printf("empty B\n");
}
else if(ans[i]==)
{
printf("pour A B\n");
}
else if(ans[i]==)
{
printf("pour B A\n");
}
}
printf("success\n");
}
// }
return ;
}