poj 1729 Jack and Jill BFS嵌套,同时进行两个

Description 
Ever since the incident on the hill, Jack and Jill dislike each other and wish to remain as distant as possible. Jack and Jill must attend school each day; Jack attends a boys’ school while Jill attends a girls’ school. Both schools start at the same time. You have been retained by their lawyers to arrange routes and a schedule that Jack and Jill will adhere to so as to maximize the closest straight-line distance between them at any time during their trip to school. 
Jack and Jill live in a town laid out as an n by n square grid (n <= 30). It takes 1 minute to walk from one location to an adjacent location. In maximizing the distance between Jack and Jill you need consider only the distance between the locations they visit (i.e. you need not consider any intermediate points on the path they take from grid location to grid location). Some locations are impassable due to being occupied by rivers, buildings, etc. Jack must start at his house and walk continuously until he gets to school. Jill must start at her house at the same time as Jack and walk continuously until she arrives at her school. Jack’s house and school are impassable to Jill while Jill’s house and school are impassable to Jack. Other grid locations that are impassable to both Jack and Jill are given in the input. 
Input 
Input will consist of several test cases. Each test case will consist of n, followed by n lines with n characters representing a map of the town. In the map, Jack’s house is represented by ‘H’, Jack’s school is represented by ‘S’, Jill’s house is represented by ‘h’, Jill’s school is represented by ‘s’, impassable locations are represented by ‘*’, and all other locations are represented by ‘.’ You may assume the normal cartographic convention that North is at the top of the page and West is to the left. A line containing 0 follows the last case. 
Output 
For each input case you should give three lines of output containing: 
the closest that Jack and Jill come during the schedule (to 2 decimal places) 
Jack’s route 
Jill’s route.

Each route is a sequence of directions that Jack or Jill should follow for each minute from the start time until arriving at school. Each direction is one of ‘N’, ‘S’, ‘E’, or ‘W’. If several pairs of routes are possible, any one will do. You may assume there is at least one solution. Leave a blank line between the output for successive cases. 
Sample Input 
10 
………. 
…H…… 
.**…s… 
.**……. 
.**……. 
.**……. 
.**……. 
.**……. 
…S..h..* 
………. 

Sample Output 
6.71 
WWWSSSSSSSEEE 
NEEENNNNNWWW


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<stack>
#include<iostream>
#include<queue>
#define Min(a,b) a<b?a:b
using namespace std;
struct node
{
    int hx,hy;
    int Hx,Hy;
    int dist;
    char moveh,moveH;
    int id,pre;
    bool operator < (const node& a) const
    {
        return dist<a.dist;
    }
};
int dirx[]={0,-1,0,1};
int diry[]={1,0,-1,0};
char move[]="ENWS";
int vis[50][50][50][50];
int n;
int p;
node st[1000010];
char g[110][110];
priority_queue<node> que;
int dist(int x1,int y1,int x2,int y2)
{
    return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
void BFS(int hx,int hy,int Hx,int Hy)
{
    while(!que.empty())
        que.pop();
    int i,j;
    memset(vis,-1,sizeof(vis));
    node x,y;
    p=0;
    x.hx=hx,x.hy=hy,x.Hx=Hx,x.Hy=Hy;
    x.id=p;
    x.pre=-1;
    int dis=dist(hx,hy,Hx,Hy);
    x.dist=dis;
    st[p++]=x;
    que.push(x);
    while(!que.empty())
    {
        y=que.top();
        que.pop();
        if(g[y.hx][y.hy]=='s'&&g[y.Hx][y.Hy]=='S')
        {
            printf("%.2f\n",sqrt(1.0*y.dist));
            stack<char> st1,st2;
            while(y.pre!=-1)
            {
                if(y.moveH!='e')
                    st1.push(y.moveH);
                if(y.moveh!='e')
                    st2.push(y.moveh);
                y=st[y.pre];
            }
            while(!st1.empty())
            {
                printf("%c",st1.top());
                st1.pop();
            }
            puts("");
            while(!st2.empty())
            {
                printf("%c",st2.top());
                st2.pop();
            }
            puts("");
            break;
        }
        for(i=0;i<4;i++)
        {
            int a=y.hx+dirx[i];
            int b=y.hy+diry[i];
            char moveh=move[i];
            if(g[y.hx][y.hy]=='s')
                a=y.hx,b=y.hy,moveh='e';
            if(a>=0&&a<n&&b>=0&&b<n&&g[a][b]!='*'&&g[a][b]!='S'&&g[a][b]!='H')
            {
                for(j=0;j<4;j++)
                {
                    int c=y.Hx+dirx[j];
                    int d=y.Hy+diry[j];
                    char moveH=move[j];
                    if(g[y.Hx][y.Hy]=='S')
                        c=y.Hx,d=y.Hy,moveH='e';
                    if(c>=0&&c<n&&d>=0&&d<n&&g[c][d]!='*'&&g[c][d]!='s'&&g[c][d]!='h')
                    {
                        dis=dist(a,b,c,d);
                        dis=Min(dis,y.dist);
                        if(dis>vis[a][b][c][d]||vis[a][b][c][d]==-1)
                        {
                            x.hx=a,x.hy=b,x.Hx=c,x.Hy=d;
                            x.id=p;
                            x.dist=dis;
                            x.pre=y.id;
                            x.moveh=moveh;
                            x.moveH=moveH;
                            st[p++]=x;
                            que.push(x);
                            vis[a][b][c][d]=dis;
                        }
                    }
                    if(g[y.Hx][y.Hy]=='S')
                        break;
                }
            }
            if(g[y.hx][y.hy]=='s')
                break;
        }
    }
}
int i,j;
int hx,hy,Hx,Hy;
int main()
{
    while(scanf("%d",&n)&&n)
    {
        for(i=0;i<n;i++)
        {
            scanf(" %s",g[i]);
            for(j=0;j<n;j++)
                if(g[i][j]=='h')
                    hx=i,hy=j;
                else
                    if(g[i][j]=='H')
                        Hx=i,Hy=j;
        }
        BFS(hx,hy,Hx,Hy);
       
    }
   
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值