Andrew Stankevich's Contest (4) H - Driving Straight bfs

H - Driving Straight

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)     Special Judge

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

N
RFLRL

題意:求最短路,要求儘量走直線,輸出路徑。

思路:從終點向起點bfs,然後找回去。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <queue>
#include <map>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1005;
int n,m;
struct node
{
    int x,y;
};
queue<node> que;
char mp[N][N];
int dp[N][N];
bool vis[N][N];
void bfs(){
    node tmp;
    tmp.x=0;
    tmp.y=m-1;
    que.push(tmp);
    memset(vis,0,sizeof(vis));
    memset(dp,inf,sizeof(dp));
    dp[0][m-1]=0;
    while(!que.empty()){
        tmp=que.front();
        que.pop();
        int x=tmp.x;
        int y=tmp.y;
        int step=dp[x][y];
        if(x==n-1&&y==0)break;
        if(vis[x][y]==1)continue;
        vis[x][y]=1;
        if(y>1&&(mp[x][y-1]=='-'&&mp[x][y-2]=='+')){
            dp[x][y-2]=min(dp[x][y-2],step+1);
            tmp.y-=2;
            que.push(tmp);
            tmp.y+=2;
        }
        if(y<m-2&&(mp[x][y+1]=='-'&&mp[x][y+2]=='+')){
            dp[x][y+2]=min(dp[x][y+2],step+1);
            tmp.y+=2;
            que.push(tmp);
            tmp.y-=2;
        }
        if(x<n-2&&(mp[x+1][y]=='|'&&mp[x+2][y]=='+')){
            dp[x+2][y]=min(dp[x+2][y],step+1);
            tmp.x+=2;
            que.push(tmp);
            tmp.x-=2;
        }
        if(x>1&&(mp[x-1][y]=='|'&&mp[x-2][y]=='+')){
            dp[x-2][y]=min(dp[x-2][y],step+1);
            tmp.x-=2;
            que.push(tmp);
            tmp.x+=2;
        }
    }
}
void get(){
    int x=n-1,y=0;
    int pre=-1;
    int xx,yy;
    int ansx,ansy,ansp;
    int st=0;
    while(dp[x][y]!=0){
        int flag=0;
        xx=x-2;
        yy=y;
        if(xx>=0&&dp[xx][yy]==dp[x][y]-1&&mp[x-1][y]=='|'){
            ansx=xx,ansy=yy,ansp=3;
            flag=1;
        }
        xx=x+2;
        yy=y;
        if(xx<n&&dp[xx][yy]==dp[x][y]-1&&mp[x+1][y]=='|'){
            if(!flag||(flag&&pre==1)){
            ansx=xx,ansy=yy,ansp=1;
            flag=1;
            }
        }
        xx=x;
        yy=y-2;
        if(yy>=0&&dp[xx][yy]==dp[x][y]-1&&mp[x][y-1]=='-'){
            if(!flag||(flag&&pre==2)){
            ansx=xx,ansy=yy,ansp=2;
            flag=1;
            }
        }
        xx=x;
        yy=y+2;
        if(yy<m&&dp[xx][yy]==dp[x][y]-1&&mp[x][y+1]=='-'){
            if((!flag||(flag&&pre==4))){
            ansx=xx,ansy=yy,ansp=4;
            }
        }
        x=ansx,y=ansy;
        if(st==0&&x==n-3&&y==0)printf("N\n");
        else if(st==0&&x==n-1&&y==2)printf("E\n");
        else if(pre==ansp+1||(pre==1&&ansp==4))printf("L");
        else if(pre==ansp-1||(pre==4&&ansp==1))printf("R");
        else printf("F");
        pre=ansp;
        st++;
    }
    printf("\n");
}
int main()
{
    scanf("%d%d",&n,&m);
    n=n*2-1;
    m=m*2-1;
    getchar();
    for(int i=0;i<n;i++)gets(mp[i]);
    bfs();
    get();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章