poj3414(BFS)

  就是一个很简单的BFS的题目,搜索出状态,看能否得目标状态,就是有六种操作,每种都记一下就好了。


  

#include "stdio.h"
#include "string.h"
#include "math.h"
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;

#define MAXM 1
#define MAXN 1
#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b
#define Mem(a,b) memset(a,b,sizeof(a))
int Mod = 1000000007;
double pi = acos(-1.0);
double eps = 1e-6;

typedef struct{
	int f,t,w,next;
}Edge;
Edge edge[MAXM];
int head[MAXN];
int kNum;

void addEdge(int f, int t, int w)
{
	edge[kNum].f = f;
	edge[kNum].t = t;
	edge[kNum].w = w;
	edge[kNum].next = head[f];
	head[f] = kNum ++;
}

bool visit[10205];
int pre[10205];   //记录前驱
int opra[10205];  //记录前驱的操作
int A,B,C;
int e1, e2;


void solve(){
	Mem(pre, -1);
	Mem(visit, false);
	Mem(opra, -1);
	bool isHas = false;

	visit[0] = true;
	pre[0] = -1;

	queue<int> q;
	q.push(0);

	int temp;
	while( !q.empty() ){
		int t = q.front();
		q.pop();
		int t1 = t / 101;
		int t2 = t % 101;
		if( t1 == C || t2 == C ){
			isHas = true;
			temp = t;
			break;
		}

		if( t1 != A ){
			int t3 = A * 101 + t2;
			if( !visit[t3] ){
				visit[t3] = true;
				pre[t3] = t;
				opra[t3] = 1;
				q.push(t3);
			}
			if( t2 != 0 ){
				if( t1 + t2 <= A ){
					int t3 = ( t1 + t2 ) * 101;
					if( !visit[t3] ){
						visit[t3] = true;
						pre[t3] = t;
						opra[t3] = 4;
						q.push(t3);
					}
				}
				else{
					int t3 = A * 101 + ( t2 + t1 - A );
					if( !visit[t3] ){
						visit[t3] = true;
						pre[t3] = t;
						opra[t3] = 4;
						q.push(t3);
					}
				}
			}
		}

		if( t2 != B ){
			int t3 = t1 * 101 + B;
			if( !visit[t3] ){
				visit[t3] = true;
				pre[t3] = t;
				opra[t3] = 2;
				q.push(t3);
			}
			if( t1 != 0 ){
				if( t1 + t2 <= B ){
					int t3 = t1 + t2;
					if( !visit[t3] ){
						visit[t3] = true;
						pre[t3] = t;
						opra[t3] = 3;
						q.push(t3);
					}
				}
				else{
					int t3 = (t1 + t2 - B) * 101 + B;
					if( !visit[t3] ){
						visit[t3] = true;
						pre[t3] = t;
						opra[t3] = 3;
						q.push(t3);
					}
				}
			}
		}

		if( t1 != 0 ){
			int t3 = t2;
			if( !visit[t3] ){
				visit[t3] = true;
				pre[t3] = t;
				opra[t3] = 5;
				q.push(t3);
			}
		}

		if( t2 != 0 ){
			int t3 = t1 * 101;
			if( !visit[t3] ){
				visit[t3] = true;
				pre[t3] = t;
				opra[t3] = 6;
				q.push(t3);
			}
		}

	}

	if( !isHas ){
		printf("impossible\n");
		return;
	}

	stack<int> s;

	while( temp != 0 ){
		s.push(opra[temp]);
		temp = pre[temp];
	}

	printf("%d\n",s.size());
	while( !s.empty() ){
		int t = s.top();
		s.pop();
		if( t == 1 ){
			printf("FILL(1)\n");
		}
		else if( t == 2 ){
			printf("FILL(2)\n");
		}
		else if( t == 3 ){
			printf("POUR(1,2)\n");
		}
		else if( t == 4 ){
			printf("POUR(2,1)\n");
		}
		else if( t == 5 ){
			printf("DROP(1)\n");
		}
		else{
			printf("DROP(2)\n");
		}
	}
}



int main()
{
//	freopen("d:\\test.txt", "r", stdin);
	while(cin>>A>>B>>C){
		solve();
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值