답안 #46118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
46118 2018-04-17T10:28:40 Z RockyB 로봇 (APIO13_robots) C++17
100 / 100
809 ms 102952 KB
/// In The Name Of God
 
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#include <bits/stdc++.h>
 
#define f first
#define s second
 
#define pb emplace_back
#define pp pop_back
#define mp make_pair
 
#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
 
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
#define nl '\n'
#define ioi exit(0);
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
const int N = (int)500 + 1;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
 
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
 
using namespace std;
 
int n, m, h;
char a[N][N];
 
int dp[55][N][N];
int id[10][10];
 
pair <int, int> res[N][N][4];
bool add[N][N][4];
bool was[N][N][4];
 

vector < pair <int, int> > q[N * N];
 
inline bool check(int x, int y) {
	return 0 <= x && x < n && 0 <= y && y < m && a[x][y] != 'x';
}
inline pair <int, int> go(int x, int y, int t) {
	if (a[x][y] == 'C') t = (t + 1) % 4;
	else if (a[x][y] == 'A') t = (t + 3) % 4;
	if (was[x][y][t]) return res[x][y][t];
	was[x][y][t] = 1;
	if (check(x + dx[t], y + dy[t])) return res[x][y][t] = go(x + dx[t], y + dy[t], t);
	return res[x][y][t] = {x, y};
}
 
int tin[N][N];
int main() {
	#ifdef IOI2018
		freopen ("in.txt", "r", stdin);
	#endif
	scanf ("%d%d%d", &h, &m, &n);
 
	int tmr = 0;
	for (int i = 0; i <= h; ++i) {
		for (int j = i; j <= h; ++j) {
			id[i][j] = tmr++;
		}
	}
	memset(dp, 0x3f, sizeof(dp));
	memset(res, -1, sizeof(res));

	for (int i = 0; i < n; ++i) {
		scanf ("%s", a[i]);
		for (int j = 0; j < m; ++j) {
			if (isdigit(a[i][j])) {
				int d = a[i][j] - '1';
				dp[id[d][d]][i][j] = 0;
			}
		}
	}	
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (check(i, j)) {
				for (int t = 0; t < 4; t++) {
					go(i, j, t);
				}
			}
		}
	}

	tmr = 0;
	for (int len = 0; len < h; len++) {
		for (int l = 0; l + len < h; l++) {
			int r = l + len;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					for (int mid = l; mid < r; mid++) {
						dp[id[l][r]][i][j] = min(dp[id[l][r]][i][j], dp[id[l][mid]][i][j] + dp[id[mid + 1][r]][i][j]);
					}
				}
			}

			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (dp[id[l][r]][i][j] < inf) q[dp[id[l][r]][i][j]].pb(mp(i, j));
				}
			}

			++tmr;
			for (int cost = 0, up = n * m; cost <= up; cost++) {
				while (sz(q[cost])) {
					pair <int, int> v = q[cost].back();
					q[cost].pp();
					tin[v.f][v.s] = tmr;
					for (int i = 0; i < 4; i++) {
						pair <int, int> to = go(v.f, v.s, i);
						if (tin[to.f][to.s] != tmr && dp[id[l][r]][to.f][to.s] > dp[id[l][r]][v.f][v.s] + 1) {
							dp[id[l][r]][to.f][to.s] = dp[id[l][r]][v.f][v.s] + 1;
							q[dp[id[l][r]][to.f][to.s]].pb(to);
						}
					}
				}
				//q[cost].shrink_to_fit();
			}

		}
	}	
	int ans = inf;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			ans = min(ans, dp[id[0][h - 1]][i][j]);
		}
	}
	if (ans >= inf) ans = -1;
	cout << ans;
	ioi
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d%d%d", &h, &m, &n);
  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:80:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%s", a[i]);
   ~~~~~~^~~~~~~~~~~~
robots.cpp:105:91: warning: array subscript is above array bounds [-Warray-bounds]
       dp[id[l][r]][i][j] = min(dp[id[l][r]][i][j], dp[id[l][mid]][i][j] + dp[id[mid + 1][r]][i][j]);
                                                                              ~~~~~~~~~~~~~^
robots.cpp:105:88: warning: array subscript is above array bounds [-Warray-bounds]
       dp[id[l][r]][i][j] = min(dp[id[l][r]][i][j], dp[id[l][mid]][i][j] + dp[id[mid + 1][r]][i][j]);
                                                                              ~~~~~~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 68088 KB Output is correct
2 Correct 51 ms 68200 KB Output is correct
3 Correct 52 ms 68200 KB Output is correct
4 Correct 52 ms 68200 KB Output is correct
5 Correct 53 ms 68272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 68088 KB Output is correct
2 Correct 51 ms 68200 KB Output is correct
3 Correct 52 ms 68200 KB Output is correct
4 Correct 52 ms 68200 KB Output is correct
5 Correct 53 ms 68272 KB Output is correct
6 Correct 48 ms 68272 KB Output is correct
7 Correct 52 ms 68300 KB Output is correct
8 Correct 69 ms 68348 KB Output is correct
9 Correct 52 ms 68352 KB Output is correct
10 Correct 52 ms 68412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 68088 KB Output is correct
2 Correct 51 ms 68200 KB Output is correct
3 Correct 52 ms 68200 KB Output is correct
4 Correct 52 ms 68200 KB Output is correct
5 Correct 53 ms 68272 KB Output is correct
6 Correct 48 ms 68272 KB Output is correct
7 Correct 52 ms 68300 KB Output is correct
8 Correct 69 ms 68348 KB Output is correct
9 Correct 52 ms 68352 KB Output is correct
10 Correct 52 ms 68412 KB Output is correct
11 Correct 150 ms 73608 KB Output is correct
12 Correct 55 ms 73608 KB Output is correct
13 Correct 82 ms 73608 KB Output is correct
14 Correct 282 ms 78752 KB Output is correct
15 Correct 116 ms 78752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 68088 KB Output is correct
2 Correct 51 ms 68200 KB Output is correct
3 Correct 52 ms 68200 KB Output is correct
4 Correct 52 ms 68200 KB Output is correct
5 Correct 53 ms 68272 KB Output is correct
6 Correct 48 ms 68272 KB Output is correct
7 Correct 52 ms 68300 KB Output is correct
8 Correct 69 ms 68348 KB Output is correct
9 Correct 52 ms 68352 KB Output is correct
10 Correct 52 ms 68412 KB Output is correct
11 Correct 150 ms 73608 KB Output is correct
12 Correct 55 ms 73608 KB Output is correct
13 Correct 82 ms 73608 KB Output is correct
14 Correct 282 ms 78752 KB Output is correct
15 Correct 116 ms 78752 KB Output is correct
16 Correct 186 ms 78752 KB Output is correct
17 Correct 809 ms 102952 KB Output is correct
18 Correct 296 ms 102952 KB Output is correct
19 Correct 190 ms 102952 KB Output is correct
20 Correct 561 ms 102952 KB Output is correct