bfs pot

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)
AC代码:

#include<stdio.h>
#include<string>
#include<queue>
#include<iostream>
using namespace std;
struct node{
   
	int x;
	int y;
	string step;
	int cnt;
};
const int MAX=100 + 5; 
bool vis[MAX][MAX];
int A,B,C;
void bfs()
{
   
	queue<node>que;
	node nd1;
	nd1.step="FILL(1)\n";
	nd1.cnt=1;
	nd1.x=A;
	nd1.y=0;
	vis[A][0]=1;
	que.push(nd1);
	node nd2;
	nd2.step="FILL(2)\n";
	nd2.x=0;
	nd2.y=B;
	nd2.cnt=1;
	vis[0][B]=1;
	que.push(nd2);
	while(!que.empty())
	{
   
		node nd=que.front();
		que.pop();
	//	printf("cnt=%d\n",nd.cnt);
		if(nd.x==C||nd.y==C)
		{
   
			printf("%d\n",nd.cnt);
			cout<<nd.step<<endl;
			return ;
		}
		nd.cnt++;
		node nd3;
		nd3.cnt=nd.cnt;
		//	printf("cnt=%d\n",nd.cnt);
		if(nd.x<A&&!vis[A][nd.y])
		{
   
			vis[A][nd.y]=1;
			nd3.x=A;
			nd3.y=nd.y;
			nd3.step=nd.step+"FILL(1)\n";
			que.push(nd3);
			
		}
		if(nd.y<B&&!vis[nd.x][B])
		{
   
			vis[nd.x][B]=1;
			nd3.x=nd.x;
			nd3.y=B;
			nd3.step=nd.step+"FILL(2)\n";
			que.push(nd3);
		}
		if(nd.x>0&&nd.y!=B)
		{
   
			if(nd.x>=(B-nd.y)&&nd.x!=0)
			{
   
				nd3.x=nd.x-(B-nd.y);
				nd3.y=B;
			}
			else
			{
   
				nd3.y=nd.y+nd.x;
				nd3.x=0;
				
			}
			if(!vis[nd3.x][nd3.y])
			{
   
				vis[nd3.x][nd3.y]=1;
				nd3.step=nd.step+"POUR(1,2)\n";
				que.push(nd3);
			}
		}
		if(nd.y>0&&nd.x!=A)
		{
   
			if(nd.y>=(A-nd.x)&&nd.y!=0)
			{
   
				nd3.y=nd.y-(A-nd.x);
					nd3.x=A;
			}
			else
			{
   
				nd3.x=nd.y+nd.x;
				nd3.y=0;
				
			}
			if(!vis[nd3.x][nd3.y])
			{
   
				vis[nd3.x][nd3.y]=1;
				nd3.step=nd.step+"POUR(2,1)\n";
				que.push(nd3);
			}
		}
		if(nd.x>0&&!vis[0][nd.y])
		{
   
			vis[0][nd.y]=1;
			nd3.step=nd.step+"DROP(1)\n";
			nd3.x=0;
			nd3.y=nd.y;
			que.push(nd3);
		}
			if(nd.y>0&&!vis[nd.x][0])
		{
   
			vis[nd.x][0]=1;
			nd3.step=nd.step+"DROP(2)\n";
			nd3.x=nd.x;
			nd3.y=0;
			que.push(nd3);
		}
	}
	printf("impossible\n");
	
	
}

int main()
{
   
	scanf("%d%d%d",&A,&B,&C);
	bfs();
 } 
全部评论

相关推荐

06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务