AtCoder Beginner Contest 213 E - Stronger Takahashi(01bfs)

题意
给你一个 h ∗ w h*w hw的表格,由#和*组成,#是是障碍不可以走,高桥想从点 ( 1 , 1 ) (1,1) (1,1)走到 ( h , w ) (h,w) (h,w),高桥还有一个能力,如果前方有障碍的时候,可以选择一拳把前方 2 ∗ 2 2*2 22的区域打开,打开以后就可以走了,问高桥最少需要几次拳击才可以到达 ( n , n ) (n,n) (n,n)
思路
一个裸的01-bfs。
代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N=1e5+10;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
char c[510][510];
int dis[510][510];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int main()
{
    memset(dis,INF,sizeof dis);
    int h,w;
    cin>>h>>w;
    for(int i=1 ; i<=h ; i++)
        for(int j=1 ; j<=w ; j++)
            cin>>c[i][j];
    queue<PII>q;
    q.push({1,1,});
    dis[1][1]=0;
    while(!q.empty())
    {
        int x=q.front().fi;
        int y=q.front().se;
        q.pop();
        for(int i=0 ; i<4 ; i++)
        {
            int cx=x+dx[i];
            int cy=y+dy[i];
            if(cx<=0||cx>h||cy<=0||cy>w) continue;
            if(c[cx][cy]=='.')
            {
                if(dis[cx][cy]>dis[x][y])
                {
                    dis[cx][cy]=dis[x][y];
                    q.push({cx,cy});
                }
            }
            else
            {
                for(int j=-1 ; j<=1 ; j++)
                {
                    for(int k=-1 ; k<=1 ; k++)
                    {
                        int ccx=cx+j;
                        int ccy=cy+k;
                        if(ccx<=0||ccx>h||ccy<=0||ccy>w) continue;
                        if(dis[ccx][ccy]>dis[x][y]+1)
                        {
                            dis[ccx][ccy]=dis[x][y]+1;
                            q.push({ccx,ccy});
                        }
                    }
                }
            }
        }
    }
    cout<<dis[h][w]<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值