poj 1729 Jack and Jill (比较有特色的bfs)
来源:互联网 发布:赵照声律启蒙知乎 编辑:程序博客网 时间:2024/05/20 22:01
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:busing 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 (比较有特色的bfs)
- POJ 1729 Jack and Jill BFS *
- POJ 1729 Jack ans Jill(bfs)
- poj 1729 Jack and Jill BFS嵌套,同时进行两个
- poj 1729 Jack and Jill 1376 Robot 1324 Holedox Moving 1475 Pushing Boxes bfs + a*
- 比较有特色的slogan
- [Android]Jack和Jill的阴暗面
- POJ1729 Jack and Jill (双向广度优先搜索, DBFS)
- Android 新一代编译 toolchain Jack & Jill 简介
- Android 新一代编译 toolchain Jack & Jill 简介
- Android 新一代编译 toolchain Jack & Jill 简介
- Android最新编译工具Jack和Jill
- 几个有特色的软件
- unity3d的特色有哪些
- POJ 2627 Gopher and hawks(bfs)
- poj 1979 Red and Black(BFS)
- POJ 1979 Red and Black(BFS)
- poj 1127 Jack Straws(计算几何)
- Kafka系列之-Kafka监控工具KafkaOffsetMonitor配置及使用
- DOS->强制结束进程命令(ntsd命令/taskkill命令)使用教程
- HDU 1863 畅通工程(Kruskal算法)
- ccc 16 s4 Combining Riceballs
- centos7 如何用无线网卡连接wifi
- poj 1729 Jack and Jill (比较有特色的bfs)
- 栈的应用—行编辑程序
- 针对 android端模拟教务系统登陆,主要针对抓包过程,post,get请求,和解析网页和cookie(一)
- Caffe网络结构
- 特权级和调用门
- 单一职责原则
- TabLayout app:tabMode和app: tabGravity配合使用效果对比
- Java内部类详解
- 图中环长-Codeforces Round #369 Div.2-D