POJ 1729 Jack ans Jill(bfs)

来源:互联网 发布:nginx加lua模块 编辑:程序博客网 时间:2024/05/24 06:07

每次扩展最多可以扩展出16个结点,搞清楚这个就可以了,其它理解不难。

不过自己想还是想不出来,看的时候不下看懂了,还是要多想啊。

////  main.cpp//  Richard////  Created by 邵金杰 on 16/8/31.//  Copyright © 2016年 邵金杰. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>#include<algorithm>using namespace std;const int N=30+5;const int maxn=1000000+10;int vis[N][N][N][N];int dist(int x,int y){    return x*x+y*y;}int sx1,sy1,sx2,sy2,ex1,ey1,ex2,ey2;struct node{    int x1,y1,x2,y2,dis,d1,d2,pre,id;    node() {}    node(int x1,int y1,int x2,int y2,int dis): x1(x1),y1(y1),x2(x2),y2(y2),dis(dis) {}    bool check(){        if(check1()&&check2())            return true;        else            return false;    }    bool check1(){        if(x1==ex1&&y1==ey1)            return true;        else            return false;    }    bool check2(){        if(x2==ex2&&y2==ey2)            return true;        else            return false;    }    bool operator < (const node &p) const {        return dis<p.dis;    }}way[maxn];int n;char map[N][N];int dx[4]={-1,0,1,0};int dy[4]={0,-1,0,1};char dir[5]="NWSE";void bfs(){    priority_queue<node> pq;    memset(vis,-1,sizeof(vis));    int dis=dist(sx1-sx2,sy1-sy2);    node state(sx1,sy1,sx2,sy2,dis);    int cnt=1;    state.d1=-1;    state.d2=-1;    state.id=0;    state.pre=-1;    pq.push(state);    way[0]=state;    vis[sx1][sy1][sx2][sy2]=state.dis;    node now,next;    while(!pq.empty())    {        now=pq.top();        pq.pop();        if(now.check())        {            printf("%.2f\n",sqrt(1.0*now.dis));            string s1="",s2="";            node p=now;            while(p.pre!=-1)            {                if(p.d1!='*'){                    s1.push_back(p.d1);                }                if(p.d2!='*'){                    s2.push_back(p.d2);                }                p=way[p.pre];            }            reverse(s1.begin(),s1.end());            reverse(s2.begin(),s2.end());            cout<<s1<<endl;            cout<<s2<<endl;            return ;        }        for(int i=0;i<4;i++)        {            int nx1=now.x1+dx[i];            int ny1=now.y1+dy[i];            int d1=dir[i];            if(now.check1()){                nx1=now.x1;                ny1=now.y1;                d1='*';            }            if(!map[nx1][ny1]||map[nx1][ny1]=='#'||map[nx1][ny1]=='*') ;            else{                for(int j=0;j<4;j++)                {                    int nx2=now.x2+dx[j];                    int ny2=now.y2+dy[j];                    int d2=dir[j];                    if(now.check2()){                        nx2=now.x2;                        ny2=now.y2;                        d2='*';                    }                    if(!map[nx2][ny2]||map[nx2][ny2]=='$'||map[nx2][ny2]=='*') ;                    else{                        int ndis=dist(nx1-nx2,ny1-ny2);                        ndis=min(ndis,now.dis);                        int vv=vis[nx1][ny1][nx2][ny2];                        if(vv==-1||ndis>vis[nx1][ny1][nx2][ny2])                        {                            vis[nx1][ny1][nx2][ny2]=ndis;                            next.x1=nx1;next.y1=ny1;                            next.x2=nx2;next.y2=ny2;                            next.d1=d1;next.d2=d2;                            next.dis=ndis;                            next.pre=now.id;                            next.id=cnt;                            way[cnt++]=next;                            pq.push(next);                        }                    }                }            }        }    }}int main(){    while(scanf("%d",&n)&&n)    {        memset(map,0,sizeof(map));        for(int i=1;i<=n;i++)        {            scanf("%s",map[i]+1);            char *p;            p=strchr(map[i]+1,'H');            if(p)            {                sx1=i;                sy1=(int)(p-map[i]);                map[sx1][sy1]='$';            }            p=strchr(map[i]+1,'S');            if(p)            {                ex1=i;                ey1=(int)(p-map[i]);                map[ex1][ey1]='$';            }            p=strchr(map[i]+1,'h');            if(p)            {                sx2=i;                sy2=(int)(p-map[i]);                map[sx2][sy2]='#';            }            p=strchr(map[i]+1,'s');            if(p)            {                ex2=i;                ey2=(int)(p-map[i]);                map[ex2][ey2]='#';            }        }        bfs();    }    return 0;}


0 0