求助 悬关
查看原帖
求助 悬关
649246
nothing__楼主2023/11/29 13:41

预处理+广搜

8pts只对了-1的点

#include<bits/stdc++.h>
using namespace std;
const int N=16400;
string ss[N];
int n, m, sx, sy, ex, ey; 
map<int, bool> mp[N];
struct node{int x, y, dep;} ;
int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};
int dxx[8]={1, 1, 1, -1, -1, -1, 0, 0};
int dyy[8]={1, -1, 0, 1, -1, 0, 1, -1};
bool pd(int x, int y) {
	if(ss[x][y]=='X') return true;
	else return false;
}
bool check() {
	for(int i=0;i<8;i++) {
		int xx=ex+dxx[i], yy=ey+dyy[i];
		if(xx<0||yy<0||xx>=n||yy>=m) continue;
		if(!pd(xx, yy)) return false;
	}
	return true;
}
void init() {
	for(int i=1;i<=n;i++) mp[i].clear();
	for(int i=0;i<8;i++) {
		int xx=ex+dxx[i], yy=ey+dyy[i];
		if(xx<0||yy<0||xx>=n||yy>=m) continue;
		if(pd(xx, yy)) continue;
		while(!pd(xx, yy)) {
			if(!mp[xx][yy]) mp[xx][yy]=true;
			xx=xx+dxx[i], yy=yy+dyy[i];
			if(xx<0||yy<0||xx>=n||yy>=m) break;
		}
	}
}
void bfs() {
	bool bk=false; int ans=0;
	map<int, bool> ct[N];
	queue<node> q; q.push({sx, sy, 0});
	if(mp[sx][sy]) {puts("0"); return ;}
	while(!q.empty()) {
		node x=q.front(); q.pop();
		for(int i=0;i<4;i++) {
			int xx=x.x+dx[i], yy=x.y+dy[i];
			if(xx<0||yy<0||xx>=n||yy>=m) continue;
			if(pd(xx, yy)) continue;
			if(ct[xx][yy]) continue;
			else ct[xx][yy]=true;
			if(mp[xx][yy]) {bk=true, ans=x.dep; break;}
			else mp[xx].erase(yy);
			q.push({xx, yy, x.dep+1});
		}
		if(bk) break;
	}
	printf("%d\n", ans+1);
}
int main() {
	scanf("%d%d", &n, &m);
	for(int i=0;i<n;i++) cin >> ss[i];
	while(scanf("%d%d%d%d", &sx, &sy, &ex, &ey)!=EOF) {
		if(sx+sy+ex+ey==0) break;
		--sx, --sy, --ex, --ey;
		if(check()) {puts("Poor Harry"); continue;}
		init();
		bfs();
	}
	return 0;
}
2023/11/29 13:41
加载中...