답안 #122420

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122420 2019-06-28T07:40:48 Z 김세빈(#2988) 로봇 (APIO13_robots) C++14
60 / 100
1500 ms 56096 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

vector <int> V[303030], X[303030];
vector <pii> T[55];
queue <pii> Q;
char S[555][555];
int chk[555][555][4], vis[303030];
int P[303030], K[303030];
int dp[303030][55];
int N[10][10];
int n, m, k, s, cnt, ans;

int dfs(int x, int y, int d)
{
	if(chk[x][y][d] >= 0) return chk[x][y][d];
	else if(chk[x][y][d] == -1) return -1;
	
	chk[x][y][d] = -1;
	
	int ret, _d;
	
	if(S[x][y] == 'A') _d = (d + 1) % 4;
	else if(S[x][y] == 'C') _d = (d + 3) % 4;
	else _d = d;
	
	if(_d == 0){
		if(x == n - 1 || S[x + 1][y] == 'x') ret = x * m + y;
		else ret = dfs(x + 1, y, _d);
	}
	else if(_d == 1){
		if(y == m - 1 || S[x][y + 1] == 'x') ret = x * m + y;
		else ret = dfs(x, y + 1, _d);
	}
	else if(_d == 2){
		if(x == 0 || S[x - 1][y] == 'x') ret = x * m + y;
		else ret = dfs(x - 1, y, _d);
	}
	else{
		if(y == 0 || S[x][y - 1] == 'x') ret = x * m + y;
		else ret = dfs(x, y - 1, _d);
	}
	
	return chk[x][y][d] = ret;
}

int main()
{
	int i, j, l;
	
	scanf("%d%d%d", &k, &m, &n);
	
	{
		int f = 0;
		
		for(i=1; i<=k; i++){
			for(j=i; j>=1; j--){
				N[j][i] = f++;
			}
		}
		
		for(i=1; i<=k; i++){
			for(j=1; j<=i; j++){
				for(l=1; l<j; l++){
					T[N[l][i]].emplace_back(N[l][j - 1], N[j][i]);
				}
			}
		}
	}
	
	for(i=0; i<n; i++){
		scanf("%s", S[i]);
		for(j=0; j<m; j++){
			for(l=0; l<4; l++){
				chk[i][j][l] = -2;
			}
			
			if('1' <= S[i][j] && S[i][j] <= '0' + k){
				K[i * m + j] = ++s; P[s] = i * m + j;
				dp[s][(int)(S[i][j] - '0') * (S[i][j] - '0' - 1) / 2] = -1e9;
			}
		}
	}
	
	for(int r=0; r<s; ){
		int p = P[++r];
		for(l=0; l<4; l++){
			int t = dfs(p / m, p % m, l);
			if(t != -1 && t != p){
				if(!K[t]) K[t] = ++s, P[s] = t;
				V[K[p]].push_back(K[t]);
			}
		}
	}
	
	for(j=1; j<=s; j++){
		for(l=0; l<k*(k+1)/2; l++){
			dp[j][l] += 1e9;
		}
	}
	
	for(i=0; i<k*(k+1)/2; i++){
		int v = 1e9;
		for(j=1; j<=s; j++){
			for(pii &t: T[i]){
				dp[j][i] = min(dp[j][i], dp[j][t.first] + dp[j][t.second]);
			}
			v = min(v, dp[j][i]);
		}
		if(v == 1e9) continue;
		
		for(j=1; j<=s; j++){
			if(dp[j][i] < v + n * m){
				X[dp[j][i] - v].push_back(j);
			}
		}
		
		cnt ++; for(; !Q.empty(); Q.pop());
		
		for(j=0; j<n*m; j++){
			for(int &t: X[j]) if(vis[t] != cnt){
				vis[t] = cnt;
				Q.push(pii(t, j));
			}
			X[j].clear();
			
			for(; !Q.empty() && Q.front().second == j;){
				int p = Q.front().first; Q.pop();
				dp[p][i] = j + v;
				for(int &t: V[p]) if(vis[t] != cnt){
					Q.push(pii(t, j + 1));
					vis[t] = cnt;
				}
			}
		}
	}
	
	ans = 1e9;
	
	for(i=1; i<=s; i++){
		ans = min(ans, dp[i][k * (k + 1) / 2 - 1]);
	}
	
	if(ans < 1e9) printf("%d\n", ans);
	else printf("-1\n");
	
	return 0;
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &k, &m, &n);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", S[i]);
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 14 ms 14592 KB Output is correct
3 Correct 13 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 15 ms 14592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 14 ms 14592 KB Output is correct
3 Correct 13 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 15 ms 14592 KB Output is correct
6 Correct 14 ms 14592 KB Output is correct
7 Correct 14 ms 14592 KB Output is correct
8 Correct 14 ms 14592 KB Output is correct
9 Correct 14 ms 14592 KB Output is correct
10 Correct 14 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 14 ms 14592 KB Output is correct
3 Correct 13 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 15 ms 14592 KB Output is correct
6 Correct 14 ms 14592 KB Output is correct
7 Correct 14 ms 14592 KB Output is correct
8 Correct 14 ms 14592 KB Output is correct
9 Correct 14 ms 14592 KB Output is correct
10 Correct 14 ms 14720 KB Output is correct
11 Correct 95 ms 25592 KB Output is correct
12 Correct 30 ms 24944 KB Output is correct
13 Correct 23 ms 17548 KB Output is correct
14 Correct 259 ms 30200 KB Output is correct
15 Correct 64 ms 25592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 14 ms 14592 KB Output is correct
3 Correct 13 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 15 ms 14592 KB Output is correct
6 Correct 14 ms 14592 KB Output is correct
7 Correct 14 ms 14592 KB Output is correct
8 Correct 14 ms 14592 KB Output is correct
9 Correct 14 ms 14592 KB Output is correct
10 Correct 14 ms 14720 KB Output is correct
11 Correct 95 ms 25592 KB Output is correct
12 Correct 30 ms 24944 KB Output is correct
13 Correct 23 ms 17548 KB Output is correct
14 Correct 259 ms 30200 KB Output is correct
15 Correct 64 ms 25592 KB Output is correct
16 Correct 42 ms 19968 KB Output is correct
17 Execution timed out 1507 ms 56096 KB Time limit exceeded
18 Halted 0 ms 0 KB -