80pts求错,请求大佬支援
查看原帖
80pts求错,请求大佬支援
467443
A宋锦瑞A楼主2022/1/20 12:41
#include<bits/stdc++.h>

using namespace std;

struct but {
	int x,y;
	int step;
};

int n,m,ans,x,y,tx,ty;
int look[8][2]= {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int fx[4][2]= {1,0,0,1,-1,0,0,-1};
bool v[10001][10001];
bool mp[10001][10001];
string in;

bool judge(int x,int y,int ex,int ey) {
	if(x==ex&&y==ey) {
		return true;
	}
	for(int i=0; i<8; i++) {
		int x0=x+look[i][0];
		int y0=y+look[i][1];
		while(x0>0&&x0<=m&&y0>0&&y0<=n&&mp[x0][y0]==0) {
			if(x0==ex&&y0==ey) {
				return true;
			}
			x0=x0+look[i][0];
			y0=y0+look[i][1];
		}
	}
	return false;
}

int BFS() {
	queue<but>q;
	but a;
	a.step=0;
	a.x=x;
	a.y=y;
	q.push(a);
	while(q.size()) {
		a=q.front();
		q.pop();
		if(judge(a.x,a.y,tx,ty)) {
			return a.step;
		}
		but b;
		for(int i=0; i<=3; i++) {
			b.x=a.x+fx[i][0];
			b.y=a.y+fx[i][1];
			if(b.x>0&&b.x<=n&&b.y>0&&b.y<=m&&v[b.x][b.y]==0&&mp[b.x][b.y]==0) {
			    b.step=a.step+1;
				v[b.x][b.y]=1;
				q.push(b);
			}
		}
	}
	return -1;
}

int main() {
	cin>>n>>m;
	for(int i=1; i<=n; i++) {
		cin>>in;
		for(int j=1; j<=m; j++) {
		    if(in[j-1]=='X') {
		        mp[i][j]=1;
		    }
		}
	}
	while(cin>>tx>>ty>>x>>y,tx+ty+x+y!=0) {
		memset(v,0,sizeof(v));
		ans=BFS();
		if(ans==-1) {
			cout<<"Poor Harry";
		} else {
			cout<<ans;
		}
		cout<<endl;
	}

	return 0;
}
2022/1/20 12:41
加载中...