您的位置:首页 > 其它

UVA - 1601(双向BFS)

2014-10-10 13:49 288 查看
使用双向BFS最大的优点在于 是的结点扩展由 a^(x)变为2*a^(x/2),降低了时间复杂度;

适用于(起始状态和末尾状态都已知);

方法从两端交替逐层搜索(为了保证最短路经优先被找到)

这里借用别人的对错误的搜索方式的解释

如果目标也已知的话,用双向BFS能很大提高速度
单向时,是 b^len的扩展。
双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时
至于实现上,网上有些做法是用两个队列,交替节点搜索 ×,如下面的伪代码:

while(!empty()){
扩展正向一个节点
遇到反向已经扩展的return
扩展反向一个节点
遇到正向已经扩展的return
}
但这种做法是有问题的,如下面的图:



求S-T的最短路,交替节点搜索(一次正向节点,一次反向节点)时
Step 1 : S –> 1 , 2
Step 2 : T –> 3 , 4
Step 3 : 1 –> 5
Step 4 : 3 –> 5 返回最短路为4,错误的,事实是3,S-2-4-T

uva - 1601 (双搜的试验田)

#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#include <iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<=n;i++)
const int N    = 17;
const int maxn = 200;
const int dx[] = {1,0,-1,0,0};
const int dy[] = {0,-1,0,1,0};
int n,m,k;
char maze

;
int x[maxn],y[maxn],cnt,G[maxn][5],deg[maxn],id[maxn][maxn];
//dig out all of the space position so that each position can be replace by an ID number;
int s[3],t[3];
int d[maxn][maxn][maxn],bd[maxn][maxn][maxn];

int ID(int a,int b,int c)
{
    return (a<<16)|(b<<8)|c;
}
void Memset(){
    rep(i,cnt)rep(j,cnt)rep(k,cnt) {
        d[i][j][k]=-1; bd[i][j][k]=-1;
    }
}
bool conflict(int a,int b,int na,int nb)
{
    if(a==nb&&b==na) return true;
    if(na==nb) return true;
    return false;
}
queue<int> q;
bool bfs()
{
    int value;
    int uu=q.front();
    int aa=(uu>>16)&0xff,bb=(uu>>8)&0xff,cc=uu&0xff;
    value=d[aa][bb][cc];
    //cout<<"front"<<"-->"<<value<<endl;
    while(!q.empty())
    {
        int u=q.front();
        int a=(u>>16)&0xff,b=(u>>8)&0xff,c=u&0xff;
        if(d[a][b][c]!=value) return false;
        if(bd[a][b][c]!=-1) return true;
        //if(a==t[0]&&b==t[1]&&c==t[2]) return d[a][b][c];
        for(int i=0; i<deg[a]; i++)
        {
            int na=G[a][i];
            for(int j=0; j<deg[b]; j++)
            {
                int nb=G[b][j];
                for(int k=0; k<deg[c]; k++)
                {
                    int nc=G[c][k];
                    if(conflict(a,b,na,nb)) continue;
                    if(conflict(a,c,na,nc)) continue;
                    if(conflict(b,c,nb,nc)) continue;
                    if(d[na][nb][nc]==-1)
                    {
                        d[na][nb][nc]=d[a][b][c]+1;
                        q.push(ID(na,nb,nc));
                    }
                }
            }
        }
        q.pop();
    }
    return false;
}
queue<int> p;
int back_bfs(){
    int value;
    int uu=p.front();
    int aa=(uu>>16)&0xff,bb=(uu>>8)&0xff,cc=uu&0xff;
    value=bd[aa][bb][cc];
    //cout<<"back"<<"-->"<<value<<endl;
    while(!p.empty())
    {
        int u=p.front();
        int a=(u>>16)&0xff,b=(u>>8)&0xff,c=u&0xff;
        if(bd[a][b][c]!=value) return false;
        if(d[a][b][c]!=-1) return true;
        //if(a==t[0]&&b==t[1]&&c==t[2]) return d[a][b][c];
        for(int i=0; i<deg[a]; i++)
        {
            int na=G[a][i];
            for(int j=0; j<deg[b]; j++)
            {
                int nb=G[b][j];
                for(int k=0; k<deg[c]; k++)
                {
                    int nc=G[c][k];
                    if(conflict(a,b,na,nb)) continue;
                    if(conflict(a,c,na,nc)) continue;
                    if(conflict(b,c,nb,nc)) continue;
                    if(bd[na][nb][nc]==-1)
                    {
                        bd[na][nb][nc]=bd[a][b][c]+1;
                        p.push(ID(na,nb,nc));
                    }
                }
            }
        }
        p.pop();
    }
    return false;
}
int BFS(){
while(!q.empty()) q.pop();
while(!p.empty()) p.pop();
q.push(ID(s[0],s[1],s[2]));
p.push(ID(t[0],t[1],t[2]));
Memset();
d[s[0]][s[1]][s[2]]=0; bd[t[0]][t[1]][t[2]]=0;
int step=0,ok=0;
while(!p.empty()&&!q.empty()){
     if(bfs()){
         ok=1;   break;
     } else step++;
     if(back_bfs()){
         ok=1;   break;
     } else step++;
}
return ok ? step:-1;
}
int main()
{
    while(scanf("%d %d %d",&m,&n,&k)==3)
    {
        if(!n&&!m&&!k) break;
        gets(maze[0]);
        cnt=0;
        for(int i=0; i<n; i++)
        {
            gets(maze[i]);
            //cout<<maze[i]<<endl;
            for(int j=0; j<m; j++)
            {
                if(maze[i][j]!='#')
                {
                    x[cnt]=i;
                    y[cnt]=j;
                    id[i][j]=cnt;
                    if(islower(maze[i][j]))
                    {
                        s[maze[i][j]-'a']=cnt;
                    }
                    if(isupper(maze[i][j]))
                    {
                        t[maze[i][j]-'A']=cnt;
                    }
                    cnt++;
                }
            }
        }

        for(int i=0; i<cnt; i++)
        {
            deg[i]=0;
            //cout<<x[i]<<"-->"<<y[i]<<endl;
            for(int j=0; j<5; j++)
            {
                int nx=x[i]+dx[j],ny=y[i]+dy[j];
                if(maze[nx][ny]!='#')
                {
                    G[i][deg[i]++]=id[nx][ny];
                    //cout<<nx<<" "<<ny<<endl;
                }
            }
        }
        if(k<=2)
        {
            deg[cnt]=1;
            G[cnt][0]=cnt;
            s[2]=t[2]=cnt++;
        }
        //add fakes in order to shorter the code length;
        if(k<=1)
        {
            deg[cnt]=1;
            G[cnt][0]=cnt;
            s[1]=t[1]=cnt++;
        }
        printf("%d\n",BFS());
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: