[蓝桥杯]迷宫于陷阱

#include<iostream>
#include<queue>
using namespace std;
const int N = 1e3 + 10;
int n, k;
char mp[N][N];
bool vis[N][N][11];
int ans;
int dix[] = { 0,1,0,-1 };
int diy[] = { 1,0,-1,0 };
struct node
{
    int x, y,k ,step ;
    node(int cx, int cy, int ck, int cs)
    {
        x = cx,y = cy, k = ck, step = cs;
    }
};
int bfs()
{
    queue<node>q;
    vis[0][0][0] = 1;
    q.push(node(0, 0, 0, 0));
    while (!q.empty())
    {
        node now = q.front();
        q.pop();
        if (now.x == n - 1 && now.y == n - 1)
        {
            return now.step;
        }
        for (int i = 0; i < 4; i++)
        {
            int tox = now.x + dix[i];
            int toy = now.y + diy[i];
            if (tox < 0 || toy < 0 || tox >= n || toy >= n || mp[tox][toy] == '#')
            {
                continue;
            }
            //无敌
            if (mp[tox][toy] == '%' && !vis[tox][toy][k])
            {
                vis[tox][toy][k] = 1;
                q.push(node(tox, toy, k, now.step + 1));
            }
            else
            {
                if (now.k && !vis[tox][toy][k - 1])
                {
                    vis[tox][toy][k-1] == 1;
                    q.push(node(tox, toy, k-1, now.step + 1));
                }
                else
                {
                    if (mp[tox][toy] == '.' && !now.k && !vis[tox][toy][0])
                    {
                        vis[tox][toy][0] = 1;
                        q.push(node(tox, toy, 0, now.step + 1));
                    }
                }
            }
        }
    }
    return -1;
}
int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> mp[i];
    }
    ans = bfs();
    cout << ans;
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值