SDUT--Pots--BFS

Pots

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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.

输入

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).

输出

The first line of the output must contain the length of the sequence of operations K.  If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

示例输入

3 5 4

示例输出

6

提示

FILL(2)

POUR(2,1)

DROP(1)

POUR(2,1)

FILL(2)

POUR(2,1)

简单搜索题,对于两个空瓶子,容积分别为A B 有6种操作 把A(或B)清空,把A(或B)装满,把A倒入B,把B倒入A 。对应这6种操作,有6种状态,典型的bfs搜索。不多了,只是这题明明说的是单组输入结果答案却要多组输入才对,白白贡献5个WA。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int m,n,c;
typedef struct node
{
	int v1,v2,op;
};
bool vis[999][999];
void bfs()
{
	node t={0,0,0};
	queue <node> Q;
	Q.push(t);
	vis[0][0]=1;
	while(!Q.empty())
	{
		node f=Q.front();Q.pop();
		if(f.v1==c||f.v2==c)
		{
			cout<<f.op<<endl;
			return ;
		}
		if(f.v1!=m)
		{
			t.v1=m;
			t.op=f.op+1;
			t.v2=f.v2;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 Q.push(t);
			}
		}
		if(f.v2!=n)
		{
			t.v2=n;
			t.op=f.op+1;
			t.v1=f.v1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 Q.push(t);
			}
		}
		if(f.v1!=0)
		{
			t.v1=0;
			t.v2=f.v2;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 Q.push(t);
			}
		}
		if(f.v2!=0)
		{
			t.v2=0;
			t.v1=f.v1;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 Q.push(t);
			}
		}
		if(f.v2!=0&&f.v1!=m)
		{
			t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;
			t.v1=f.v1+f.v2;  if(t.v1>m) t.v1=m;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 Q.push(t);
			}
		}
		if(f.v1!=0&&f.v2!=n)
		{
			t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;
			t.v2=f.v2+f.v1;  if(t.v2>n) t.v2=n;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 Q.push(t);
			}
		}
	}
	puts("impossible");
}
int main()
{

	while(cin>>m>>n>>c)
	{
		memset(vis,0,sizeof(vis));
		bfs();
	}
	return 0;
}

SDUT--Pots--BFS

时间: 2024-11-25 14:48:18

SDUT--Pots--BFS的相关文章

Pots BFS(著名倒水问题升级版)

Pots 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 

Pots 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 po

poj 3414 Pots(BFS)(简单题)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11551   Accepted: 4900   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ 3414 Pots(BFS 倒水)

题意  你有两个容积分别为a,b杯子  你每次可以将某个杯子中的水倒满或者倒掉或者倒到另一个杯子  问能否通过这两个杯子量出c容量的水 和上一个倒可乐问题类似  只是这个操作更多了点  将两个杯子中各含有的水作为状态  每出队列一个状态  将所有可能到达的状态入队  直到有一个杯子里面水的体积为c   打印路径直接递归就行了 #include <map> #include <cstdio> #include <cstring> using namespace std;

POJ 3414 Pots (BFS + 记录路径)

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10300 Accepted: 4362 Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill th

poj 3414 pots (bfs+路径记录)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 4955   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ 3414 Pots (BFS/DFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fi

POJ3414 Pots(BFS)

倒水问题. 题意:给两个杯子,容积分别为A和B.通过从水龙头接水(每次都接满),把杯里水全倒掉,把一个杯子里的水倒到另一个杯子里面三种操作使其中一个杯子里的水为C. 解题思路:6入口的BFS.难点在如何保存路径. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; bool vi

POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A.B,经过6种操作使获得C升水,求最少操作数: 思路:广搜.最少操作数很简单,练习一下打印路径:打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int A,B,C; int shortest[150][150];//更新最短步数

(简单) POJ 3414 Pots,BFS+记录路径。

Description 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