Submission #122426

# Submission time Handle Problem Language Result Execution time Memory
122426 2019-06-28T07:49:20 Z 김세빈(#2988) Robots (APIO13_robots) C++14
100 / 100
1348 ms 56104 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++){
			int &_v = dp[j][i];
			for(pii &t: T[i]){
				_v = min(_v, dp[j][t.first] + dp[j][t.second]);
			}
			v = min(v, _v);
		}
		if(v == 1e9) continue;
		
		for(j=1; j<=s; j++){
			if(dp[j][i] < v + s){
				X[dp[j][i] - v].push_back(j);
			}
		}
		
		cnt ++; for(; !Q.empty(); Q.pop());
		
		for(j=0; j<s; 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]);
   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 16 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 14 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 16 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 14 ms 14720 KB Output is correct
6 Correct 17 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 15 ms 14592 KB Output is correct
10 Correct 15 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 16 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 14 ms 14720 KB Output is correct
6 Correct 17 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 15 ms 14592 KB Output is correct
10 Correct 15 ms 14720 KB Output is correct
11 Correct 80 ms 25592 KB Output is correct
12 Correct 45 ms 24952 KB Output is correct
13 Correct 20 ms 17408 KB Output is correct
14 Correct 216 ms 30172 KB Output is correct
15 Correct 58 ms 25592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14592 KB Output is correct
2 Correct 16 ms 14592 KB Output is correct
3 Correct 14 ms 14592 KB Output is correct
4 Correct 14 ms 14720 KB Output is correct
5 Correct 14 ms 14720 KB Output is correct
6 Correct 17 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 15 ms 14592 KB Output is correct
10 Correct 15 ms 14720 KB Output is correct
11 Correct 80 ms 25592 KB Output is correct
12 Correct 45 ms 24952 KB Output is correct
13 Correct 20 ms 17408 KB Output is correct
14 Correct 216 ms 30172 KB Output is correct
15 Correct 58 ms 25592 KB Output is correct
16 Correct 21 ms 19968 KB Output is correct
17 Correct 1348 ms 56104 KB Output is correct
18 Correct 257 ms 43348 KB Output is correct
19 Correct 24 ms 19704 KB Output is correct
20 Correct 722 ms 43528 KB Output is correct