Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
3100K,375ms~~~,不知道大牛们几K、0ms是怎么做到的。。。。。。#include<iostream> using namespace std; const int MaxSize=9999999; class SeqQueue { public: bool IsEmpty() const { return front==rear; } bool Front(int &x,int &y) const; bool EnQueue(int x,int y); bool DeQueue(); void Clear() { front=rear=0; } //private: int front,rear; int arr[MaxSize][2]; }queue; bool SeqQueue::Front(int &x,int &y) const { int temp=(front+1)%MaxSize; x=arr[temp][0]; y=arr[temp][1]; return true; } bool SeqQueue::EnQueue(int x,int y) { rear=(rear+1)%MaxSize; arr[rear][0]=x; arr[rear][1]=y; return true; } bool SeqQueue::DeQueue() { front=(front+1) % MaxSize; return true; } int N; int si,sj; int ei,ej; int ans; bool ChessBoard[300][300]; int Move[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; bool BFS(); int main() { //freopen("input.txt","r",stdin); int T; cin>>T; while(T--) { memset(ChessBoard,false,sizeof(ChessBoard)); cin>>N; cin>>si>>sj; cin>>ei>>ej; queue.Clear(); ans=0; queue.EnQueue(si,sj); while(!BFS()); cout<<ans<<endl; } return 0; } bool BFS() { int i,j; int t=queue.rear-queue.front; while(!queue.IsEmpty() && t--) { queue.Front(i,j); queue.DeQueue(); if(i<0 || i>=N || j<0 || j>=N) continue; if(ChessBoard[i][j]==true) continue; ChessBoard[i][j]=true; if(i==ei && j==ej) return true; int k; for(k=0;k<8;k++) { if(i+Move[k][0]<0 || i+Move[k][0]>=N || j+Move[k][1]<0 || j+Move[k][1]>=N) continue; if(ChessBoard[i+Move[k][0]][j+Move[k][1]]==true) continue; queue.EnQueue(i+Move[k][0],j+Move[k][1]); } }//end while ans++; return false; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator