比赛 |
2025暑期集训第7场 |
评测结果 |
AAAAAAAAAA |
题目名称 |
倒水 |
最终得分 |
100 |
用户昵称 |
会挽弯弓满月 |
运行时间 |
0.038 s |
代码语言 |
C++ |
内存使用 |
5.04 MiB |
提交时间 |
2025-08-11 16:08:20 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=110,inf=1e9+7;
int read(){
int x=0,f=1;
char c=getchar();
while(c<48||c>57){
if(x==45) f=-1;
c=getchar();
}
while(c>=48&&c<=57){
x=x*10+c-48;
c=getchar();
}
return f*x;
}
int top[4];//容量
int s[4];//当前
int res,ans;
bool vis[N][N][N];
//x与y互相倒水 之前进行了cnt次
void dfs(int x,int y,int cnt){
//到达目标
if(s[x]==res||s[y]==res){
ans=min(ans,cnt);
return;
}
vis[s[1]][s[2]][s[3]]=1;
int sx,sy,g,mid;
for(int i=1;i<=3;i++){
if(i==x) continue;
if(i==y) continue;
mid=i;
}
sx=top[x]-s[x];
sy=top[y]-s[y];
if(sx){//x没满
//y->x
g=min(sx,s[y]);//本次要倒多少水
s[x]+=g;s[y]-=g;
if(!vis[s[1]][s[2]][s[3]]){
dfs(x,mid,cnt+1);
dfs(y,mid,cnt+1);
}
s[x]-=g;s[y]+=g;
}
if(sy){//y没满
//x->y
g=min(sy,s[x]);//本次要倒多少水
s[y]+=g;s[x]-=g;
if(!vis[s[1]][s[2]][s[3]]){
dfs(x,mid,cnt+1);
dfs(y,mid,cnt+1);
}
s[y]-=g;s[x]+=g;
}
return;
}
int main(){
freopen("pourwater.in","r",stdin);
freopen("pourwater.out","w",stdout);
for(int i=1;i<=3;i++) top[i]=read();
s[1]=top[1];
res=read();
ans=inf;
dfs(1,2,0);
s[2]=s[3]=0;
s[1]=top[1];
memset(vis,0,sizeof(vis));
dfs(1,3,0);
if(ans==inf) printf("false");
else printf("%d",ans);
return 0;
}