답안 #122418

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

using namespace std;

typedef pair <int, int> pii;

vector <int> V[303030], X[303030];
queue <pii> Q;
char S[555][555];
int chk[555][555][4], vis[303030];;
int P[303030];
int dp[303030][55];
int _N[10][10], _X[55], _Y[55];
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);
	
	for(i=1; i<=k; i++){
		for(j=i; j>=1; j--){
			_N[i][j] = s;
			_X[s] = i; _Y[s] = j;
			s ++;
		}
	}
	
	s = 0;
	
	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;
			}
			
			for(l=0; l<k*(k+1)/2; l++){
				dp[i * m + j][l] = 1e9;
			}
			
			if('1' <= S[i][j] && S[i][j] <= '0' + k){
				dp[i * m + j][(int)(S[i][j] - '0') * (S[i][j] - '0' - 1) / 2] = 0;
				P[++s] = i * m + j; vis[i * m + j] = 1;
			}
		}
	}
	
	cnt ++;
	
	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){
				V[p].push_back(t);
				if(vis[t] != cnt){
					vis[t] = cnt;
					P[++s] = t;
				}
			}
		}
	}
	
	for(i=0; i<k*(k+1)/2; i++){
		int v = 1e9;
		for(j=1; j<=s; j++){
			v = min(v, dp[P[j]][i]);
		}
		if(v == 1e9) continue;
		
		for(j=1; j<=s; j++){
			if(dp[P[j]][i] < v + n * m){
				X[dp[P[j]][i] - v].push_back(P[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;
				}
			}
		}
		
		int x = _X[i], y = _Y[i], z;
		
		if(n <= 300){
			for(j=1; j<=s; j++){
				for(z=y-1; z>=1; z--){
					dp[P[j]][_N[x][z]] = min(dp[P[j]][_N[x][z]], dp[P[j]][_N[y - 1][z]] + dp[P[j]][i]);
				}
			}
		}
	}
	
	ans = 1e9;
	
	for(i=1; i<=s; i++){
		ans = min(ans, dp[P[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:53: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:66: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 15 ms 14592 KB Output is correct
2 Correct 15 ms 14592 KB Output is correct
3 Correct 16 ms 14592 KB Output is correct
4 Correct 15 ms 14720 KB Output is correct
5 Correct 16 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14592 KB Output is correct
2 Correct 15 ms 14592 KB Output is correct
3 Correct 16 ms 14592 KB Output is correct
4 Correct 15 ms 14720 KB Output is correct
5 Correct 16 ms 14720 KB Output is correct
6 Correct 16 ms 14592 KB Output is correct
7 Correct 16 ms 14592 KB Output is correct
8 Correct 21 ms 14528 KB Output is correct
9 Correct 15 ms 14592 KB Output is correct
10 Correct 15 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14592 KB Output is correct
2 Correct 15 ms 14592 KB Output is correct
3 Correct 16 ms 14592 KB Output is correct
4 Correct 15 ms 14720 KB Output is correct
5 Correct 16 ms 14720 KB Output is correct
6 Correct 16 ms 14592 KB Output is correct
7 Correct 16 ms 14592 KB Output is correct
8 Correct 21 ms 14528 KB Output is correct
9 Correct 15 ms 14592 KB Output is correct
10 Correct 15 ms 14720 KB Output is correct
11 Correct 177 ms 38264 KB Output is correct
12 Correct 43 ms 37624 KB Output is correct
13 Correct 41 ms 36772 KB Output is correct
14 Correct 351 ms 39544 KB Output is correct
15 Correct 104 ms 38264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14592 KB Output is correct
2 Correct 15 ms 14592 KB Output is correct
3 Correct 16 ms 14592 KB Output is correct
4 Correct 15 ms 14720 KB Output is correct
5 Correct 16 ms 14720 KB Output is correct
6 Correct 16 ms 14592 KB Output is correct
7 Correct 16 ms 14592 KB Output is correct
8 Correct 21 ms 14528 KB Output is correct
9 Correct 15 ms 14592 KB Output is correct
10 Correct 15 ms 14720 KB Output is correct
11 Correct 177 ms 38264 KB Output is correct
12 Correct 43 ms 37624 KB Output is correct
13 Correct 41 ms 36772 KB Output is correct
14 Correct 351 ms 39544 KB Output is correct
15 Correct 104 ms 38264 KB Output is correct
16 Incorrect 72 ms 73724 KB Output isn't correct
17 Halted 0 ms 0 KB -