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

哪位高手帮忙看看代码,自己在下面能通过,为什么总是wa呢 ,我用的是宽度优先搜索

Posted by 2007011268 at 2008-11-30 15:44:52 on Problem 1606
# include <iostream>                                          // 1606 wrong answer
# include <vector>
# include <string>
# include <queue>
using namespace std;
class jiedian
{
public:
	jiedian(int aa,int bb,string ss,int pp):a(aa),b(bb),s(ss),pre(pp) {}
	int a;
	int b;
	string s;
	int pre;
};
bool Search[1001][1001];
int ca,cb,N;
queue<jiedian> qj;
vector<jiedian> jvec;
void clear(int ca,int cb)
{
	for(int i=0;i<=ca;i++)
		for(int j=0;j<=cb;j++)
			Search[i][j]=false;
}
void print(jiedian jd)
{
	if(jd.pre==-1)
	{
		return;
	}
	else
	{
		print(jvec[jd.pre]);
		cout<<jd.s<<endl;
	}
}
void bfs(int N)
{
	clear(ca,cb);
    while(!qj.empty())
		qj.pop();
	jvec.clear();
	jiedian gen(0,0,"",-1);
	qj.push(gen);
	Search[0][0]=true;
	int num=0;
	while(!qj.empty())
	{
		if(qj.front().b==N)
		{                                       
			print(qj.front());
			cout<<"success"<<endl;
			return;
		}
		if(qj.front().a!=ca&&!Search[ca][qj.front().b])
		{
			jiedian jd(ca,qj.front().b,"fill A",num); 
			qj.push(jd);
			Search[ca][qj.front().b]=true;
		}
		if(qj.front().a!=0&&qj.front().b!=0)
		{
			if(!Search[0][qj.front().b])
			{
				jiedian jd(0,qj.front().b,"empty A",num);   
				qj.push(jd);
				Search[0][qj.front().b]=true;
			}
			if(!Search[qj.front().a][0])                     
			{
				jiedian jd(qj.front().a,0,"empty B",num);        
				qj.push(jd);
				Search[qj.front().a][0]=true;
			}
		}
		if(qj.front().b!=cb&&!Search[qj.front().a][cb])
		{
			jiedian jd(qj.front().a,cb,"fill B",num);            
			qj.push(jd);
			Search[qj.front().a][cb]=true;
		}
		if(qj.front().a!=0&&qj.front().b!=cb)
		{
			if(qj.front().a+qj.front().b>cb&&!Search[qj.front().a+qj.front().b-cb][cb])
			{
				jiedian jd(qj.front().a+qj.front().b-cb,cb,"pour A B",num);   
				qj.push(jd);
				Search[qj.front().a+qj.front().b-cb][cb]=true;
			}
			if(qj.front().a+qj.front().b<=cb&&!Search[0][qj.front().a+qj.front().b])
			{
				jiedian jd(0,qj.front().a+qj.front().b,"pour A B",num);       
				qj.push(jd);
				Search[0][qj.front().a+qj.front().b]=true;
			}
		}
		if(qj.front().a!=ca&&qj.front().b!=0)
		{
			if(qj.front().a+qj.front().b>ca&&!Search[ca][qj.front().a+qj.front().b-ca])
			{
				jiedian jd(ca,qj.front().a+qj.front().b-ca,"pour B A",num);             
				qj.push(jd);
				Search[ca][qj.front().a+qj.front().b-ca]=true;
			}
			if(qj.front().a+qj.front().b<=ca&&!Search[qj.front().a+qj.front().b][0])
			{ 
				jiedian jd(qj.front().a+qj.front().b,0,"pour B A",num);            
				qj.push(jd);
				Search[qj.front().a+qj.front().b][0]=true;
			}
		}
		jvec.push_back(qj.front());
		qj.pop();
		num++;		
	}
	return;
}
int main()
{
	while(cin)
	{
		cin>>ca>>cb>>N;
		bfs(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