POJ 3414 (BFS)

题目链接:http://poj.org/problem?id=3414

题意:给出三个容器,各有容量A,B,C

有以下三种方式:

①:装满i

②:清空i

③把i倒入j中,直至倒满,否则全部全部倒入

只要模拟这6中操作就可以了...

难点在与打印路径..用map做一个该层与上一层的映射。

只能感慨自己写的代码丑到爆了..

最后参考了discuss里面的代码...

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=1010;
int T,n;
int A,B,C;
bool flag;

struct node{
	int x,y;
	string s;
	bool operator<(node a) const{
		return x<a.x||(x==a.x&&y<a.y);
	}
};

void Bfs(){
	string ans="";
	int sum=0;
	queue<node>q;
	map<node,node>m;
	node s,tmp;
	s.x=0,s.y=0,tmp.x=-1;
	m[s]=tmp;
	q.push(s);
	while(!q.empty()){
		s=q.front();
		q.pop();
		if(s.x==C||s.y==C){
			for(tmp=s;tmp.x||tmp.y;tmp=m[tmp])
				sum++,ans=tmp.s+"\n"+ans;
			cout<<sum<<endl<<ans;
			flag=true;
			return ;
		}

		tmp.x=A;tmp.y=s.y;tmp.s="FILL(1)";
		if(m.find(tmp)==m.end()) {q.push(tmp);m[tmp]=s;}

		tmp.x=s.x;tmp.y=B;tmp.s="FILL(2)";
		if(m.find(tmp)==m.end()) {q.push(tmp);m[tmp]=s;}

		tmp.x=0;tmp.y=s.y;tmp.s="DROP(1)";
		if(m.find(tmp)==m.end()) {q.push(tmp);m[tmp]=s;}

		tmp.x=s.x;tmp.y=0;tmp.s="DROP(2)";
		if(m.find(tmp)==m.end()) {q.push(tmp);m[tmp]=s;}

		tmp.x=min(s.x+s.y,A);tmp.y=max(0,s.x+s.y-A);
		tmp.s="POUR(2,1)";
		if(m.find(tmp)==m.end()) {q.push(tmp);m[tmp]=s;}

		tmp.x=max(0,s.x+s.y-B);tmp.y=min(s.x+s.y,B);
		tmp.s="POUR(1,2)";
		if(m.find(tmp)==m.end()) {q.push(tmp);m[tmp]=s;}

	}
	flag=false;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
#endif
	while(~scanf("%d%d%d",&A,&B,&C)){
		Bfs();
		if(!flag) puts("impossible");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值