40分求调(悬关)
  • 板块灌水区
  • 楼主nflshc20230416
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/4 23:54
  • 上次更新2025/2/5 00:57:37
查看原帖
40分求调(悬关)
1382940
nflshc20230416楼主2025/2/4 23:54

题目:https://www.luogu.com.cn/problem/P10491

代码:

#include<bits/stdc++.h>
#define N 160
using namespace std;
int d[8][2]={2,1,1,2,2,-1,1,-2,-1,2,-2,1,-1,-2,-2,-1};
bool vis[N][N];
char a[N][N];
struct node{
	int x,y,step;
};
queue <node> q;
int main(){
	int n,m;
	int x1,y1;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
			if(a[i][j]=='K') x1=i,y1=j;
		}
	q.push(node{x1,y1,0});
	memset(vis,false,sizeof(vis));
	vis[x1][y1]=true;
	while(!q.empty()){
		node cur=q.front();
		q.pop();
		int x=cur.x,y=cur.y,step=cur.step;
		for(int i=0;i<8;i++){
			int nx=x+d[i][0];
			int ny=y+d[i][1];
			if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&!vis[nx][ny]&&a[nx][ny]!='*'){
				q.push(node{nx,ny,step+1});
				vis[nx][ny]=true;
				if(a[nx][ny]=='H'){
					cout<<step+1;
					return 0;
				}
			}
		}
	}
	return 0;
}

有回必关

2025/2/4 23:54
加载中...