acwing 188 武士风度的牛

简介: acwing 188 武士风度的牛

武士风度的牛 - AcWing题库

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
 
using namespace std ;
const int N = 200 ;
char g[N][N] ;
int n , m ;
int fx,fy ; 
int hx,hy ;
struct node{
  int x , y ;
  node(int xx ,int yy){
    x = xx , y = yy ;
  }
};
int d[8][2]= {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
queue<node> q ;
int dis[N][N] ;
void bfs(){
  q.push(node(fx,fy)) ;
  dis[fx][fy] = 0 ;
  while(!q.empty()){
    node now = q.front() ;
    q.pop() ;
    int x = now.x , y = now.y ;
    if(x == hx && y == hy) return ;
    for(int i = 0 ; i < 8 ; i ++){
      int tx = x + d[i][0] , ty = y + d[i][1] ;
      if(tx<0||tx>=n||ty<0||ty>=m||dis[tx][ty]!=-1||g[tx][ty] == '*') continue ;
      q.push(node(tx,ty)) ;
      dis[tx][ty] = dis[x][y] + 1 ;
    }
  }
}
int main(){
  cin >> m >> n ;
  memset(dis,-1,sizeof(dis)) ;
  for(int i = 0 ; i < n ;i ++){
    for(int j = 0 ; j < m ;j ++){
      cin >> g[i][j] ;
      if(g[i][j] == 'K') fx = i , fy = j ;
      if(g[i][j] == 'H') hx = i , hy = j ;
    }
  }
  bfs() ;
  cout << dis[hx][hy] << endl ;
}
目录
相关文章
|
12天前
acwing 1010 拦截导弹
acwing 1010 拦截导弹
26 1
AcWing 1265. 数星星(每日一题)
AcWing 1265. 数星星(每日一题)
|
9天前
acwing 1116 马走日
acwing 1116 马走日
7 0
|
9天前
acwing 1107 魔板
acwing 1107 魔板
9 0
|
10天前
acwing 285. 没有上司的舞会
acwing 285. 没有上司的舞会
10 0
|
11天前
acwing 1017 怪盗基德的滑翔翼
acwing 1017 怪盗基德的滑翔翼
22 0
|
11天前
acwing 1012 友好城市
acwing 1012 友好城市
11 0
|
11天前
acwing 1014 登山
acwing 1014 登山
19 0
AcWing 562. 壁画(每日一题)
AcWing 562. 壁画(每日一题)
|
5月前
|
人工智能
acwing 5478. 分班
acwing 5478. 分班