2020CCPC(威海) - Labyrinth(bfs+思维)

题目大意:给出一个 n * m 的矩阵,在矩阵内部最有 k 个黑洞(表示不可行走的区域),现在给出 q 次询问,每次询问给出两个点 ( x1 , y1 ) 和 ( x2 , y2 ) ,问从点 ( x1 , y1 ) 到点 ( x2 , y2 ) 的最短路是多少

题目分析:本题一个很重要的思维点:

  1. 设 x1 < x2 同时 y1 < y2:如果矩形区域 [ x1 : x2 ] X [ y1 : y2 ] 内不存在黑洞,那么答案就是其曼哈顿距离
  2. 否则一定存在着一条最短路,经过某个黑洞 { 上、下、左、右 } 的四个点其中之一

又因为黑洞的数量很小,所以可以用 bfs 预处理,然后对于每个查询来说直接枚举中间点维护最短距离就好了,预处理的时间复杂度是 O( n * m * k ),查询的时间复杂度是 O( q * k ),我在预处理时还套了一个 set 用来判断是否为黑洞点,所以还需要加个 log,不过无伤大雅

然后有个坑点就是这个题不能用 vector 来储存矩阵,会 MLE,换成 C++ 的 new 动态开内存就能过了

代码:
 

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;
     
typedef long long LL;
     
typedef unsigned long long ull;
     
const int inf=0x3f3f3f3f;

const int N=2e5+100;

const int b[4][2]={0,1,0,-1,1,0,-1,0};

int n,m,k,q;

set<pair<int,int>>st;

int* dis[50][4][N];//dis[black_hole_id][pos][x][y]

vector<pair<int,int>>node;

struct Node
{
	int x,y,step;
	Node(){}
	Node(int x,int y,int step):x(x),y(y),step(step){}
};

void bfs()
{
	for(int id=0;id<node.size();id++)//枚举黑洞 
	{
		int x,y;
		tie(x,y)=node[id];
		for(int pos=0;pos<4;pos++)//枚举四周的每个位置 
		{
			int xx=x+b[pos][0];
			int yy=y+b[pos][1];
			if(xx<=0||yy<=0||xx>n||yy>m)//起点越界 
				continue;
			for(int i=1;i<=n;i++)
			{
				dis[id][pos][i]=new int[m+5];
				for(int j=1;j<=m;j++)
					dis[id][pos][i][j]=inf;
			}
			if(st.count(make_pair(xx,yy)))//起点是黑洞 
				continue;
			queue<Node>q;
			dis[id][pos][xx][yy]=0;
			q.push(Node(xx,yy,0));
			while(q.size())
			{
				Node cur=q.front();
				q.pop();
				for(int i=0;i<4;i++)
				{
					int xx=cur.x+b[i][0];
					int yy=cur.y+b[i][1];
					if(xx<=0||yy<=0||xx>n||yy>m)
						continue;
					if(dis[id][pos][xx][yy]!=inf)//已经遍历过了 
						continue;
					if(st.count(make_pair(xx,yy)))//黑洞 
						continue;
					dis[id][pos][xx][yy]=cur.step+1;
					q.push(Node(xx,yy,cur.step+1));
				}
			}
		}
	}
}

bool check(int x1,int y1,int x2,int y2)//检查[x1,x2]X[y1,y2]内是否有黑洞 
{
	if(x1>x2)
		swap(x1,x2);
	if(y1>y2)
		swap(y1,y2);
	for(int i=0;i<k;i++)
	{
		int x,y;
		tie(x,y)=node[i];
		if(x1<=x&&x<=x2&&y1<=y&&y<=y2)
			return true;
	}
	return false;
}

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	scanf("%d%d%d%d",&n,&m,&k,&q);
	for(int i=1;i<=k;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		st.emplace(x,y);
		node.emplace_back(x,y);
	}
	bfs();
	while(q--)
	{
		int x1,x2,y1,y2;
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		if(!check(x1,y1,x2,y2))//内部没有黑洞
			printf("%d\n",abs(x1-x2)+abs(y1-y2));
		else
		{
			int ans=inf;
			for(int i=0;i<k;i++)
				for(int pos=0;pos<4;pos++)
				{
					int xx=node[i].first+b[pos][0];
					int yy=node[i].second+b[pos][1];
					if(xx<=0||yy<=0||xx>n||yy>m)
						continue;
					ans=min(ans,dis[i][pos][x1][y1]+dis[i][pos][x2][y2]);
				}
			if(ans==inf)
				ans=-1;
			printf("%d\n",ans);
		}
	}


















    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/109297647
今日推荐