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 lujunda at 2012-01-29 14:44:44 on Problem 1915
#include<iostream>
#include<queue>
#include<cstring> 
using namespace std;
bool used[300][300];
int l;
struct point
{
       int x;
       int y;
};

bool pd(int x,int y)
{
     if(x>=0 && x<l && y>=0 && y<l)
          return true;
     return false;
}

int BFS(int x1,int y1,int x2,int y2)
{
    for(int i=0;i<300;i++)
            for(int j=0;j<300;j++)
                    used[i][j] = false;
    int s=0;
    queue<point> a;
    struct point p;
    p.x = x1;
    p.y = y1;
    a.push(p);
    used[x1][y2]=true; 
    while(true)
    {
               int temp=a.size();
               while(temp--)
               {
                            struct point head=a.front();
                            if(head.x == x2 && head.y == y2)
                            return s;
                            p.x = head.x-2;
                            p.y = head.y-1;
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            p.x = head.x-1;
                            p.y = head.y-2; 
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            p.x = head.x-2;
                            p.y = head.y+1;
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            p.x = head.x-1;
                            p.y = head.y+2;
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            p.x = head.x+2;
                            p.y = head.y+1;
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            p.x = head.x+1;
                            p.y = head.y+2;
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            p.x = head.x+2;
                            p.y = head.y-1;
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            p.x = head.x+1;
                            p.y = head.y-2;
                            if(pd(p.x,p.y) && !used[p.x][p.y]) 
                            {
                                                        a.push(p);
                                                        used[p.x][p.y]=true;
                            }
                            a.pop();
               }
               s++;
    }
}
int main()
{
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    int x1,y1,x2,y2,t;
    for(cin>>t;t>0;t--)
    {
                       scanf("%d%d%d%d%d",&l,&x1,&y1,&x2,&y2);
                       cout<<BFS(x1,y1,x2,y2)<<endl;
    }
}

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