社区讨论

30分一个WA加6个RE求救

P10491[USACO09NOV] The Chivalrous Cow B参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lz6nhkwe
此快照首次捕获于
2024/07/29 15:11
2 年前
此快照最后确认于
2024/07/29 15:58
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+5;
int n,m,vis[maxn][maxn],x,y,u,v;
int dx[8]={1,1,-1,-1,2,2,-2,-2};
int dy[8]={2,-2,2,-1,1,-1,1,-1};
string s[maxn];
struct node{
	int x,y;
};
queue<node>q;
void bfs(int x,int y){
	while(!q.empty()){
		node a=q.front();
		q.pop();
		for(int i=0;i<8;++i){
			int nx=dx[i]+a.x,ny=dy[i]+a.y;
			if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&vis[nx][ny]==0&&s[nx][ny]!='*'){
				q.push({nx,ny});
				vis[nx][ny]=vis[a.x][a.y]+1;
				if(nx==u&&ny==v){
					cout<<vis[nx][ny]-1;
					return;
				}
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>m>>n;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j){
			cin>>s[i][j];
			if(s[i][j]=='K')
				x=i,y=j,q.push({i,j});
			if(s[i][j]=='H')
				u=i,v=j;
		}
	vis[x][y]=1;
	bfs(x,y);
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...