int bfs()
{
    int i;
    head=0;
    tail=0;
    que[head].x=si;
    que[head].y=sf;
    que[head].time=0;
    hash[si][sf]=1;
    srh(si,sf,0);
    while(head<=tail)
    {
        tem=que[head];
        head++;
        if(tem.x==ei&&tem.y==ef)
        {
            return tem.time;
        }
        for(i=0;i<4;i++)
        {
            if(1==hash[tem.x+dir[i][0]][tem.y+dir[i][1]])
            {
                continue;
            }
            hash[tem.x+dir[i][0]][tem.y+dir[i][1]]=1;
            tail++;
            que[tail].x=tem.x+dir[i][0];
            que[tail].y=tem.y+dir[i][1];
            que[tail].time=tem.time+1;
            srh(tem.x+dir[i][0],tem.y+dir[i][1],tem.time+1);
        }
    }
    return -1;
}
Exemple #2
0
int srh(node t, str s) {
    if (t == NULL)
        return 0;
    else {
        if (s == t->s)
            return 1;
        else if (s < t->s)
            return srh(t->l, s);
        else
            return srh(t->r, s);
    }
}
Exemple #3
0
int search(tree t, str s) {
    return srh(t->root, s);
}