ACdream P1242 Driving Straight

来源:互联网 发布:mac air怎么重装系统 编辑:程序博客网 时间:2024/06/14 10:40

Driving Straight

Special JudgeTime Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
SubmitStatisticNext Problem

Problem Description

      The city where Peter lives has the form of the rectangle with M streets running from east to west and N streets running from north to south. Recently, because of preparations for the celebration of the city’s 3141-st birthday, some street sectors has been closed for driving.

      Peter lives in the house next to point (1, 1) and works next to the point (N, M ). He always drives from home to work by his car using the shortest possible path. Of course, he can’t drive through closed sectors. Since there can be many shortest pathes between his house and his work, he may choose any. 

      But Peter doesn’t like to turn (he is an inexperienced driver), so he wants to choose the path using the following algorithm: starting from the point (1, 1) he drives either northwards, or eastwards (wherever there is the shortest path available, if there are both, he may choose any). Whenever he comes to the junction he must decide where to go. If there is only one direction he can drive to stay on the shortest path, he must choose that direction. In the other case he would like to choose the direction giving priority to driving forward, that is, if he can drive forward and still stay on some shortest path, he would go forward. If he can’t drive forward to stay on the shortest path, he would choose any available direction. 

      Help Peter to create the path from his house to his work using the rules described.

Input

      The first line of the input file contains integer numbers M and N (2 ≤ M, N ≤ 400).
      Next 2M − 1 lines contain 2N − 1 characters each, representing the city map. House blocks are marked with spaces, junctions with ‘+’, open sectors with ‘-’ and ’|’, closed sectors with spaces. Peter’s house is at the lower-left corner of the map, his work is at the upper-right corner.

Output

      On the first line of the output file print the direction Peter should choose first, ‘N’ or ‘E’. On the second line output the sequence of latin letters ‘F’, ‘L’ and ‘R’ representing Peter’s behaivour on junctions — go forward, turn left or right respectively. If there are several paths Peter can choose from, output any. You must output Peter’s action on the junction even if he has no choice due to closed streets. It is guaranteed that there will always be the way for Peter to get from home to work.

Sample Input

4 4+-+ +-+| |   |+ +-+-+|   |+-+-+-+|     |+-+-+-+

Sample Output

NRFLRL

Source

Andrew Stankevich Contest 4

广搜题目,从终点逆推,每个点每个方向是否是最短路都可以标记出来,有不少细节需要自己处理好,比如矩阵化图,标记已经搜过的点等。总的来说就是一道水题。


代码:

#include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>#include<string>#include<cstring>#include<algorithm>#include<fstream>#include<queue>#include<stack> #include<vector>#include<cmath>#include<iomanip>#define rep(i,n) for(i=1;i<=n;i++)#define MM(a,t) memset(a,t,sizeof(a))#define INF 1e9typedef long long ll;#define mod 1000000007using namespace std;int edge[700000],nxt[700000],fir[200000],lst[200000];int n,m,n2,ST,TR,vis[200000];bool f[200000][4];queue<int> qu;void addedge(int s,int e){  edge[++n2]=e;  if(fir[s]==-1){fir[s]=n2; lst[s]=n2;}  else          {nxt[lst[s]]=n2; lst[s]=n2;}}int getnode(int x,int y){  x=(x+1)/2;  y=(y+1)/2;  return (x-1)*m+y;} int getdir(int s,int e){  if(s==e+1) return 0;  if(s==e+m) return 1;  if(s==e-1) return 2;  if(s==e-m) return 3;}int getnext(int x,int dir){  switch(dir){    case 0:return x-1;case 1:return x-m;case 2:return x+1;case 3:return x+m;  }}int main(){int i,j,dir,len;char st[1000];    while(scanf("%d%d",&n,&m)!=EOF){     n2=0; MM(fir,-1); MM(nxt,-1); MM(lst,-1); MM(vis,0);      ST=getnode(2*n-1,1); TR=getnode(1,2*m-1); MM(f,0);      getchar();      for(i=1;i<=2*n-1;i++){      cin.getline(st,2*m);      len=strlen(st);        if(len!=2*m-1){          for(j=len;j<2*m-1;j++) st[j]=' ';  st[2*m-1]='\0';        }       if(i%2){         int s,e;    for(j=1;j<=2*(m-1)-1;j+=2)  if(st[j]!=' '){      s=getnode(i,j); e=getnode(i,j+2);addedge(s,e);addedge(e,s);    }            }        else{       int s,e;    for(j=0;j<=2*(m-1);j+=2)  if(st[j]!=' '){      s=getnode(i-1,j+1); e=getnode(i+1,j+1);addedge(s,e);addedge(e,s);    }        }  }//作图   while(!qu.empty()) qu.pop();  qu.push(TR);  vis[TR]=1;  while(!qu.empty()){      int s,e=qu.front(); qu.pop();      if(e==ST) break;for(i=fir[e];i!=-1;i=nxt[i]){  s=edge[i];  if(vis[s]==2) continue;  f[s][getdir(s,e)]=1;  if(!vis[s]){            vis[s]=1;            qu.push(s);    }}vis[e]=2;    }  if(f[ST][1]){      printf("N\n");ST-=m; dir=1;    }    else{      printf("E\n");ST+=1; dir=2;    }    while(ST!=TR){      if(f[ST][dir]){      printf("F");  ST=getnext(ST,dir);    continue;      }      int l=(dir+3)%4,r=(dir+5)%4;      if(f[ST][l]){      printf("L");      dir=l;  ST=getnext(ST,l);       }     else{      printf("R");      dir=r;  ST=getnext(ST,r);        }    }    printf("\n");    }return 0;}

其实码完才发现不用化图,本来化图是为了可能可以套什么模板,没想到直接可以做了。





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刚刚开的淘宝企业店没有流量怎么办 有图片怎么在淘宝找不到商品怎么办 白色高跟鞋鞋面磨脏了怎么办弄干净 我跟鞋后跟磨烂的脚怎么办? 内衣买回来有一股刺鼻的味道怎么办 裁剪袖子开口偏了1公分怎么办 对于这乱扔垃圾不听劝者该怎么办 感觉被南通蒲公英店铺骗了怎么办 6个月宝宝吃米粉不吃奶怎么办 十个月的宝宝不吃辅食怎么办 5个月宝宝拉肚子有泡沫怎么办 生完孩子后皮肤暗黄怎么办 开服装店批发服装的吊牌怎么办 天虹的鞋一天就坏了怎么办 车被钥匙划了露底漆了怎么办 数控铣z轴回不了参考点怎么办 白色衣服被黑色衣服染了怎么办 夏天出汗衣服粘身上都是毛毛怎么办 支付宝租的手机坏了怎么办 新买的衣服布料扎人怎么办 洗衣服的时候卫生纸沾裤子上怎么办 洗衣服给白衣服染上色了怎么办 不小心喝了游泳池的水怎么办 铁水中硅的含量高了怎么办 视频拍摄单人变双人是怎么办的 四季青进来的货比淘宝还贵怎么办 淘宝还没收货价格买贵了怎么办 在微信上赌博庄跑了怎么办 微信赌博输了10000多怎么办 欠了信用卡说来来家里调查怎么办 没用过的超市购物卡丢了怎么办 体验服抢号成功手机号填错了怎么办 起亚kx3一键启动钥匙没电怎么办 逆战下载的时候显示文件损坏怎么办 移动公司买手机送话费套路怎么办 开通京东白条身份信息被占用怎么办 丰巢快递柜把东西寄丢了怎么办? 圆通快递把我寄的东西弄丢了怎么办 快递把我寄出去的东西弄丢了怎么办 京东被盗刷都是到付怎么办 订机票时护照号错了怎么办