8 条题解
-
0
#include<bits/stdc++.h> using namespace std; int n,m,sx,sy,ex,ey; char ch; int a[155][155]; bool qp[155][155]; struct id { int x,y,s; }; queue<id>q; int xx[8]={1,1,-1,-1,2,2,-2,-2}; int yy[8]={2,-2,2,-2,1,-1,1,-1}; int main() { memset(qp,false,sizeof(qp)); cin>>n>>m; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { cin>>ch; if(ch=='*')continue; qp[i][j]=true; if(ch=='K')sx=i,sy=j; if(ch=='H')ex=i,ey=j; } } q.push(id{sx,sy,0}); qp[sx][sy]=false; while(!q.empty()) { id t=q.front(); q.pop(); for(int i=0;i<8;i++) { int nx=t.x+xx[i]; int ny=t.y+yy[i]; if(qp[nx][ny]) { if(nx==ex&&ny==ey) { cout<<t.s+1; return 0; } q.push(id{nx,ny,t.s+1}); qp[nx][ny]=false; } } } }
信息
- ID
- 99
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 5
- 标签
- 递交数
- 376
- 已通过
- 139
- 上传者