2021-01-31

OpenJudge 023 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 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. 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’.

样例输入
3 5 4
样例输出
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
思路
用DSF来做,开一个二维数组记录俩个容器的状态,开一个队列记录每次操作完之后的状态和第几个操作以及父亲节点(要输出)。

#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
//#include<windows.h>
using namespace std;
int a, b;
int A, B;
int C;
int states[105][105];
bool OK;
static string maths[6] = { "FILL(1)","FILL(2)","POUR(1,2)","POUR(2,1)","DROP(1)","DROP(2)" };
struct datas {
	int x;
	int y;
	int op;
	int father;
}datas[10001];
void DFS(int head, int count) {
	count++;
	if (datas[head].father != 0) {
		DFS(datas[head].father, count);
	}
	else {
		cout << count << endl;
	}
	cout << maths[datas[head].op-1] << endl;
}
int main(void) {
	cin >> A >> B >> C;
	memset(states, 0, sizeof(states));
	a = 0;
	b = 0;
	OK = false;
	int head = 0;
	int tail = 1;
	datas[0].father = -1;
	datas[0].x = 0;
	datas[0].y = 0;
	datas[0].op = 0;
	states[0][0] = 1;
	while (head != tail) {
		if (datas[head].x == C || datas[head].y == C) {
			OK = true;
		}
		if (OK) {
			break;
		}
		for (int i = 1; i <=6; ++i) {
			switch (i) {
			case 1:
				if (datas[head].x != A&&states[A][datas[head].y] == 0) {
					datas[tail].op = 1;
					datas[tail].x = A;
					datas[tail].y = datas[head].y;
					datas[tail].father = head;
					states[A][datas[head].y] = 1;
					tail++;
				}
				break;
			case 2:
				if (datas[head].y != B&&states[datas[head].x][B] == 0) {
					datas[tail].op = 2;
					datas[tail].x = datas[head].x;
					datas[tail].y = B;
					datas[tail].father = head;
					states[datas[head].x][B] = 1;
					tail++;
				}
				break;
			case 3:
				int ca, cb;
				if (datas[head].x + datas[head].y > B) {
					cb = B;
					ca = datas[head].x + datas[head].y - B;
				}
				else {
					cb = datas[head].x + datas[head].y;
					ca = 0;
				}
				if (datas[head].x != 0 && states[ca][cb] == 0&&datas[head].y!=B) {
					datas[tail].x = ca;
					datas[tail].y = cb;
					datas[tail].father = head;
					datas[tail].op = 3;
					states[ca][cb] = 1;
					tail++;
				}
				break;
			case 4:
				int caa, cbb;
				if (datas[head].x + datas[head].y > A) {
					caa = A;
					cbb = datas[head].x + datas[head].y - A;
				}
				else {
					caa = datas[head].x + datas[head].y;
					cbb = 0;
				}
				if (datas[head].y != 0 && states[caa][cbb] == 0&&datas[head].x!=A) {
					datas[tail].x = caa;
					datas[tail].y = cbb;
					datas[tail].father = head;
					datas[tail].op = 4;
					states[caa][cbb] = 1;
					tail++;
				}
				break;
			case 5:
				if (datas[head].x != 0 && states[0][datas[head].y] == 0) {
					datas[tail].x = 0;
					datas[tail].y = datas[head].y;
					datas[tail].father = head;
					datas[tail].op = 5;
					states[0][datas[head].y] = 1;
					tail++;
				}
				break;
			case 6:
				if (datas[head].y != 0 && states[datas[head].x][0] == 0) {
					datas[tail].x = datas[head].x;
					datas[tail].y = 0;
					datas[tail].father = head;
					datas[tail].op = 6;
					states[datas[head].x][0] = 1;
					tail++;
				}
				break;
			}
		}
		head++;
	}
	if (OK) {
		DFS(head, 0);
	}
	else {
		cout << "impossible" << endl;
	}
//	Sleep(2000000);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值