我需要帮助
  • 板块灌水区
  • 楼主Phoenix114514
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/28 18:49
  • 上次更新2024/7/28 20:27:25
查看原帖
我需要帮助
1233263
Phoenix114514楼主2024/7/28 18:49

rt

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int dx[]={1,2,2,1,-1,-2,-2,-1};
const int dy[]={2,1,-1,-2,-2,-1,1,2};
struct node{
    int x,y,step;
};
int c,r;
char a[155][155];
queue<node> q;
set<pair<int,int>> s;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>c>>r;
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++){
            cin>>a[i][j];
            if (a[i][j]=='K'){
                q.push({i,j,0});
                s.insert({i,j});
            }
        }
    }
    while(!q.empty()){
        auto t=q.front();
        if (a[t.x][t.y]=='H'){
            cout<<t.step;
            return 0;
        }
        q.pop();
        for (int i=0;i<8;i++){
            int nx=t.x+dx[i],ny=t.y+dy[i];
            if (nx<0||nx>c-1|ny<0||ny>r-1||a[nx][ny]=='*'||s.count({nx,ny}))continue;
            s.insert({nx,ny});
            q.push({nx,ny,t.step+1});
        }
    }
    return 0;
}

2024/7/28 18:49
加载中...