题目描述

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)

题解:大概这个题目的难点就是对于路径的输出吧,这里我是用了一个结构体里面的string数组储存了之前已经走过的路径表示,最后将符合的过程输出即可,就是把这六个过程分别模拟一遍就好,注意1->2或2->1的时候要讨论一下1,2目前的已有水量是否大于倒入的杯子。

代码看着很复杂很烦多脑壳痛 ,但是巨容易理解。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <string>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#define maxn 1000000
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
const double pi = acos(-1);
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;
bool visited[1010][1010];
struct wazxy
{
	int x, y;
	string s[1010];
	int steps;
}node,temp,sum;
int asize, bsize, endsize;
bool ans;
queue<wazxy> q;

void bfs() {
	temp.steps = temp.x = temp.y = 0;
	q.push(temp);
	while(!q.empty()){
        node=q.front();
        q.pop();
        if(node.x==endsize||node.y==endsize){
            ans=true;
            sum=node;
            return ;
        }
        if(visited[0][node.y]){  //drop(a)
            visited[0][node.y]=false;
            temp=node,temp.steps++,temp.x=0,temp.s[temp.steps]="DROP(1)";
            q.push(temp);
        }
        if(visited[node.x][0]){  //drop(2)
            visited[node.x][0]=false;
            temp=node,temp.steps++,temp.y=0,temp.s[temp.steps]="DROP(2)";
            q.push(temp);
        }
        if(visited[asize][node.y]){
            visited[asize][node.y]=false;
            temp=node,temp.steps++,temp.x=asize,temp.s[temp.steps]="FILL(1)";
            q.push(temp);
        }
        if(visited[node.x][bsize]){
            visited[node.x][bsize]=false;
            temp=node,temp.steps++,temp.y=bsize,temp.s[temp.steps]="FILL(2)";
            q.push(temp);
        }
        if(node.x+node.y>bsize&&visited[node.x+node.y-bsize][bsize]){           //1->2
            visited[node.x+node.y-bsize][bsize]=false;
            temp=node,temp.x=node.x+node.y-bsize,temp.y=bsize,temp.steps++,temp.s[temp.steps]="POUR(1,2)";
            q.push(temp);
        }
        if(node.x+node.y<=bsize&&visited[0][node.x+node.y]){
            visited[0][node.x+node.y]=false;
            temp=node,temp.x=0,temp.y=node.x+node.y,temp.steps++,temp.s[temp.steps]="POUR(1,2)";
            q.push(temp);
        }
        if(node.x+node.y>asize&&visited[asize][node.x+node.y-asize]){
            visited[asize][node.x+node.y-asize]=false;
            temp=node,temp.steps++,temp.x=asize,temp.y=node.x+node.y-asize,temp.s[temp.steps]="POUR(2,1)";
            q.push(temp);
        }
        if(node.x+node.y<=asize&&visited[node.x+node.y][0]){
            visited[node.x+node.y][0]=false;
            temp=node,temp.steps++,temp.x=node.x+node.y,temp.y=0,temp.s[temp.steps]="POUR(2,1)";
            q.push(temp);
        }

	}
	return ;

}

int main() {
	cin >> asize >> bsize >> endsize;
	ans = false;
	memset(visited,true,sizeof(visited));
	bfs();
	if (ans == true) {
        cout<<sum.steps<<endl;
        for(int i=1;i<=sum.steps;i++){
            cout<<sum.s[i]<<endl;
        }
	}
	else cout << "impossible" << endl;
	return 0;
}