###### POJ 1729 Jack ans Jill（bfs）

//
//  main.cpp
//  Richard
//
//  Created by 邵金杰 on 16/8/31.
//

#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;
}

#### poj 1729 Jack and Jill （比较有特色的bfs）

2016-08-30 22:53:05

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

2014-02-19 15:32:12

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

2015-08-29 20:53:26

#### poj1729(bfs+优先队列优化）

2017-07-29 21:20:17

#### POJ 1729 Jack and Jill BFS *

2016-09-12 09:19:12

#### [Android]Jack和Jill的阴暗面

2016-05-04 15:35:27

#### CD(二分)

2013-06-01 17:10:13

#### Android 新一代编译 toolchain Jack & Jill 简介

2016-12-01 11:56:25

#### HDU 3763 C D（二分）

2017-11-18 17:01:12

#### HDU3763：CD

2013-06-01 20:29:46

## 不良信息举报

POJ 1729 Jack ans Jill（bfs）