55分蒟蒻求助
查看原帖
55分蒟蒻求助
354310
Tnuzy_plzro楼主2023/3/23 11:25
// Problem: P7473 [NOI Online 2021 入门组] 重力球
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P7473
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define pii pair<int,int>
int n,m,q;
vector<pii> pts;
int tot;
int zyt[1301][1301][5];
bool vis[1301][1301][5];
pii wt[255][255][5];
int imp[255][255];
int ag[5]={0,3,4,1,2};
struct edt{
	int a,b,sd;
};
int zyy[5]={0,1,0,-1,0};
int zyx[5]={0,0,-1,0,1};
pii ZY(int x,int y,int sd){
	if(imp[x][y])return {-1,-1};
	if(wt[x][y][sd]!=(pii){0,0}){
		return wt[x][y][sd];
	}
	pii res;
	int x1=x+zyx[sd],y1=y+zyy[sd];
	if(ZY(x1,y1,sd)!=(pii){-1,-1}){
		res=ZY(x1,y1,sd);
	}else{
		res={x,y};
	}
	return wt[x][y][sd]=res;
}
bool valid(int x,int y){
	return x>0&&x<=n&&y>0&&y<=n&&!imp[x][y];
}
vector<edt> lns[1301][1301][5];
void line(){
	rep(i,1,tot)rep(j,1,tot)rep(w,1,4){
		auto top=(edt){i,j,w};
		int x1=pts[top.a].first,y1=pts[top.a].second;
		int x2=pts[top.b].first,y2=pts[top.b].second;
		x1+=zyx[top.sd];x2+=zyx[top.sd];
		y1+=zyy[top.sd];y2+=zyy[top.sd];
		if(!valid(x1,y1))continue;
		if(!valid(x2,y2))continue;
		rep(k,1,4){
			auto p1=ZY(x1,y1,k),
			p2=ZY(x2,y2,k);
			auto [xx,yy]=p1;
			xx+=zyx[k],yy+=zyy[k];
			auto [xxx,yyy]=p2;
			xxx+=zyx[k],yyy+=zyy[k];
			auto tox=imp[xx][yy],
            toy=imp[xxx][yyy];
            lns[tox][toy][ag[k]].push_back(top);
		}
	}
}
void bfs(){
	queue<edt> Q;
	rep(i,1,tot)rep(j,1,4)zyt[i][i][j]=0,Q.push((edt){i,i,j});
	while(!Q.empty()){
		auto top=Q.front();Q.pop();
        if(vis[top.a][top.b][top.sd])continue;
        vis[top.a][top.b][top.sd]=1;
		for(auto ed:lns[top.a][top.b][top.sd]){
			if(!vis[ed.a][ed.b][ed.sd]&&zyt[ed.a][ed.b][ed.sd]>zyt[top.a][top.b][top.sd]+1){
				zyt[ed.a][ed.b][ed.sd]=zyt[top.a][top.b][top.sd]+1;
				Q.push((edt){ed.a,ed.b,ed.sd});
			}
		}
	}
}
signed main(){
    memset(zyt,127,sizeof zyt);
	cin>>n>>m>>q;
	pts.push_back({0,0});
	rep(i,1,m){
		int x,y;
		cin>>x>>y;
		pts.push_back({x,y});
	}
	rep(i,1,n)pts.push_back({0,i});
	rep(i,1,n)pts.push_back({i,0});
	rep(i,1,n)pts.push_back({n+1,i});
	rep(i,1,n)pts.push_back({i,n+1});
	tot=pts.size()-1;
	rep(i,1,tot){
		imp[pts[i].first][pts[i].second]=i;
	}
	line();
    bfs();
    rep(i,1,q){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        if(x1==x2&&y1==y2){
        	puts("0");
        	continue;
        }
        int ans=1e9;
        rep(k,1,4){
			auto p1=ZY(x1,y1,k),
			p2=ZY(x2,y2,k);
			auto [xx,yy]=p1;
			xx+=zyx[k],yy+=zyy[k];
			auto [xxx,yyy]=p2;
			xxx+=zyx[k],yyy+=zyy[k];
			auto tox=imp[xx][yy],
            toy=imp[xxx][yyy];
            ans=min(ans,zyt[tox][toy][ag[k]]);
		}
        cout<<ans+1<<'\n';
    }
}
2023/3/23 11:25
加载中...