답안 #124647

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124647 2019-07-03T16:19:54 Z MoNsTeR_CuBe 오벨리스크 (NOI14_obelisk) C++17
0 / 25
1000 ms 24056 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long 

const int INF = numeric_limits<int>::max();

vector< vector< pair<int, int> > > holes;

vector< vector< vector< int > > > memo;

pair<int, int> start;
pair<int, int> ende;

int m,k;

int X [4] = {1,-1,0,0};
int Y[4] = {0,0,1,-1};

void generate_grid(int A, int B, vector< vector< int > > &grid){
	
	
	priority_queue< tuple<int, int, int>, vector< tuple<int, int, int> >, greater< tuple<int, int, int> > > pq;
	
	pq.push(make_tuple(0,A,B));
	
	while(!pq.empty()){
		tuple< int, int, int> tup = pq.top();
		pq.pop();
		int a = get<1>(tup);
		int b = get<2>(tup);
		for(int i = 1; i <= m; i++){
			for(int j = 0; j < 4; j++){
				if(a+i*(X[j]) <= 50 && a+i*(X[j]) > 0 && b+i*Y[j] <= 50 && b+i*Y[j] > 0){
					if(grid[a+i*(X[j])][b+i*Y[j]] != 0 || (a+i*(X[j]) == A && b+i*Y[j] == B)) continue;
					grid[a+i*(X[j])][b+i*Y[j]] = 2+i%m+grid[a][b];
					pq.push(make_tuple(2+i%m+grid[a][b],a+i*(X[j]),b+i*Y[j]));
				}
			}
		}
	}
	
	grid[A][B] = 0;
}

int dist(int a, int b, int c, int d){
	if(k == 1) return abs(a-c)+abs(b-d);
	
	vector< vector< int > > grid(51, vector< int >(51));
	
	m++;
	generate_grid(a,b, grid);
	m--;
	return grid[c][d];
}

int dp(int floor, int x, int y){
	//cout << floor << ' ' << x << ' ' << y << endl;
	if(floor == 1){
		return dist(x,y,ende.first, ende.second);
	}
	
	if(memo[floor][x][y] != -1) return memo[floor][x][y];
	
	memo[floor][x][y] = INF;
	
	for(int i = 0; i < (int)holes[floor].size(); i++){
		pair<int, int> p = holes[floor][i];
		memo[floor][x][y] = min(memo[floor][x][y], dp(floor-1, p.first, p.second) + dist(p.first, p.second, x, y));
	}
	return memo[floor][x][y];
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> k >> m;
	
	cin >> start.first >> start.second;
	
	cin >> ende.first >> ende.second;
	
	holes.resize(k+1);
	
	for(int i = k; i > 1; i--){
		int q;
		cin >> q;
		
		for(int j = 0; j < q; j++){
			pair<int, int> p;
			cin >> p.first >> p.second;
			holes[i].push_back(p);
		}
	}
	memo.resize(k+1, vector< vector< int > >(51, vector< int > (51,-1)));
	
	cout << dp(k, start.first, start.second) << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 140 ms 632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1084 ms 632 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 33 ms 24056 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 36 ms 24056 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -