Pots(poj-3414)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 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;
}
题解 文章被收录于专栏

主要写一些题目的题解

全部评论

相关推荐

程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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