每日打卡(1/2)
传送门:点击打开链接
题目大意:有一瓶可乐和两个杯子,瞎倒以后,要求平分这瓶可乐。
需要注意的是:可乐也可以放在瓶子里面喝。
思路:
感觉比之前倒水的那道题目简单,但是题意有点模糊不清,没有说放在瓶子里也能喝。
主要就是一个循环6次的BFS,思路以及解法都和倒水那题差不多
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 105;
int s,n,m,ans;
struct node{
int s,x,y,tot;
};
bool vis[maxn][maxn][maxn];
int bfs()
{
node a;
a.s= s;
a.x = 0;
a.y = 0;
a.tot = 0;
queue<node> q;
q.push(a);
while(!q.empty())
{
a = q.front();
// cout<<a.x<<" "<<a.y<<" "<<a.s<<" "<<a.tot<<endl;
q.pop();
if((a.x==a.y&&!a.s)||(a.x==a.s&&!a.y)||(a.y==a.s&&!a.x))
{
return a.tot;
}
node next;
for(int i=0;i<6;i++)//1往23,2往13,3往12
{
switch(i)
{
case 0:
if(a.s>n-a.x)
{
next.s = a.s-(n-a.x);
next.x = n;
next.y = a.y;
}
else{
next.s = 0;
next.x = a.s+a.x;
next.y = a.y;
}
break;
case 1:
if(a.s>m-a.y)
{
next.s = a.s-(m-a.y);
next.y = m;
next.x = a.x;
}
else{
next.s = 0;
next.y = a.s+a.y;
next.x = a.x;
}
break;
case 2:
if(a.x>m-a.y)
{
next.s = a.s;
next.x = a.x-(m-a.y);
next.y = m;
}
else{
next.s = a.s;
next.x = 0;
next.y = a.x+a.y;
}
break;
case 3:
if(a.y>n-a.x)
{
next.s = a.s;
next.y = a.y-(n-a.x);
next.x = n;
}
else{
next.s = a.s;
next.y = 0;
next.x = a.x+a.y;
}
break;
case 4:
next.s = a.s+a.x;
next.x = 0;
next.y = a.y;
break;
case 5:
next.s = a.s+a.y;
next.y = 0;
next.x = a.x;
break;
}
next.tot = a.tot+1;
if(!vis[next.x][next.y][next.s])
{
q.push(next);
vis[next.x][next.y][next.s] = 1;
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&s,&n,&m)&&s&&n&&m)
{
memset(vis,0,sizeof(vis));
ans = bfs();
if(ans==-1) cout<<"NO"<<endl;
else cout<<ans<<endl;
}
return 0;
}
这是搜索专题的最后一题了,(最后一题BFS实在是太弱智了,不想做),到目前为止共做11题。
底下进入下一阶段最短路的练习~