1685: 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<queue>
#include<cstring>
#include <cstdio>
#include<memory.h>

using namespace std;

const int N=120;
int ans, n, treax[10], treay[10], treal;
bool a[N][N][20][64];
int fx[] = {0, 1, 0, -1, 0}, fy[] = {0, 0, 1, 0, -1};
char maze[N][N];

struct node
{
    int x, y, time;
    int board, trea;
};

int judgenum(int x, int y)
{
    for(int i = 1; i <= treal; i++)
    {
        if(treax[i] == x && treay[i] == y )
        {
            return i;
        }
    }
}

node New, now, first;

void bfs(node xxx)
{
    queue<node> q;


    a[first.x][first.y][first.trea][first.board] = true;

    ans = 9999;

    q.push(first);
    while(!q.empty())
    {
        now = q.front();
        q.pop();


        if(maze[now.x][now.y] == '*')
        {
            ans = min(now.time, ans);
            continue;
        }
        for(int i = 1; i <= 4; i++)
        {

            New = now;
            New.x += fx[i];
            New.y += fy[i];
            New.time ++ ;

            if(New.x < 1 || New.y < 1 || New.x > n || New.y > n || maze[New.x][New.y] == '#')
                continue;

            char ch = maze[New.x][New.y];

            if(ch == '=')
            {
                int xx = judgenum(New.x, New.y);

                if((New.trea & (1<<xx)) == 0)
                    New.trea |= (1<<xx);

                if(!a[New.x][New.y][New.trea][New.board])
                {
                    a[New.x][New.y][New.trea][New.board] = true;
                    New.time ++;
                    q.push(New);
                }
            }

            else if(ch >= '1' && ch <= '4')
            {
                int j = maze[New.x][New.y] - '1';
                    New.board |= (1<<j);

                if(!a[New.x][New.y][New.trea][New.board])
                {
                    q.push(New);
                    a[New.x][New.y][New.trea][New.board] = true;
                }
            }

            else if(ch >= 'a' && ch <= 'd')
            {
                if(New.board & (1<<(ch-'a')) && !a[New.x][New.y][New.trea][New.board])
                {
                    a[New.x][New.y][New.trea][New.board] = true;
                    q.push(New);
                }
            }

            else if(!a[New.x][New.y][New.trea][New.board])
            {
                a[New.x][New.y][New.trea][New.board] = true;
                q.push(New);
            }
        }
    }
}

int main()
{
    int t, x, y;

        while(scanf("%d", &n) != EOF){
            //scanf("%d", &n);
            memset(a, 0, sizeof(a));
            treal = 0;

            for(int i = 1; i <= n; i++)
            {
                scanf("%s", &maze[i][1]);
                for(int j = 1; j <= n; j++)
                {
                    if(maze[i][j] == '@')
                    {
                        first.x = i;
                        first.y = j;
                    }
                    else if(maze[i][j] == '=')
                    {
                        treax[++treal] = i; treay[treal] = j;
                    }
                }
            }

            first.time = 0;
            first.board = 0;
            first.trea = 0;

            bfs(first);
            if(ans == 9999)
                printf("MISSION FAILED\n");
            else
                printf("%d\n", ans);
        }

    //}
    return 0;
}
发布了55 篇原创文章 · 获赞 3 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章