求助站外题
  • 板块学术版
  • 楼主Phoenix114514
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/27 20:33
  • 上次更新2024/7/27 21:52:32
查看原帖
求助站外题
1233263
Phoenix114514楼主2024/7/27 20:33

Link

Code

#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;
}

Hack 数据

15 45 ............... .............. .............. ............... ............... .............. ............... .............. ..K............ .............. .............. .............. .............. ............... .............. .............. ............... .............. ............... .............. ............... ............... ............... ............... ............... .............. ............... .............. ............... ............... ............... .............. .............. ............... .............. .............. ............. .............. ............. ............... ............... .............. ............... ..............* .........H.....

2024/7/27 20:33
加载中...