ABC213E题解

· · 题解

先来看不用拳头的情况,每次上下左右一个,比较好办。

而用拳头相当于就是可以走到任意一个被拳头砸开的位置。

就比如说,开始我们在红点:

而我们可以走到的地方是绿色的:

那么整体来说,所有可以用拳头走到的地方是:

按照这种方式转移即可。

代码:

#include<iostream>
#include<deque>
#include<cstring>
using namespace std;

int h,w;
string a[1005];
int dx[10] = {0,0,1,-1},dy[10] = {1,-1,0,0};
int dis[1005][1005];

struct node
{
    int x,y;
};

deque<node> q;

void bfs()
{
    q.push_front({1,1});
    dis[1][1] = 0;
    while(!q.empty())
    {
        int x = q.front().x,y = q.front().y;
        q.pop_front();

        for(int i=0;i<4;i++)
        {
            int xnew = x + dx[i],ynew = y + dy[i];
            if(xnew < 1 || xnew > h || ynew < 1 || ynew > w || a[xnew][ynew] == '#' || dis[xnew][ynew] <= dis[x][y]) continue;
            q.push_front({xnew,ynew});
            dis[xnew][ynew] = dis[x][y];
        }

        for(int xnew=x-2;xnew<=x+2;xnew++)
            for(int ynew=y-2;ynew<=y+2;ynew++)
            {
                if(abs(xnew - x) + abs(ynew - y) == 4) continue;
                if(xnew < 1 || xnew > h || ynew < 1 || ynew > w  || dis[xnew][ynew] <= dis[x][y]+1) continue;
                q.push_back({xnew,ynew});
                dis[xnew][ynew] = dis[x][y]+1;
            }
    }
}

int main()
{
    cin >> h >> w;
    for(int i = 1; i <= h; i++)
    {
        cin >> a[i];
        a[i] = " " + a[i];
    }

    memset(dis, 0x3f, sizeof(dis));
    bfs();

    printf("%d",dis[h][w]);
    return 0;
}