poj 3414

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16029 Accepted: 6756 Special Judge

Description

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

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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 AB, 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两个杯子来说,要么是从a向b倒水,路径最短,要么是从b向a倒水,路径最短。但是如果a和b之间

是倍数关系,但是c不与a和b成倍数关系,则无解。(应用到离散数学里面的东西,我不是特别懂,读者可以去查一下资料)。

代码:

#include<stdio.h>
#define Max 200
int a, b, c;
char str1[Max][Max], str2[Max][Max];
void path() {
	int i, j, temp, count1 = 0, count2 = 0, temp1, temp2;
	temp1 = a;
	temp2 = b;
	temp = a > b ? a: b;
	b = a + b - temp;
	a = temp;
	if(a % b == 0 && a % c != 0)
		printf("impossible\n");
	else {
		a = temp1;
		b = temp2;
		i = 0;
		j = 0;
		while(i != c && j != c) {     //从a向b倒水 
			if(i == 0) {         
				i = a;
				sscanf("FILL(1)", "%s", str1[count1++]);
			}
			else if(j == b) {
				j = 0;
				sscanf("DROP(2)", "%s", str1[count1++]);
			}
			else {
				temp = i;
				i = i - (b - j);
				if(i < 0)
					i = 0;
				j = temp + j;
				if(j > b)
					j = b;
				sscanf("POUR(1,2)", "%s", str1[count1++]);
			}			
		}
		
		i = 0;
		j = 0;
		while(i != c && j != c) {    // 从b向a倒水 
			if(j == 0) {
				j = b;
				sscanf("FILL(2)", "%s", str2[count2++]);
			}
			else if(i == a) {
				i = 0;
				sscanf("DROP(1)", "%s", str2[count2++]);
			}
			else {
				temp = j;
				j = j - (a - i);
				if(j < 0)
					j = 0;
				i = temp + i;
				if(i > a)
					i = a;
				sscanf("POUR(2,1)", "%s", str2[count2++]);
			}			
		}
		if(count1 > count2) {
			printf("%d\n", count2);
			for(i = 0; i < count2; i++)
				printf("%s\n", str2[i]);
		}
		else{
			printf("%d\n", count1);
			for(i = 0; i < count1; i++)
				printf("%s\n", str1[i]);
		}
	}	
}

int main() {
	while(scanf("%d%d%d", &a, &b, &c) != EOF) {
		path();
	}
}


第二种用bfs来进行搜索找到最优解,一共只有六种情况,倒满a,倒满b,清空a,清空b, 从a向b倒水,从b向a倒水。

这里给出两种实现方法,一种用库函数队列, 一种用数组模拟队列实现。

代码1:

#include<stdio.h>
#include<string.h>
#define Max 105
int dir[] = {1, 2, 21, 12, 10, 20};
typedef struct q{
	int a, b;
	int step;
	int pre;
	int act;
}pat[Max * Max], node;
int a, b, visited[Max][Max], front, rear, c;
pat path;

int bfs() {
	int k;
	front = 0;
	rear = 1;
	memset(visited, 0, sizeof(visited));
	path[front].a = 0;
	path[front].b = 0;
	path[front].step = 0;
	path[front].pre = -1;
	int i, j;
	node temp;
	while(front < rear) {
		if(path[front].a == c || path[front].b == c)
			return 1;
		for(k = 0; k < 6; k++) {
			if(dir[k] == 1) {
				i = a;
				j = path[front].b;
			}
			else if(dir[k] == 2) {
				i = path[front].a;
				j = b;
			}
			else if(dir[k] == 21){
				if(path[front].a + path[front].b > a) {
					j = path[front].a + path[front].b - a;
					i = a;
				}
				else {
					i = path[front].a + path[front].b;
					j = 0;
				}
			}
			else if(dir[k] == 12) {
				if(path[front].a + path[front].b > b) {
					i = path[front].a + path[front].b - b;
					j = b;
				}
				else {
					j = path[front].a + path[front].b;
					i = 0;
				}
			}
			else if(dir[k] == 10) {
				i = 0;
				j = path[front].b;
			}
			else {
				i = path[front].a;
				j = 0;
			}
			if(!visited[i][j]) {
				temp.a = i;
				temp.b = j;
				temp.step = path[front].step + 1;
				temp.pre = front;
				temp.act = dir[k];
				path[rear] = temp;
				visited[i][j] = 1;
				rear++;
			}
		}
		front++;
	}
	return 0;
}

void outPut(node temp) {
	if(temp.pre == -1)
		return;
	else {
		outPut(path[temp.pre]);
		if(temp.act == 1)
			printf("FILL(1)\n");
		else if(temp.act == 2)
			printf("FILL(2)\n");
		else if(temp.act == 21)
			printf("POUR(2,1)\n");
		else if(temp.act == 12)
			printf("POUR(1,2)\n");
		else if(temp.act == 10)
			printf("DROP(1)\n");
		else 
			printf("DROP(2)\n");
	}
}

int main() {
	while(scanf("%d%d%d", &a, &b, &c) != EOF) {
		if(bfs()) {
			printf("%d\n", path[front].step);
			outPut(path[front]);
		}
		else
			printf("impossible\n");
	}
}

代码2:

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
struct path {
	int a;
	int b;
	int step;
	string s;
}pa, ph;
queue <path> q;
int A, B, C, visited[200][200], aWater, bWater;
int BFS() {
	int i;
	char c;
	while(!q.empty())
		q.pop();
	visited[0][0] = 1;
	pa.a = 0;
	pa.b = 0;
	pa.step = 0;
	q.push(pa);
	while(!q.empty()) {
		pa = q.front();
		q.pop();
		if(pa.a == C || pa.b == C)
			return 1;
		for(i = 1; i <= 6; i++) {
			if(i == 1) {
				aWater = A;
				bWater = pa.b;
			}
			else if(i == 2) {
				aWater = pa.a;
				bWater = B;
			}
			else if(i == 3) {
				if(pa.a + pa.b > A) {
					aWater = A;
					bWater = pa.a + pa.b - A;
				}
				else {
					aWater = pa.a + pa.b;
					bWater = 0;
				}
			}
			else if(i == 4) {
				if(pa.a + pa.b > B) {
					aWater = pa.a + pa.b - B;
					bWater = B;
				}
				else {
					aWater = 0;
					bWater = pa.a + pa.b;
				}
			}
			else if(i == 5) {
				aWater = 0;
				bWater = pa.b;
			}
			else {
				aWater = pa.a;
				bWater = 0;
			}
			if(!visited[aWater][bWater]) {
				ph.a = aWater;
				ph.b = bWater;
				visited[aWater][bWater] = 1;
				ph.step = pa.step + 1;
				c = i + '0';
				ph.s = "";
				ph.s += (pa.s + c);
				q.push(ph); 
			}
		}
	}
	return 0;	
}

void outPut(char c) {
	if(c == '1')
		printf("FILL(1)\n");
	else if(c == '2')
		printf("FILL(2)\n");
	else if(c == '3')
		printf("POUR(2,1)\n");
	else if(c == '4')
		printf("POUR(1,2)\n");
	else if(c == '5')
		printf("DROP(1)\n");
	else
		printf("DROP(2)\n");
}

int main() {
	while(scanf("%d%d%d", &A, &B, &C) != EOF) {
		memset(visited, 0, sizeof(visited));
		if(BFS()) {
			printf("%d\n", pa.step);
			for(int i = 0; i < pa.step; i++) 
				outPut(pa.s[i]);
		}
		else
			printf("impossible\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值