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 sky4091 at 2007-06-16 12:40:18 on Problem 1915
#include<iostream>
using namespace std;
typedef struct queuenode{
	short i,j,p;
}node;

bool b[310][310];
node q[96100];
int s;
short x,y,x0,y0,n;


void BFS(int r){
	int i,h;
	for (h=0;!(q[h].i==x && q[h].j==y);h++){
		if (q[h].i-1>=0 && q[h].j-2>=0 && b[q[h].i-1][q[h].j-2]==false){
			q[r].p=h;
			q[r].i=q[h].i-1;
			q[r++].j=q[h].j-2;
			b[q[h].i-1][q[h].j-2]=true;
		}
		if (q[h].i-2>=0 && q[h].j-1>=0 && b[q[h].i-2][q[h].j-1]==false){
			q[r].p=h;
			q[r].i=q[h].i-2;
			q[r++].j=q[h].j-1;
			b[q[h].i-2][q[h].j-1]=true;
		}
		if (q[h].i-2>=0 && q[h].j+1<n && b[q[h].i-2][q[h].j+1]==false){
			q[r].p=h;
			q[r].i=q[h].i-2;
			q[r++].j=q[h].j+1;
			b[q[h].i-2][q[h].j+1]=true;
		}
		if (q[h].i-1>=0 && q[h].j+2<n && b[q[h].i-1][q[h].j+2]==false){
			q[r].p=h;
			q[r].i=q[h].i-1;
			q[r++].j=q[h].j+2;
			b[q[h].i-1][q[h].j+2]=true;
		}
		if (q[h].i+1<n && q[h].j-2>=0 && b[q[h].i+1][q[h].j-2]==false){
			q[r].p=h;
			q[r].i=q[h].i+1;
			q[r++].j=q[h].j-2;
			b[q[h].i+1][q[h].j-2]=true;
		}
		if (q[h].i+2<n && q[h].j-1>=0 && b[q[h].i+2][q[h].j-1]==false){
			q[r].p=h;
			q[r].i=q[h].i+2;
			q[r++].j=q[h].j-1;
			b[q[h].i+2][q[h].j-1]=true;
		}
		if (q[h].i+2<n && q[h].j+1<n && b[q[h].i+2][q[h].j+1]==false){
			q[r].p=h;
			q[r].i=q[h].i+2;
			q[r++].j=q[h].j+1;
			b[q[h].i+2][q[h].j+1]=true;
		}
		if (q[h].i+1<n && q[h].j+2<n && b[q[h].i+1][q[h].j+2]==false){
			q[r].p=h;
			q[r].i=q[h].i+1;
			q[r++].j=q[h].j+2;
			b[q[h].i+1][q[h].j+2]=true;
		}
	}
	for (i=h,s=0;q[i].p!=-1;i=q[i].p,s++) ;
}


int main(){
	int i,j,r,k;
	cin>>k;
	while(k--!=0){
		cin>>n;
		cin>>x0>>y0;
		cin>>x>>y;
		if ((x0==x && y0==y))
			cout<<0<<endl;
		else{
			for (i=0;i<n;i++)
				for (j=0;j<n;j++)
					b[i][j]=false;
			s=0;r=0;
			q[r].p=-1;
			q[r].i=x0;
			q[r++].j=y0;
			b[x0][y0]=true;
			BFS(r);
			cout<<s<<endl;
		}
	}
	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