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

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
• 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
• 2016年08月30日 22:53
• 696

poj1729(bfs+优先队列优化）

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

POJ 1729 Jack and Jill BFS *

• qq_34446253
• 2016年09月12日 09:19
• 212

POJ 1729 Jack ans Jill（bfs）

• qq_33901573
• 2016年08月31日 16:23
• 155

C++ 学习笔记10

Design and implement a simple spell checker.   许多常用字处理软件都内置了拼写检查程序。请设计和实现自己的拼写检查程序。可以在程序中建立字典文件。...
• 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
• 2014年10月07日 16:18
• 3679

[Android]Jack和Jill的阴暗面

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

比较有特色的slogan

MONO：用有趣的方式看世界 陌陌：总有新奇在身边 携程：携程在手，说走就走 今日头条：你关心的才是头条 爱奇艺视频：悦享品质 微博：随时随地，发现新鲜事 京东：多快好省 in：我的...
• 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
• 2015年01月09日 18:14
• 290

举报原因： 您举报文章：poj 1729 Jack and Jill （比较有特色的bfs） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)