poj 1729 Jack and Jill (比较有特色的bfs)

原创 2016年08月30日 22:53:05

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..*
……….
0
Sample Output
6.71
WWWSSSSSSSEEE
NEEENNNNNWWW

案例图:
这里写图片描述

两个人分别从H到S,h到s。地图上*无法走,可以走到之前走的地方,到达s或者S后就停止。每一个单位时间走可上下左右走一步。求再各个单位时间的距离最短的路线(special judge)
思路:一个结构体保存两个人的状态,优先队列优化,bfs求解最优解。

code:

#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);
        //memset(g,0,sizeof(g));
    }
    /*priority_queue<node> q;
      for(int i=0;i<10;i++)
      {
      st[i].dist=i;
      q.push(st[i]);
      }
      while(!q.empty())
      {
      cout<<q.top().dist<<endl;
      q.pop();
      }*/
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

poj1729(bfs+优先队列优化)

Jack and Jill Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 1502   Accepted: 39...
  • update7
  • update7
  • 2017年07月29日 21:20
  • 17572

poj 1729 Jack and Jill 1376 Robot 1324 Holedox Moving 1475 Pushing Boxes bfs + a*

poj 1729 Jack and JillJack和Jill要从各自的家走到各自的学校,但是他们俩各自不喜欢对方,因此,需要你找到两个人行走的路线,使得他们路线中两个人最近的直线距离最长。单位时间内...
  • gwq5210
  • gwq5210
  • 2015年08月29日 20:53
  • 1476

POJ1729 Jack and Jill (双向广度优先搜索, DBFS)

loading
  • yew1eb
  • yew1eb
  • 2014年02月19日 15:32
  • 2359

poj1729(bfs+优先队列优化)

Jack and Jill Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 1502   Accepted: 39...
  • update7
  • update7
  • 2017年07月29日 21:20
  • 17572

POJ1729 Jack and Jill (双向广度优先搜索, DBFS)

loading
  • yew1eb
  • yew1eb
  • 2014年02月19日 15:32
  • 2359

POJ3026-Borg Maze【BFS+Prim】

  • 2011年07月30日 18:36
  • 12KB
  • 下载

POJ 1979 Red and Black(BFS)

题目网址:http://poj.org/problem?id=1979 题目: Language: DefaultEspañol Red and Black Time Limit:...

hdu 1312 poj 1979 Red and Black BFS

题目  http://poj.org/problem?id=1979     这是一个简单的BFS   刚开始调试老错了  对不上答案  很纠结  最后发现原来我的w 和 h搞反了哦   含纠结...

poj 1979Red and Black(BFS DFS)

一道简单的搜索题,和poj2386差不多,DFS和BFS都可以,当然并查集也可以…… Red and Black Time Limit: 1000MS   Memory...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 1729 Jack and Jill (比较有特色的bfs)
举报原因:
原因补充:

(最多只允许输入30个字)