ACdream P1242 Driving Straight
来源:互联网 发布:mac air怎么重装系统 编辑:程序博客网 时间:2024/06/14 10:40
Driving Straight
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
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
Sample Input
4 4+-+ +-+| | |+ +-+-+| |+-+-+-+| |+-+-+-+
Sample Output
NRFLRL
Source
广搜题目,从终点逆推,每个点每个方向是否是最短路都可以标记出来,有不少细节需要自己处理好,比如矩阵化图,标记已经搜过的点等。总的来说就是一道水题。
代码:
#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;}
其实码完才发现不用化图,本来化图是为了可能可以套什么模板,没想到直接可以做了。
- ACdream P1242 Driving Straight
- acdream 1242 Driving Straight bfs
- Andrew Stankevich's Contest (4) H - Driving Straight bfs
- Andrew Stankevich Contest 4——Driving Straight
- ACdream
- Acdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- 汉诺塔——洛谷P1242 新汉诺塔
- svn 团队开发
- [ios]获取app版本更新检测的不同语言信息
- 查看系统 是否存在绑定变量问题
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- 16进制字符串 以前丛 16进制字符串转成数据
- ACdream P1242 Driving Straight
- php遍历目录的四种方法
- 日语学习之沪江N4基础 20141201 -6
- lua中math
- 选择屏幕相关
- Android百日程序:Intent传递数据
- WIFI的基础知识的了解
- 有限域GF(2^8)的四则运算及拉格朗日插值
- 服务器不使用默认80端口时wordpress的设置