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

很神奇的 用队列哪里错了呢?关于队列的细节问题!

Posted by dowork at 2010-04-08 21:12:22 on Problem 1915
#include <iostream>
#include <algorithm>
#include<queue>
using namespace std;
int array[400][400],vis[400][400];
int move[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-2,-1},{-1,-2}};
int main()
{
	int i,n,t,x1,y1,x2,y2,x,y;
	while(scanf("%d",&t)!=-1)
	{
		while(t--)
		{
			queue<int> qu;
			while(!qu.empty())
				qu.pop();
			scanf("%d",&n);
			scanf("%d%d",&x1,&y1);
			scanf("%d%d",&x2,&y2);
			qu.push(x1);
			qu.push(y1);		
			memset(array,0,sizeof(array));
			memset(vis,0,sizeof(vis));
			array[x1][y1]=0;
			while(!qu.empty())
			{
				x=qu.front();
				qu.pop();
				y=qu.front();
				qu.pop();
				if(x==x2 && y==y2)
					break;
				for(i=0;i<8;i++)
				{
					if((x+move[i][0]>=0)&&(x+move[i][0]<n)&&(y+move[i][0]>=0)&&(y+move[i][1]<n)&&vis[x+move[i][0]][y+move[i][1]]==0)
					{
						qu.push(x+move[i][0]);
						qu.push(y+move[i][1]);
						array[x+move[i][0]][y+move[i][1]]=array[x][y]+1;
						vis[x+move[i][0]][y+move[i][1]]=1;
					}
				}
			}
			cout<<array[x][y]<<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