Pots POJ - 3414 非常可乐 HDU - 1495 bfs(倒水问题)

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

**题意:**题意就是用两个杯子a和b,然后两个杯子之间相互倒水,最终任意一个杯子里的水达到c水量游戏结束,求需要的最少步骤和每个具体的步骤。
**题解:**思路是用bfs来搜索每一种可能性,然后将每一个步骤记录下来,最后回溯输出每一个步骤,用vis二维数组来标记a,b杯的水量是否达到过,从而进行每个状态的搜索。
AC代码如下

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 10000007
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 50;
const int N = 1e2+50;
const int INF = 0x3f3f3f3f;
const int inf = 0xfffffff;//比INF小,防止累加爆int
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
int gcd(int x, int y) {
    
    
	return y ? gcd(y, x % y) : x;
}
int vis[N][N];
string str[10] = {
    
    "", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)","POUR(2,1)"};
int flag=0;
int a,b,c;
struct node{
    
    
	int x,y,step;
	string s;
	node(int x,int y,int step,string s):x(x),y(y),step(step),s(s){
    
    };
};
void bfs(){
    
    
	queue<node>q;
	vis[0][0]=1;
	q.push(node(0,0,0,"0"));
	while(!q.empty()){
    
    
		node temp=q.front();
		q.pop();
		if(temp.x==c||temp.y==c){
    
    
			flag=1;
			cout<<temp.step<<endl;
			for(int i=1;i<temp.s.length();++i)
			cout<<str[temp.s[i]-'0']<<endl;
			return;
		}
		if(temp.x<a){
    
    //a杯未满 
			if(!vis[a][temp.y]){
    
    
				vis[a][temp.y]=1;
				q.push(node(a,temp.y,temp.step+1,temp.s+"1"));
			}
		}
		if(temp.y<b){
    
    //b杯未满 
			if(!vis[temp.x][b]){
    
    
				vis[temp.x][b]=1;
				q.push(node(temp.x,b,temp.step+1,temp.s+"2"));
			}
		}
		if(temp.x!=0){
    
    //a杯不为空,drop 
			if(!vis[0][temp.y]){
    
    
				vis[0][temp.y]=1;
				q.push(node(0,temp.y,temp.step+1,temp.s+"3"));
			}
		}
		if(temp.y!=0){
    
    //b杯不为空 ,drop 
			if(!vis[temp.x][0]){
    
    
				vis[temp.x][0]=1;
				q.push(node(temp.x,0,temp.step+1,temp.s+"4"));
			}
		}
		if(temp.x!=0&&temp.y<b){
    
    //a杯不为空,且b杯未满 
			int next_x,next_y;
			if(temp.x<=b-temp.y)next_x=0,next_y=temp.x+temp.y;
			else next_x=temp.x-b+temp.y,next_y=b;
			if(!vis[next_x][next_y]){
    
    
				vis[next_x][next_y]=1;
				q.push(node(next_x,next_y,temp.step+1,temp.s+"5"));
			}
		}
		if(temp.y!=0&&temp.x<a){
    
    //b杯不为空,且a杯未满 
			int next_x,next_y;
			if(temp.y<=a-temp.x)next_x=temp.y+temp.x,next_y=0;
			else next_x=a,next_y=temp.y-a+temp.x;
			if(!vis[next_x][next_y]){
    
    
				vis[next_x][next_y]=1;
				q.push(node(next_x,next_y,temp.step+1,temp.s+"6"));
			}
		}
	}
}
int main(){
    
    
	speed;
	cin>>a>>b>>c;
	mem(vis,0);
	bfs();
	if(!flag)puts("impossible");
	return 0;
}

非常可乐
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3

**题解:**中文题面,题目的意思很明确。具体的思路就是bfs六种情况搜索最优方案

AC代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 10000007
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 50;
const int N = 1e2+50;
const int INF = 0x3f3f3f3f;
const int inf = 0xfffffff;//比INF小,防止累加爆int
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
int gcd(int x, int y) {
    
    
	return y ? gcd(y, x % y) : x;
}
int s,n,m;
int flag=0;
int vis[N][N][N];
struct node{
    
    
	int left_s,left_n,left_m;
	int step;
	node(int s,int n,int m,int step):left_s(s),left_n(n),left_m(m),step(step){
    
    };
};
int judge(int x,int y,int z){
    
    
	if(x>=0&&x<=s&&y>=0&&y<=n&&z>=0&&z<=m)return 1;
	return 0;
}
void bfs(){
    
    
	for(int i=0;i<=s;++i){
    
    
		for(int j=0;j<=n;++j){
    
    
			for(int k=0;k<=m;++k)
			vis[i][j][k]=0;
		}
	}
	queue<node>q;
	q.push(node(s,0,0,0));
	vis[s][0][0]=1;
	while(!q.empty()){
    
    
		node temp=q.front();
		q.pop();
		if(flag||temp.left_s==temp.left_n&&temp.left_m==0||temp.left_s==temp.left_m&&temp.left_n==0||temp.left_m==temp.left_n&&temp.left_s==0){
    
    
			flag=1;
			printf("%d\n",temp.step);
			return;
		}
		int next_s,next_n,next_m;
		if(temp.left_s!=0){
    
    
			//s->n
			if(temp.left_s<=n-temp.left_n)next_s=0,next_n=temp.left_s+temp.left_n;
			else next_s=temp.left_s-n+temp.left_n,next_n=n;
			next_m=temp.left_m;
			if(!vis[next_s][next_n][next_m]&&judge(next_s,next_n,next_m)){
    
    
				vis[next_s][next_n][next_m]=1;
				q.push(node(next_s,next_n,next_m,temp.step+1));
			}
			//s->m
			if(temp.left_s<=m-temp.left_m)next_s=0,next_m=temp.left_s+temp.left_m;
			else next_s=temp.left_s-m+temp.left_m,next_m=m;
			next_n=temp.left_n;
			if(!vis[next_s][next_n][next_m]&&judge(next_s,next_n,next_m)){
    
    
				vis[next_s][next_n][next_m]=1;
				q.push(node(next_s,next_n,next_m,temp.step+1));
			}
		}
		if(temp.left_n!=0){
    
    
			//n->s
			next_n=0;
			next_s=temp.left_n+temp.left_s;
			next_m=temp.left_m;
			if(!vis[next_s][next_n][next_m]&&judge(next_s,next_n,next_m)){
    
    
				vis[next_s][next_n][next_m]=1;
				q.push(node(next_s,next_n,next_m,temp.step+1));
			}
			//n->m
			if(temp.left_n<=m-temp.left_m)next_n=0,next_m=temp.left_n+temp.left_m;
			else next_n=temp.left_n-m+temp.left_m,next_m=m;
			next_s=temp.left_s;
			if(!vis[next_s][next_n][next_m]&&judge(next_s,next_n,next_m)){
    
    
				vis[next_s][next_n][next_m]=1;
				q.push(node(next_s,next_n,next_m,temp.step+1));
			}
		}
		if(temp.left_m!=0){
    
    
			//m->s
			next_m=0;
			next_s=temp.left_m+temp.left_s;
			next_n=temp.left_n;
			if(!vis[next_s][next_n][next_m]&&judge(next_s,next_n,next_m)){
    
    
				vis[next_s][next_n][next_m]=1;
				q.push(node(next_s,next_n,next_m,temp.step+1));
			}
			//m->n
			if(temp.left_m<=n-temp.left_n)next_m=0,next_n=temp.left_m+temp.left_n;
			else next_m=temp.left_m-n+temp.left_n,next_n=n;
			next_s=temp.left_s;
			if(!vis[next_s][next_n][next_m]&&judge(next_s,next_n,next_m)){
    
    
				vis[next_s][next_n][next_m]=1;
				q.push(node(next_s,next_n,next_m,temp.step+1));
			}
		}

	}
}
int main(){
    
    
	while(scanf("%d%d%d",&s,&n,&m),s,n,m){
    
    
		if(s%2){
    
    
			printf("NO\n");
			continue;
		}
		else{
    
    
			flag=0;
			bfs();
			if(!flag)printf("NO\n");
		}
	}
	return 0;
}

这是后面发掘的一个大佬写的代码,同样的一个题,为啥别人的代码就是如此的赏心悦目,害
大佬的代码

猜你喜欢

转载自blog.csdn.net/qq_40924271/article/details/108146525