답안 #515056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
515056 2022-01-19T02:17:10 Z amunduzbaev Golf (JOI17_golf) C++14
30 / 100
2162 ms 136824 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array

const int N = 2e3 + 5;
int g[N][N][4];
int d[N][N][4];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	ar<int, 2> a, b; cin>>a[0]>>a[1]>>b[0]>>b[1];
	int n; cin>>n;
	vector<ar<int, 4>> p(n);
	for(int i=0;i<n;i++){
		cin>>p[i][0]>>p[i][2]>>p[i][1]>>p[i][3];
	}
	
	vector<ar<int, 2>> tt;
	for(int i=0;i<n;i++){
		tt.push_back({p[i][0], i<<1});
		tt.push_back({p[i][2], i<<1|1});
	} 
	tt.push_back({a[0], -1});
	tt.push_back({b[0], -2});
	sort(tt.begin(), tt.end());
	
	int last = 0;
	for(int i=0;i<(int)tt.size();){
		int j = i;
		while(j < (int)tt.size() && tt[i][0] == tt[j][0]){
			if(tt[j][1]>=0) p[tt[j][1]>>1][(tt[j][1]&1) << 1] = last;
			else if(tt[j][1]==-1) a[0] = last;
			else b[0] = last;
			j++;
		} i = j, last++;
	} tt.clear();
	
	for(int i=0;i<n;i++){
		tt.push_back({p[i][1], i<<1});
		tt.push_back({p[i][3], i<<1|1});
	} 
	tt.push_back({a[1], -1});
	tt.push_back({b[1], -2});
	
	sort(tt.begin(), tt.end());
	last = 0;
	for(int i=0;i<(int)tt.size();){
		int j = i;
		while(j < (int)tt.size() && tt[j][0] == tt[i][0]){
			if(tt[j][1] >= 0) p[tt[j][1]>>1][(tt[j][1]&1)<<1|1] = last;
			else if(tt[j][1] == -1) a[1] = last;
			else b[1] = last;
			j++;
		} last++, i = j;
	}
	
	int ch[4][2] = {
		{0, 1},
		{0, -1},
		{1, 0},
		{-1, 0}
	};
	
	for(int k=0;k<n;k++){
		for(int i=p[k][0];i<=p[k][2];i++){
			for(int j=p[k][1];j<=p[k][3];j++){
				for(int t=0;t<4;t++){
					int x = i+ch[t][0], y = j+ch[t][1];
					if(p[k][0] <= x && x <= p[k][2] && 
					   p[k][1] <= y && y <= p[k][3]){
						if(x == i && (i == p[k][0] || i == p[k][2])) continue;
						if(y == j && (j == p[k][1] || j == p[k][3])) continue;
						g[i][j][t] = 1;
					}
				}
			}
		}
	}
	
	auto check = [&](int x, int y){ return (0 <= x && x < N && 0 <= y && y < N); };
	memset(d, 2, sizeof d);
	deque<int> qq;
	for(int t=0;t<4;t++){
		qq.push_back((a[0] * N + a[1]) << 2 | t);
		d[a[0]][a[1]][t] = 1;	
	}
	
	while(!qq.empty()){
		int u = qq.front(); qq.pop_front(); 
		int t = u & 3; u >>= 2;
		int i = u / N, j = u % N;
		//~ cout<<i<<" "<<j<<" "<<t<<"\n";
		if(!g[i][j][t]){
			int x = i + ch[t][0], y = j + ch[t][1];
			if(check(x, y) && d[x][y][t] > d[i][j][t]){
				d[x][y][t] = d[i][j][t];
				qq.push_front((x * N + y) << 2 | t);
			}
		}
		
		for(int k=0;k<4;k++){
			if(g[i][j][k]) continue;
			int x = i + ch[k][0], y = j + ch[k][1];
			if(check(x, y) && d[x][y][k] > d[i][j][t] + 1){
				d[x][y][k] = d[i][j][t] + 1;
				qq.push_back((x * N + y) << 2 | k);
			}
		}
	}
	
	int res = 1e9;
	for(int t=0;t<4;t++) res = min(res, d[b[0]][b[1]][t]);
	cout<<res<<"\n";
}

# 결과 실행 시간 메모리 Grader output
1 Correct 393 ms 112688 KB Output is correct
2 Correct 520 ms 112636 KB Output is correct
3 Correct 516 ms 112380 KB Output is correct
4 Correct 528 ms 113708 KB Output is correct
5 Correct 538 ms 116540 KB Output is correct
6 Correct 389 ms 107644 KB Output is correct
7 Correct 435 ms 117804 KB Output is correct
8 Correct 392 ms 116748 KB Output is correct
9 Correct 419 ms 118100 KB Output is correct
10 Correct 446 ms 118160 KB Output is correct
11 Correct 459 ms 118856 KB Output is correct
12 Correct 446 ms 118184 KB Output is correct
13 Correct 457 ms 118480 KB Output is correct
14 Correct 399 ms 107848 KB Output is correct
15 Correct 545 ms 114996 KB Output is correct
16 Correct 515 ms 118100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 393 ms 112688 KB Output is correct
2 Correct 520 ms 112636 KB Output is correct
3 Correct 516 ms 112380 KB Output is correct
4 Correct 528 ms 113708 KB Output is correct
5 Correct 538 ms 116540 KB Output is correct
6 Correct 389 ms 107644 KB Output is correct
7 Correct 435 ms 117804 KB Output is correct
8 Correct 392 ms 116748 KB Output is correct
9 Correct 419 ms 118100 KB Output is correct
10 Correct 446 ms 118160 KB Output is correct
11 Correct 459 ms 118856 KB Output is correct
12 Correct 446 ms 118184 KB Output is correct
13 Correct 457 ms 118480 KB Output is correct
14 Correct 399 ms 107848 KB Output is correct
15 Correct 545 ms 114996 KB Output is correct
16 Correct 515 ms 118100 KB Output is correct
17 Correct 296 ms 129612 KB Output is correct
18 Correct 304 ms 129480 KB Output is correct
19 Correct 262 ms 128404 KB Output is correct
20 Correct 274 ms 129604 KB Output is correct
21 Correct 288 ms 129264 KB Output is correct
22 Correct 274 ms 128956 KB Output is correct
23 Correct 271 ms 128128 KB Output is correct
24 Correct 261 ms 126816 KB Output is correct
25 Correct 257 ms 128580 KB Output is correct
26 Correct 261 ms 128024 KB Output is correct
27 Correct 532 ms 115508 KB Output is correct
28 Correct 507 ms 118856 KB Output is correct
29 Correct 499 ms 119096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 393 ms 112688 KB Output is correct
2 Correct 520 ms 112636 KB Output is correct
3 Correct 516 ms 112380 KB Output is correct
4 Correct 528 ms 113708 KB Output is correct
5 Correct 538 ms 116540 KB Output is correct
6 Correct 389 ms 107644 KB Output is correct
7 Correct 435 ms 117804 KB Output is correct
8 Correct 392 ms 116748 KB Output is correct
9 Correct 419 ms 118100 KB Output is correct
10 Correct 446 ms 118160 KB Output is correct
11 Correct 459 ms 118856 KB Output is correct
12 Correct 446 ms 118184 KB Output is correct
13 Correct 457 ms 118480 KB Output is correct
14 Correct 399 ms 107848 KB Output is correct
15 Correct 545 ms 114996 KB Output is correct
16 Correct 515 ms 118100 KB Output is correct
17 Correct 296 ms 129612 KB Output is correct
18 Correct 304 ms 129480 KB Output is correct
19 Correct 262 ms 128404 KB Output is correct
20 Correct 274 ms 129604 KB Output is correct
21 Correct 288 ms 129264 KB Output is correct
22 Correct 274 ms 128956 KB Output is correct
23 Correct 271 ms 128128 KB Output is correct
24 Correct 261 ms 126816 KB Output is correct
25 Correct 257 ms 128580 KB Output is correct
26 Correct 261 ms 128024 KB Output is correct
27 Correct 532 ms 115508 KB Output is correct
28 Correct 507 ms 118856 KB Output is correct
29 Correct 499 ms 119096 KB Output is correct
30 Runtime error 2162 ms 136824 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -