欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

洛谷 P1322 血色先锋队 题解 广度优先搜索

程序员文章站 2022-05-24 09:09:29
...

这是个题记

广搜题
改过一次之后就不会有更小的经过这个点
所以无需判断这个点是否会有更小的点经过

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAXN 100005
#define FRE freopen("1.txt", "r", stdin)
#define IOS ios::sync_with_stdio(false)
//
int ap[505][505], ans, n, m, a, b;
struct node{
	int x;
	int y;
	int time;
}Start, Next[4], pointA[MAXN];
void init(){
	Next[0].x = 0;	Next[0].y = 1;
	Next[1].x = 0;	Next[1].y = -1;
	Next[2].x = 1;	Next[2].y = 0;
	Next[3].x = -1;	Next[3].y = 0;
}
void bfs(){
	queue<node>now;
	for(int i = 0; i < a; i++){
		now.push(pointA[i]);
	}
	node nextStep;
	while(!now.empty()){
		nextStep = now.front();
		now.pop();
		for(int i = 0; i < 4; i++){
			Start.x = nextStep.x + Next[i].x;
			Start.y = nextStep.y + Next[i].y;
			Start.time = nextStep.time;
			if((0 < Start.x && Start.x <= n) && (0 < Start.y && Start.y <= m) && ap[Start.x][Start.y] == 0){
				ap[Start.x][Start.y] = ++Start.time;
				now.push(Start);
			}
		}
	}
}
int main(int argc, char** argv) {
	IOS;
	FRE;
	cin >> n >> m >> a >> b;
	init();
	for(int i = 0; i < a; i++){
		cin >> pointA[i].x >> pointA[i].y;
		pointA[i].time = 0;
	}
	bfs();
	for(int i = 0; i < a; i++){
		ap[pointA[i].x][pointA[i].y] = 0;
	}
	for(int i = 0; i < b; i++){
		cin >> pointA[i].x >> pointA[i].y;
		cout << ap[pointA[i].x][pointA[i].y] << endl;
	}
	return 0;
}