답안 #645580

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
645580 2022-09-27T11:20:42 Z google 로봇 (APIO13_robots) C++17
30 / 100
1500 ms 68980 KB
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
const int INF = 0x3f3f3f3f;
int N,W,H;
string G[500];
int h[10][10];
int dist[10][500][500],dp[50][500][500];
pair<int,int> ma[500][500][4];
bool legal(int i, int j, int d){
	if (d == 0 && (i+1 == H || G[i+1][j] == 'x')) return 0;
	if (d == 1 && (j+1 == W || G[i][j+1] == 'x')) return 0;
	if (d == 2 && (i == 0 || G[i-1][j] == 'x')) return 0;
	if (d == 3 && (j == 0 || G[i][j-1] == 'x')) return 0;
	return 1;
}
/*
2 3 4
...
.2.
xxx
.1.
4 10 5
1.........
AA...x4...
..A..x....
2....x....
..C.3.A...

*/
pair<int,int> move(int i, int j, int d){
	int oi = i,oj = j,od = d;
	while (1){
		if (ma[i][j][d].first != -1) {
			return ma[oi][oj][od] = ma[i][j][d];
		}
		if (G[i][j] == 'A') {
			d++;
			if (d == 4) d = 0;
		}
		if (G[i][j] == 'C'){
			d--;
			if (d == -1) d = 3;
		}
		if (!legal(i,j,d)) return ma[oi][oj][od] = {i,j};
		if (d == 0) i++;
		if (d == 1) j++;
		if (d == 2) i--;
		if (d == 3) j--;
	}
}
void calc(int i, int j, int l){
	queue<pair<pair<int,int>,int>> q;
	q.push({{i,j},0});
	while (!q.empty()){
		auto [t,tt] = q.front();q.pop();
		if (dp[h[l][l]][t.first][t.second] < tt) continue;
		dp[h[l][l]][t.first][t.second] = tt;
		for (int ii = 0;ii<4;ii++)q.push({{ma[t.first][t.second][ii].first,ma[t.first][t.second][ii].second},tt+1});
	}
}
vector<vector<int>> calc1(int i1,int j1,int i2, int j2){
	vector<vector<int>> ans(H,vector<int>(W));
	//v1 = dp[h[i1][j1]],v2 = dp[h[i2][j2]]; 
	priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;
	for (int i = 0;i<H;i++){
		for (int j = 0;j<W;j++){
			ans[i][j] = min(INF,dp[h[i1][j1]][i][j]+dp[h[i2][j2]][i][j]);
			if (ans[i][j] != INF) pq.push({ans[i][j],{i,j}});
		}
	}
	while (!pq.empty()){
		auto [tt,t] = pq.top();pq.pop();
		if (ans[t.first][t.second] < tt) continue;
		ans[t.first][t.second] = tt;
		for (int ii = 0;ii<4;ii++)pq.push({tt+1,{ma[t.first][t.second][ii].first,ma[t.first][t.second][ii].second}});
	}
	return ans;
}
int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> N >> W >> H;
	memset(ma,-1,sizeof(ma));
	memset(dist,INF,sizeof(dist));
	for (int i = 0;i<H;i++) cin >> G[i];
	for (int i = 0;i<H;i++){
		for (int j = 0;j<W;j++){
			move(i,j,0);
			move(i,j,1);
			move(i,j,2);
			move(i,j,3);
		}
	}
	int cnt = 1;
	for (int i = 1;i<=N;i++){
		for (int j = i;j<=N;j++){
			h[i][j] = cnt++;
		}
	}
	memset(dp,INF,sizeof(dp));
	for (int l = 1;l<=N;l++){
		for (int i = 0;i<H;i++){
			for (int j = 0;j<W;j++){
				if (G[i][j] == l+'0') calc(i,j,l);
			}
		}
	}
	for (auto &a:dp[0]) for (auto &aa:a) aa = 0;
	for (int k = 1;k<N;k++){
		for (int i = 1;i+k<=N;i++){
			vector<vector<int>> t;
			t = calc1(i,i,i+1,i+k);
			for (int I = 0;I<H;I++){
				for (int J = 0;J<W;J++){
					dp[h[i][i+k]][I][J] = min(dp[h[i][i+k]][I][J],t[I][J]);
				}
			}
			if (k == 1) continue;
			t = calc1(i,i+k-1,i+k,i+k);
			for (int I = 0;I<H;I++){
				for (int J = 0;J<W;J++){
					dp[h[i][i+k]][I][J] = min(dp[h[i][i+k]][I][J],t[I][J]);
				}
			}
		}
	}
	int ans = INF;
	for (auto &a:dp[h[1][N]]) for (auto &aa:a) ans = min(ans,aa);
	cout << (ans==INF?-1:ans);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 66772 KB Output is correct
2 Correct 25 ms 66772 KB Output is correct
3 Correct 26 ms 66772 KB Output is correct
4 Correct 25 ms 66812 KB Output is correct
5 Correct 25 ms 66772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 66772 KB Output is correct
2 Correct 25 ms 66772 KB Output is correct
3 Correct 26 ms 66772 KB Output is correct
4 Correct 25 ms 66812 KB Output is correct
5 Correct 25 ms 66772 KB Output is correct
6 Correct 26 ms 66836 KB Output is correct
7 Correct 24 ms 66772 KB Output is correct
8 Correct 28 ms 66844 KB Output is correct
9 Correct 25 ms 66772 KB Output is correct
10 Correct 27 ms 66760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 66772 KB Output is correct
2 Correct 25 ms 66772 KB Output is correct
3 Correct 26 ms 66772 KB Output is correct
4 Correct 25 ms 66812 KB Output is correct
5 Correct 25 ms 66772 KB Output is correct
6 Correct 26 ms 66836 KB Output is correct
7 Correct 24 ms 66772 KB Output is correct
8 Correct 28 ms 66844 KB Output is correct
9 Correct 25 ms 66772 KB Output is correct
10 Correct 27 ms 66760 KB Output is correct
11 Execution timed out 1588 ms 68980 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 66772 KB Output is correct
2 Correct 25 ms 66772 KB Output is correct
3 Correct 26 ms 66772 KB Output is correct
4 Correct 25 ms 66812 KB Output is correct
5 Correct 25 ms 66772 KB Output is correct
6 Correct 26 ms 66836 KB Output is correct
7 Correct 24 ms 66772 KB Output is correct
8 Correct 28 ms 66844 KB Output is correct
9 Correct 25 ms 66772 KB Output is correct
10 Correct 27 ms 66760 KB Output is correct
11 Execution timed out 1588 ms 68980 KB Time limit exceeded
12 Halted 0 ms 0 KB -