Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

想过个好年!真心请教!究竟哪里Wrong了?? ×-×

Posted by wcfairytale at 2007-12-14 21:20:22 on Problem 3414
//BFS!!!!!!!!!!!!!!!!!

#include<iostream>

using namespace std;

class node
{
public:
	int opr;
	node *parent;
};

const int MAX=1000000;
bool have[101][101];
int queuea[MAX];
int queueb[MAX];
int queues[MAX];
int queueo[MAX];
int op[MAX];
node *queuep[MAX];

int a,b,c;

void BFS()
{
	int i,j;

	//pre judge
	for(i=1;i<100;++i)
	{
		if(a%i==0 && b%i==0 && c%i!=0)
		{
			printf("impossible\n");
			return;
		}
	}

	int front=0,tail=0; 

	queuea[++tail]=0;//ta
	queueb[tail]=0;//tb
	queues[tail]=0;//step
	queueo[tail]=0;//operation
	queuep[tail]=NULL;//node

	int ta,tb,ts,to;
	node *q;
	
	while(front!=tail)
	{
		if(front+1 == MAX)front=-1;
		ta=queuea[++front];
		tb=queueb[front];
		ts=queues[front];
		to=queueo[front];
		q=queuep[front];
		
		have[ta][tb]=true;
	
		node *p=new node;
		p->opr=to;
		p->parent=q;
		//printf("ta=%d tb=%d ts=%d\n",ta,tb,ts);
		if(ta==c || tb==c)
		{
			printf("%d\n",ts);
			i=0;
			while(p->opr!=0)
			{
				op[i++]=p->opr;
				p=p->parent;
			}
			for(i=i-1;i>=0;--i)
			{
				if(op[i]==1)printf("FILL(1)\n");
				else if(op[i]==2)printf("FILL(2)\n");
				else if(op[i]==3)printf("POUR(1,2)\n");
				else if(op[i]==4)printf("POUR(2,1)\n");
				else if(op[i]==5)printf("DROP(1)\n");
				else if(op[i]==6)printf("DROP(2)\n");
			}
			return;
		}

		//fill(1)
		if(ta!=a && have[a][tb]==false)
		{
			if(tail+1 == MAX)tail=-1;
			queuea[++tail]=a;
			queueb[tail]=tb;
			queues[tail]=ts+1;
			queueo[tail]=1;
			queuep[tail]=p;
		}
		//fill(2)
		if(tb!=b && have[ta][b]==false)
		{
			if(tail+1 == MAX)tail=-1;
			queuea[++tail]=ta;
			queueb[tail]=b;
			queues[tail]=ts+1;
			queueo[tail]=2;
			queuep[tail]=p;
		}
		//pour(1,2);
		int tob=ta+tb;
		if(tob>b)tob=b;
		int ra=ta-(b-tb);
		if(ra<0)ra=0;
		if(ta!=0 && have[ra][tb]==false)
		{
			if(tail+1 == MAX)tail=-1;
			queuea[++tail]=ra;
			queueb[tail]=tob;
			queues[tail]=ts+1;
			queueo[tail]=3;
			queuep[tail]=p;
		}
		//pour(2,1)
		int toa=ta+tb;
		if(toa>a)toa=a;
		int rb=tb-(a-ta);
		if(rb<0)rb=0;
		if(tb!=0 && have[toa][rb]==false)
		{
			if(tail+1 == MAX)tail=-1;
			queuea[++tail]=toa;
			queueb[tail]=rb;
			queues[tail]=ts+1;
			queueo[tail]=4;
			queuep[tail]=p;
		}
		//drop(1)
		if(ta!=0 && have[0][tb]==false)
		{
			if(tail+1 == MAX)tail=-1;
			queuea[++tail]=0;
			queueb[tail]=tb;
			queues[tail]=ts+1;
			queueo[tail]=5;
			queuep[tail]=p;
		}
		//drop(2)
		if(tb!=0 && have[ta][0]==false)
		{
			if(tail+1 == MAX)tail=-1;
			queuea[++tail]=ta;
			queueb[tail]=0;
			queues[tail]=ts+1;
			queueo[tail]=6;
			queuep[tail]=p;
		}
	}
	printf("impossible\n");
}
int main()
{
	scanf("%d %d %d",&a,&b,&c);
	//printf("a=%d b=%d c=%d\n",a,b,c);
	//return 0;
	memset(have,false,sizeof(have));
	BFS();
	/*
	while(scanf("%d %d %d",&a,&b,&c))
	{
	//printf("a=%d b=%d c=%d\n",a,b,c);
	//return 0;
		memset(have,false,sizeof(have));
		memset(queuea,false,sizeof(queuea));
		memset(queueb,false,sizeof(queueb));
		memset(queueo,false,sizeof(queueo));
		memset(queuep,false,sizeof(queuep));
		BFS();
	}
	*/
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator