BFS-Route Planning

题目描述

然而事情并不是Richard想的那么简单,等他登陆后他才发现,地图里有各种各样的炸弹和关卡。当Richard经过炸弹的时候,他需要花费1分钟的时间拆弹;地图中有关卡对应的通行证,如果Richard拿到了通行证,那么它就可以通过对应的关卡。现在请你重新为Richard规划一条路线,使执行任务的时间最短。

.代表空地

‘#’代表障碍

@代表出发点

*代表任务地点

=代表炸弹

1-4代表通行证

a-d代表关卡

输入

输入第一行为T,即有T组输入数据(1<=T<=10)

每组数据第一行为整数N 10≤N≤100,代表地图的长度和宽度

接下来为N行,每行N个字符,代表地图

输出

输出一个整数,为Richard从起点出发到达终点所需的最短时间。

如果Richard无法到达终点,请输出MISSION FAILED

每组输出占一行

样例输入

5
@….
.1…
…..
=a###
….*

样例输出

8


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define INF 0xfffffff

using namespace std;

typedef struct node{
    int x,y,t;
    int k,bomb;
}node;

int fmin(int a,int b){return a<b?a:b;}
int dirx[]={0,0,1,0,-1},diry[]={0,1,0,-1,0};
int bombx[10],bomby[10],bombl;
int n,TT;
bool mark[105][105][20][64];
char maze[105][105];

int which_bomb(int x,int y){
    for(int i=1;i<=bombl;++i)
        if(x==bombx[i] && y==bomby[i]) return i;
}

void bfs(node s){

    queue <node> q;
    mark[s.x][s.y][s.k][s.bomb]=true;

    q.push(s);
    while(!q.empty()){
        s=q.front();
        q.pop();
        if(s.t>TT)continue;
        if(maze[s.x][s.y]=='*'){
            TT =fmin(TT, s.t);
            continue ;
        }
        for(int i=1;i<=4;i++){
            node w=s;
            w.x+=dirx[i];
            w.y+=diry[i];
            w.t++;
            if(w.x<1 || w.x>n || w.y<1 || w.y>n || maze[w.x][w.y]=='#') continue;
            char c=maze[w.x][w.y];
            if(c=='='){
                int j=which_bomb(w.x,w.y);
                if((w.bomb&(1<<j))==0){
                    w.bomb |= (1<<j);
                    w.t++;
                }
                if(!mark[w.x][w.y][w.k][w.bomb]){
                    q.push(w);
                    mark[w.x][w.y][w.k][w.bomb]=true;
                }
            }
            else if(isdigit(c)){
                w.k |= (1<<(c-'1'));
                if(!mark[w.x][w.y][w.k][w.bomb]){
                    mark[w.x][w.y][w.k][w.bomb]=true;
                    q.push(w);
                }
            }
            else if(islower(c)){
                if((w.k & (1<<(c-'a'))) && (!mark[w.x][w.y][w.k][w.bomb])){
                   mark[w.x][w.y][w.k][w.bomb]=true;
                    q.push(w);
                }
            }
            else if(!mark[w.x][w.y][w.k][w.bomb]){
                mark[w.x][w.y][w.k][w.bomb]=true;
                q.push(w);
            }
        }
    }
}

int main()
{

    while(scanf("%d",&n)!=EOF){
        node s;
        memset(mark,false,sizeof(mark));
        bombl=0;
        for(int i=1;i<=n;i++){
            scanf("%s",&maze[i][1]);
            for(int j=1;j<=n;j++)
                if(maze[i][j]=='@') s.x=i,s.y=j;
                else if(maze[i][j]=='=') bombx[++bombl]=i,bomby[bombl]=j;
        }
        TT=INF;
        s.k=0;
        s.bomb=0;
        s.t=0;
        bfs(s);
        if(TT==INF )printf("MISSION FAILED\n");
        else printf("%d\n",TT);
    }
    return 0;
}
# CRP Open source C++ Implementation of Customizable Route Planning (CRP) by Delling et al. This project was part of a practical course at Karlsruhe Institute of Technology (KIT). Requirements ============ In order to build CRP you need to have the following software installed: - Boost C++ Library (http://www.boost.org), more specifically Boost Iostreams. - Scons (http://scons.org) - g++ >= 4.8 (https://gcc.gnu.org) Building CRP ============ If the Boost Library is not in your PATH, make sure to edit the *SConstruct* file in the root directory to point the build script to the correct location of Boost. There is a section *Libraries* in the *SConstruct* file where you can specify the paths. Once you have installed all the software packages listed above, you can build the CRP programs by typing ``` scons --target=CRP --optimize=Opt -jX ``` into your terminal where `X` is the number of cores you want to use for building the project. If you want to use a specific g++ compiler version you can add `--compiler=g++-Version`. We also support a debug and profiling build that you can call with `--optimize=Dbg` and `--optimize=Pro` respectively. This command will build three programs in the folder *deploy*: - *osmparser*: Used to parse an OpenStreetMap (OSM) bz2-compressed map file. Call it with `./deploy/osmparser path_to_osm.bz2 path_to_output.graph.bz2` - *precalculation*: Used to build an overlay graph based on a given partition. Call it with `./deploy/precalculation path_to_graph path_to_mlp output_directory`. Here, *path_to_mlp* is the path to a *MultiLevelPartition* file for the graph that you need to provide. For more details, take a look into our project documentation. - *customization*: Used to precompute the metric weights for the overlay graph. Call it with `./deploy/customization path_to_graph path_to_overlay_graph metric_output_directory metric_type`. We currently support the following metric types: *hop* (number of edges traversed), *time* and *dist*.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值