8 条题解

  • -2
    @ 2022-7-31 20:32:31

    #include <math.h> #include <stdio.h> #include #include <string.h> #include #include using namespace std; char a[155][155]; struct node { int x,y,step; }; int n , m; int dx[] = {1,1,-1,-1,2,2,-2,-2}; int dy[] = {2,-2,2,-2,1,-1,1,-1}; int bfs(int x , int y) { queue p; p.push( (node){x,y,0} ); a[x][y] = ''; while(!p.empty()) { node t = p.front(); p.pop(); for(int i = 0 ; i < 8 ;i++) { x = dx[i] + t.x; y = dy[i] + t.y; if(x < 0 || y < 0 || x>= n || y >= m || a[x][y]=='') continue; if(a[x][y] == 'H') return t.step + 1; a[x][y] = '*'; p.push((node){x,y,t.step + 1}); } } return -1; } int main() { cin >> m >> n; int sx,sy; for(int i = 0 ; i < n ; i++) { cin >> a[i]; for(int j = 0 ; j < m ; j++) { if(a[i][j] == 'K') sx = i , sy = j; } } cout << bfs(sx,sy); return 0; }

    信息

    ID
    99
    时间
    1000ms
    内存
    128MiB
    难度
    5
    标签
    递交数
    376
    已通过
    139
    上传者