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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

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
  • 1563

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

Description Ever since the incident on the hill, Jack and Jill dislike each other and wish to remai...
  • carryheart
  • carryheart
  • 2016年08月30日 22:53
  • 696

poj1729(bfs+优先队列优化)

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

POJ 1729 Jack and Jill BFS *

题目地址:http://poj.org/problem?id=1729 本来想用string保存路程 后来发现太慢了 优先队列要以一条路中最近的距离排序 #include #include #i...
  • qq_34446253
  • qq_34446253
  • 2016年09月12日 09:19
  • 212

POJ 1729 Jack ans Jill(bfs)

每次扩展最多可以扩展出16个结点,搞清楚这个就可以了,其它理解不难。 不过自己想还是想不出来,看的时候不下看懂了,还是要多想啊。 // // main.cpp // Richard // //...
  • qq_33901573
  • qq_33901573
  • 2016年08月31日 16:23
  • 155

C++ 学习笔记10

Design and implement a simple spell checker.   许多常用字处理软件都内置了拼写检查程序。请设计和实现自己的拼写检查程序。可以在程序中建立字典文件。...
  • jj12345jj198999
  • jj12345jj198999
  • 2011年07月14日 10:53
  • 40623

算法设计手册(第2版)读书笔记, Springer - The Algorithm Design Manual, 2ed Steven S.Skiena 2008

The Algorithm Design Manual, 2ed 跳转至: 导航、 搜索 Springer - The Algorithm Design Manual, 2ed...
  • cteng
  • cteng
  • 2014年10月07日 16:18
  • 3679

[Android]Jack和Jill的阴暗面

Jack & Jill Android新编译工具链
  • lihenair
  • lihenair
  • 2016年05月04日 15:35
  • 4975

比较有特色的slogan

MONO:用有趣的方式看世界 陌陌:总有新奇在身边 携程:携程在手,说走就走 今日头条:你关心的才是头条 爱奇艺视频:悦享品质 微博:随时随地,发现新鲜事 京东:多快好省 in:我的...
  • dmeveryday
  • dmeveryday
  • 2017年04月21日 14:59
  • 298

hdu 3763 CD(二分)

CD Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm...
  • sungaochao
  • sungaochao
  • 2015年01月09日 18:14
  • 290
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 1729 Jack and Jill (比较有特色的bfs)
举报原因:
原因补充:

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