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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值