【bfs】调酒壶里的酸奶

题目描述

最近小w学了一手调酒的技巧,这么帅的操作,说不定能靠这个俘获女神的芳心,为了在女神面前露一手,他想在学校里建一个"pub",但是显然学校不可能让他真的建一个"pub",那么他退而求次,想建一个"Yogurt shop",不能用酒,那用酸奶也行啊!
今天女神终于来光顾小w的酸奶店了!兴奋的小w拿出自己准备已久每天都仔细擦干净的装备——调酒壶、果汁机、隔冰器和计量杯、砧板、小刀....准备露一手给女神看看
但是女神却没有那么多耐心,女神只是觉得,自己买一瓶大酸奶喝不完,小瓶酸奶不够喝,所以在小w的酸奶店,说不定她可以想买多少就买多少。
于是女神告诉了小w她想要多少体积的酸奶,而小w却依旧想秀一下自己的操作,于是他决定用仅有的两个调酒壶为女神倒出女神想要的酸奶....
小w的两个调酒壶体积是不同的(一开始都是空的),小w每次可以选择一个调酒壶倒入另一个调酒壶(若A倒入B,A倒完或B倒满则停止),或者选择一个调酒壶倒光,或者选择一个调酒壶去接满酸奶.....
满心失望的小w想找一朵花,一瓣一瓣的撕下来,问问花朵女神到底喜不喜欢他...虽然这个答案是显而易见的,但是他还是想找一朵花...然而找花未果,反正花瓣不是偶数就是奇数,那他索性就用自己的操作次数作为花瓣个数吧!(找不到花我还不能脑补一朵吗...)
但是小w已经没有心情去想答案了...那么你能告诉他,需要多少步操作才能倒出女神想要的酸奶吗?

 

输入

输入包含多组数据,每行三个正整数a,b,c分别表示两个调酒壶的容量以及女神想要的酸奶体积,a,b的范围都在[0,100],c<=max(a,b)   

 

输出

一行包含一个整数表示完成要求的最少操作次数,若达不到则输出"impossible"(没有双引号)

 

样例输入

复制样例数据

10 15 11
6 5 4

样例输出

impossible
4

 

提示

 

 我不知道为什么酸奶可以倒进调酒壶,我也不知道为什么女神不喜欢小w,我只知道凭小w的想象力,游泳池都行更别说一朵花了!

 

解题思路:

对于两个调酒壶,可以对其进行六种操作,将a中的酒倒入b中,将b中的酒倒入a中,将a灌满,将b灌满,将a倒空,将b倒空,用一个数组标记一下两酒壶出现的情况,bfs即可。

推荐两道与此题差不多的题

https://blog.csdn.net/YT201758501112/article/details/83278909
https://blog.csdn.net/YT201758501112/article/details/83278654

代码:

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int a,b,c;
bool vis[120][120];
struct node
{int x;int y;int step;
};
void bfs()
{queue<node> q;while(!q.empty()) q.pop();node fis;fis.x=0;fis.y=0;fis.step=0;vis[0][0]=true;q.push(fis);while(!q.empty()) {node fro=q.front(),nxt;q.pop();if(fro.x==c||fro.y==c) {cout<<fro.step<<endl;return;}rep(i,1,6) {if(i==1) {int nape=fro.x+fro.y;if(fro.x>0) {if(nape>b) {nxt.x=nape-b;nxt.y=b;}else {nxt.x=0;nxt.y=nape;}nxt.step=fro.step+1;if(vis[nxt.x][nxt.y]==false) {vis[nxt.x][nxt.y]=true;q.push(nxt);}}}if(i==2) {int nape=fro.x+fro.y;if(fro.y>0) {if(nape>a) {nxt.y=nape-a;nxt.x=a;}else {nxt.y=0;nxt.x=nape;}nxt.step=fro.step+1;if(vis[nxt.x][nxt.y]==false) {vis[nxt.x][nxt.y]=true;q.push(nxt);}}}if(i==3) {if(fro.x>0) {nxt.x=0;nxt.y=fro.y;nxt.step=fro.step+1;if(vis[nxt.x][nxt.y]==false) {vis[nxt.x][nxt.y]=true;q.push(nxt);}}}if(i==4) {if(fro.y>0) {nxt.y=0;nxt.x=fro.x;nxt.step=fro.step+1;if(vis[nxt.x][nxt.y]==false) {vis[nxt.x][nxt.y]=true;q.push(nxt);}}}if(i==5) {if(fro.x<a) {nxt.x=a;nxt.y=fro.y;nxt.step=fro.step+1;if(vis[nxt.x][nxt.y]==false) {vis[nxt.x][nxt.y]=true;q.push(nxt);}}}if(i==6) {if(fro.y<b) {nxt.y=b;nxt.x=fro.x;nxt.step=fro.step+1;if(vis[nxt.x][nxt.y]==false) {vis[nxt.x][nxt.y]=true;q.push(nxt);}}}}}cout<<"impossible"<<endl;
}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);while(cin>>a>>b>>c) {memset(vis,false,sizeof vis);bfs();}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536307.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【dfs】Election of Evil

题目描述 Dylan is a corrupt politician trying to steal an election. He has already used a mind-control technique to enslave some set U of government representatives. However, the representatives who will be choosing the winner of the election is a differe…

【思维】Iranian ChamPions Cup

题目描述 The Iranian ChamPions Cup (ICPC), the most prestigious football league in Iran, is reaching its end, and people are eagerly waiting for the finals, which happened to be between the two most popular Iranian teams, Persepolis and Esteghlal. The ICP…

【数学】Chaarshanbegaan at Cafebazaar

题目描述 Chaarshanbegaan is a gathering event at Cafebazaar similar to TGIF events at Google. Some entertainment programs like pantomime, foosball, Xbox/PS4, and several board games are part of the event. You are going to set up a dart game in Chaarshanbe…

【思维】Congestion Charging Zone

题目描述 Tehran municipality has set up a new charging method for the Congestion Charging Zone (CCZ) which controls the passage of vehicles in Tehran’s high-congestion areas in the congestion period (CP) from 6:30 to 19:00. There are plate detection came…

【二分】LED

题目描述 A Light-Emitting Diode (LED) is a semiconductor light source, which emits light when an electric current of voltage higher than a threshhold is applied to its leads. ACM R&D recently reported that they have succesfully developed a new LED, na…

【差分数组】Master of GCD

题目描述 Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Hakase is interested in primes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and for every l≤i≤r, she will change ai into ai*x. To…

【模拟】Ground Defense

题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced along an east-west line. In fact, they are often numbered in order from 1 to n, where 1 is the westmost city and n is the eastmost city. Linetopia was a lovely plac…

【模拟】Bulbs

题目描述 Greg has an m n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Initially, some of the bulbs are on and some are off. Greg can toggle some bulbs by shooting his laser at them. When he shoots his laser at a bulb, it toggles th…

【模拟】Ingenious Lottery Tickets

题目描述 Your friend Superstitious Stanley is always getting himself into trouble. This time, in his Super Lotto Pick and Choose plan, he wants to get rich quick by choosing the right numbers to win the lottery. In this lottery, entries consist of six dis…

【数学】Hunter’s Apprentice

题目描述 When you were five years old, you watched in horror as a spiked devil murdered your parents. You would have died too, except you were saved by Rose, a passing demon hunter. She ended up adopting you and training you as her apprentice. Rose’s cur…

【模拟】Thanks, TuSimple!

题目链接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId5979 Thanks, TuSimple! Time Limit: 1 Second Memory Limit: 65536 KB In the very first sentence of the very first problem, we would like to give our sincere thanks to TuSimple,…

【二维差分】Monitor

Monitor 题目&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid6514 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 600 Accepted Submission(s): 190 Problem Description Xiaoteng has a la…

【数学】MORE XOR

Given a sequence of nnn numbers a1,a2,⋯&ThinSpace;,ana_1, a_2,\cdots, a_na1​,a2​,⋯,an​ and three functions. Define a function f(l,r)f(l,r)f(l,r) which returns ⊕a[x](l≤x≤r)\oplus a[x] (l \le x \le r)⊕a[x](l≤x≤r). The \oplus⊕ represents excl…

【数学】Element Swapping

Element Swapping Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has an integer sequence a1,a2,a3,…,ana_1,a_2,a_3,\dots,a_na1​,a2​,a3​,…,an​ and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements aia_iai​ an…

【二分+二维前缀和】Largest Allowed Area

Largest Allowed Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 146 解决: 54 [提交] [状态] [命题人:admin] 题目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【数学】Floating-Point Hazard

Floating-Point Hazard 时间限制: 1 Sec 内存限制: 128 MB 提交: 106 解决: 42 [提交] [状态] [命题人:admin] 题目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【manacher】Strings in the Pocket

Strings in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found two strings ss1s2…snss_1s_2\dots s_nss1​s2​…sn​ and tt1t2…tntt_1t_2\dots t_ntt1​t2​…tn​ in his left pocket, where sis_isi​ indicates the iii-th character in…

【并查集+dp】Team

Team 时间限制: 1 Sec 内存限制: 128 MB 提交: 124 解决: 10 [提交] [状态] [命题人:admin] 题目描述 ACM-ICPC is a interesting game. A team takes part in this game must consist of exactly (no more and no less) three players. Every year, many new members wil…

【线段树】Segment Tree

Segment Tree 时间限制: 1 Sec 内存限制: 512 MB 提交: 107 解决: 23 [提交] [状态] [命题人:admin] 题目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach w…

UVA1025——A Spy in the Metro【dp】

题目链接&#xff1a;https://cn.vjudge.net/problem/UVA-1025 题目大意&#xff1a;Mario从第1站出发&#xff0c;目的是在时刻T会见车站 nnn 的一个间谍。由于在车站等待容易被抓&#xff0c;所以应尽量躲在开动的火车上&#xff0c;即在车站等待的时间最短&#xff0c;且Ma…