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

大牛们看看 为啥容器VECTOR要先加clear()函数 不加就错了 附代码

Posted by xj_nan at 2009-08-17 00:10:58 on Problem 1606
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int used[1010][1010]={0};



typedef struct mystruct
{
	int x,y,x1,y1,flag,pre;
}mystruct;

vector<mystruct> Q1;

void outprint(mystruct node)
{
	if(node.pre!=-1)
	{
		outprint(Q1[node.pre]);
		if(node.flag==0)
			cout<<"fill A"<<endl;
		if(node.flag==1)
			cout<<"fill B"<<endl;
		if(node.flag==2)
			cout<<"empty A"<<endl;
		if(node.flag==3)
			cout<<"empty B"<<endl;
		if(node.flag==4)
			cout<<"pour A B"<<endl;
		if(node.flag==5)
			cout<<"pour B A"<<endl;
	}
	else
		return ;
}

void BFS(mystruct node,int N)
{
	queue<mystruct> Q;
	
	mystruct node1;
	Q.push(node);
	Q1.push_back(node);
	int c=0;
	used[0][0]=1;
	Q1.clear();                                         //不加就错了
	while(!Q.empty())
	{
		node=Q.front();
		Q1.push_back(node);
		

		if(node.y1==N)
		{
			outprint(node);
			cout<<"success"<<endl;
			return ;
		}
		else
		{
			
			if(node.x1!=node.x&&used[node.x][node.y1]==0)//填满x  0
			{
				used[node.x][node.y1]=1;
				node1=node;
				node1.x1=node.x;
				node1.flag=0;
				node1.pre=c;
				Q.push(node1);
				
				
			}
			if(node.y1!=node.y&&used[node.x1][node.y]==0)//填满y  1
			{
				used[node.x1][node.y]=1;
				node1=node;
				node1.y1=node.y;
				node1.pre=c;
				node1.flag=1;
				Q.push(node1);
				//Q1.push_back(node1);
				
			}
			if(node.x1!=0&&used[0][node.y1]==0)//倒掉x  2
			{
				used[0][node.y1]=1;
				node1=node;
				node1.x1=0;
				node1.pre=c;
				node1.flag=2;
				Q.push(node1);
				//Q1.push_back(node1);
				
			}
			if(node.y1!=0&&used[node.x1][0]==0)//倒掉y  3
			{
				used[node.x1][0]=1;
				node1=node;
				node1.y1=0;
				node1.pre=c;
				node1.flag=3;
				Q.push(node1);
				//Q1.push_back(node1);
			
			}

			if(node.x1<=node.y-node.y1&&used[0][node.y1+node.x1]==0)//如果x1里面的水少过y-y1的  x倒到y  4
			{
				used[0][node.y1+node.x1]=1;
				node1=node;
				node1.x1=0;
				node1.y1=node.y1+node.x1;
				node1.pre=c;
				node1.flag=4;
				Q.push(node1);
				//Q1.push_back(node1);
				
			}
			if(node.x1>node.y-node.y1&&used[node.x1-node.y+node.y1][node.y]==0)//如果x1里面的水多过y-y1的  x倒到y  4
			{
				used[node.x1-node.y+node.y1][node.y]=1;
				node1=node;
				node1.x1=node.x1-node.y+node.y1;
				node1.y1=node.y;
				node1.pre=c;
				node1.flag=4;
				Q.push(node1);
					//Q1.push_back(node1);
				
			}
			if(node.y1<=node.x-node.x1&&used[node.x1+node.y1][0]==0)  //5 
			{
				used[node.x1+node.y1][0]=1;
				node1=node;
				node1.y1=0;
				node1.x1=node.y1+node.x1;
				node1.pre=c;
				node1.flag=5;
				Q.push(node1);
				//Q1.push_back(node1);
			
			}
			if(node.y1>node.x-node.x1&&used[node.x][node.y1-node.x+node.x1]==0)   //5
			{
				used[node.x][node.y1-node.x+node.x1]=1;
				node1=node;
				node1.x1=node.x;
				node1.y1=node.y1-node.x+node.x1;
				node1.pre=c;
				node1.flag=5;
				Q.push(node1);
				//Q1.push_back(node1);
				
			}
		}
		Q.pop();
		c++;
	}
}
int main()
{
	int Ca,Cb,N,d;
	mystruct node;
	
	while(cin>>Ca&&cin>>Cb&&cin>>N)
	{
		for(int i=0;i<=1000;i++)
			for(int j=0;j<=1000;j++)
				used[i][j]=0;
		node.x=Ca;
		node.y=Cb;
		node.x1=0;
		node.y1=0;
		node.pre=-1;
		BFS(node,N);
	}
	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