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

TLE[BFS]

Posted by hello1806 at 2009-02-22 18:03:09 on Problem 2243
#include <iostream>
#include <queue>
using namespace std;
struct x
{
    int i,j;
    int value;
};

int c,d;
int dir[8][2]={{-1,-2},{1,-2},{-1,2},{1,2},{-2,-1},{-2,1},{2,-1},{2,1}};
bool used[9][9];
bool success(struct x cur)
{
    if (cur.i==c&&cur.j==d) return true;
    return false;
}

bool legal(struct x next)
{
    if (next.i<=8&&next.i>=1&&next.j<=8&&next.j>=1) return true;
    return false;
}
int main()
{
    struct x tmp,next;
    char cmd1[15],cmd2[15],s[59];
    while (scanf("%s%s",cmd1,cmd2)!=EOF)
    {
        memset(used,false,sizeof(used));
        c=cmd2[0]-'a'+1;
        d=cmd2[1]-'0';
        tmp.i=cmd1[0]-'a'+1;
        tmp.j=cmd1[1]-'0';
        tmp.value=0;
        queue<struct x>Q;
        Q.push(tmp);
        while (! Q.empty())
        {
            tmp=Q.front();
            Q.pop();
            if (success(tmp)) break;
            used[tmp.i][tmp.j]=true;
            for (int i=0;i<8;i++)
            {
                next.i=tmp.i+dir[i][0];
                next.j=tmp.j+dir[i][1];
                if (legal(next)&&!used[next.i][next.j])
                {
                    next.value=tmp.value+1;
                    Q.push(next);
                }
            }
        }
        printf ("To get from %s to %s takes %d knight moves.\n",cmd1,cmd2,tmp.value);
    }
    return 0;
}
不知道为什么超时了....是出现了死循环 还是那句while (scanf("%s%s",cmd1,cmd2)!=EOF)有问题....
多谢

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