답안 #116022

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
116022 2019-06-10T08:23:14 Z MAMBA 포탈들 (BOI14_portals) C++17
100 / 100
988 ms 168448 KB
#include <bits/stdc++.h> 

using namespace std;

#define rep(i , j , k) for (int i = j; i < (int)k; i++)
#define pb push_back
#define mt make_tuple
#define all(x) x.begin(),x.end()

typedef long long ll;
typedef pair<int , int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef complex<ld> point;
typedef pair<ld , ld> pld;
typedef vector<ll> vi;

inline void fileIO(string s) {
	//	freopen((s + ".in").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}

template<class T , class S>
inline bool smin(T &a, S b) { return (T)b < a ? a = b , 1 : 0; }
template<class T , class S>
inline bool smax(T &a, S b) { return a < (T)b ? a = b , 1 : 0; }

constexpr int N = 1000 + 10;
constexpr int MOD = 1e9 + 7;

template<typename T>
inline T mod(T &v) { return v = (v % MOD + MOD) % MOD; }
template<typename S, typename T>
inline S add(S &l, T r) { return mod(l += r); }

int r, w, L[N][N] ,U[N][N], R[N][N], D[N][N], d1[N][N] , d2[N][N];
pii s , c;
int dx[4] = {1, 0 , -1, 0};
int dy[4] = {0 , 1 , 0 , -1};
pii q[N * N];
int ql, qr;
vector<pair<int , pii>> adj[N][N];
string arr[N];

inline bool valid(int x, int y) {
	return x >= 0 && y >= 0 && x < r && y < w && arr[x][y] != '#';
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> r >> w;

	rep(i , 0 , r) {
		cin >> arr[i];
		int id = find(all(arr[i]) , 'S') - arr[i].begin();
		if (id != w)
			s = {i , id };
		id = find(all(arr[i]) , 'C') - arr[i].begin();
		if (id != w)
			c = {i , id };
	}

	rep(i , 0 , r)
		rep(j , 0 , w) { 
			if (j == 0 && arr[i][j] != '#') L[i][j] = -1;
			else L[i][j] = (arr[i][j] == '#') ? j : L[i][j - 1];
		}
	rep(i , 0 , r)
		for (int j = w - 1; ~j; j--) {
			if (j == w - 1 && arr[i][j] != '#') R[i][j] = w;
			else	R[i][j] = (arr[i][j] == '#') ? j : R[i][j + 1];
		}
	rep(i , 0 , r)
		rep(j , 0 , w) {
			if (i == 0 && arr[i][j] != '#') U[i][j] = -1;
			else
			U[i][j] = (arr[i][j] == '#') ? i : U[i - 1][j];
		}
	for (int i = r - 1; ~i; i--) {
		rep(j , 0 , w) { 
			if (i == r - 1 && arr[i][j] != '#') D[i][j] = r;
			else D[i][j] = (arr[i][j] == '#') ? i : D[i + 1][j];
		}
	}
	memset(d1, 63, sizeof(d1));
	rep(i , 0 , r)
		rep(j , 0 , w)
		if (arr[i][j] != '#' && ( 
				L[i][j] + 1 == j || 
				R[i][j] - 1 == j ||
				U[i][j] + 1 == i ||
				D[i][j] - 1 == i)) {
			d1[i][j] = 1;
			q[qr++] = {i , j};
		}

	while (ql != qr) {
		pii me = q[ql++];
		rep(i , 0 , 4) {
			int x_ = me.first + dx[i];
			int y_ = me.second + dy[i];
			if (valid(x_ , y_) && d1[me.first][me.second] + 1 < d1[x_][y_]) {
				d1[x_][y_] = d1[me.first][me.second] + 1;
				q[qr++] = {x_ , y_};
			}
		}
	}

	rep(i , 0 , r) 
		rep(j , 0 , w) {
			if (arr[i][j] == '#') continue;
			rep(z , 0 , 4) {
				int x = i + dx[z];
				int y = j + dy[z];
				if (valid(x , y)) adj[i][j].pb({1 , {x , y}});
			}
			adj[i][j].pb({d1[i][j] , {i , L[i][j] + 1}});
			adj[i][j].pb({d1[i][j] , {i , R[i][j] - 1}});
			adj[i][j].pb({d1[i][j] , {U[i][j] + 1 , j}});
			adj[i][j].pb({d1[i][j] , {D[i][j] - 1 , j}});
		}

	memset(d2, 63, sizeof(d2));

	d2[s.first][s.second] = 0;
	set<pair<int , pii>> st;
	st.insert({0 , {s.first , s.second}});
	while (!st.empty()) {
		int x , y;
		tie(x , y) = st.begin()->second;
		st.erase(st.begin());
		for (auto e : adj[x][y]) {
			int x_ = e.second.first;
			int y_ = e.second.second;
			if (d2[x][y] + e.first < d2[x_][y_]) {
				st.erase({d2[x_][y_] , {x_ , y_}});
				d2[x_][y_] = d2[x][y] + e.first;
				st.insert({d2[x_][y_] , {x_ , y_}});
			}
		}
	}	
	


	cout << d2[c.first][c.second] << endl;

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 32384 KB Output is correct
2 Correct 28 ms 32504 KB Output is correct
3 Correct 29 ms 32512 KB Output is correct
4 Correct 28 ms 32384 KB Output is correct
5 Correct 28 ms 32504 KB Output is correct
6 Correct 30 ms 32504 KB Output is correct
7 Correct 28 ms 32512 KB Output is correct
8 Correct 29 ms 32636 KB Output is correct
9 Correct 28 ms 32384 KB Output is correct
10 Correct 29 ms 32376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 32384 KB Output is correct
2 Correct 32 ms 32512 KB Output is correct
3 Correct 30 ms 32504 KB Output is correct
4 Correct 30 ms 32512 KB Output is correct
5 Correct 30 ms 32504 KB Output is correct
6 Correct 30 ms 32504 KB Output is correct
7 Correct 30 ms 32512 KB Output is correct
8 Correct 30 ms 32512 KB Output is correct
9 Correct 31 ms 33480 KB Output is correct
10 Correct 32 ms 33380 KB Output is correct
11 Correct 32 ms 33280 KB Output is correct
12 Correct 32 ms 33280 KB Output is correct
13 Correct 32 ms 33280 KB Output is correct
14 Correct 29 ms 32376 KB Output is correct
15 Correct 32 ms 33408 KB Output is correct
16 Correct 30 ms 32384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 32384 KB Output is correct
2 Correct 30 ms 32504 KB Output is correct
3 Correct 29 ms 32448 KB Output is correct
4 Correct 30 ms 32512 KB Output is correct
5 Correct 48 ms 38264 KB Output is correct
6 Correct 60 ms 38392 KB Output is correct
7 Correct 52 ms 38648 KB Output is correct
8 Correct 53 ms 38648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 32376 KB Output is correct
2 Correct 31 ms 32440 KB Output is correct
3 Correct 31 ms 32504 KB Output is correct
4 Correct 32 ms 32384 KB Output is correct
5 Correct 31 ms 32504 KB Output is correct
6 Correct 85 ms 32504 KB Output is correct
7 Correct 30 ms 32512 KB Output is correct
8 Correct 30 ms 32512 KB Output is correct
9 Correct 34 ms 33400 KB Output is correct
10 Correct 32 ms 33400 KB Output is correct
11 Correct 32 ms 33280 KB Output is correct
12 Correct 31 ms 33272 KB Output is correct
13 Correct 32 ms 33272 KB Output is correct
14 Correct 49 ms 38244 KB Output is correct
15 Correct 62 ms 38392 KB Output is correct
16 Correct 53 ms 38776 KB Output is correct
17 Correct 51 ms 38520 KB Output is correct
18 Correct 54 ms 39416 KB Output is correct
19 Correct 60 ms 40440 KB Output is correct
20 Correct 58 ms 40308 KB Output is correct
21 Correct 49 ms 38396 KB Output is correct
22 Correct 50 ms 38392 KB Output is correct
23 Correct 51 ms 38520 KB Output is correct
24 Correct 59 ms 40268 KB Output is correct
25 Correct 31 ms 32376 KB Output is correct
26 Correct 32 ms 33272 KB Output is correct
27 Correct 31 ms 32376 KB Output is correct
28 Correct 48 ms 38776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 32384 KB Output is correct
2 Correct 31 ms 32512 KB Output is correct
3 Correct 30 ms 32512 KB Output is correct
4 Correct 32 ms 32384 KB Output is correct
5 Correct 36 ms 32480 KB Output is correct
6 Correct 30 ms 32512 KB Output is correct
7 Correct 31 ms 32504 KB Output is correct
8 Correct 32 ms 32512 KB Output is correct
9 Correct 32 ms 33400 KB Output is correct
10 Correct 31 ms 33408 KB Output is correct
11 Correct 32 ms 33324 KB Output is correct
12 Correct 32 ms 33272 KB Output is correct
13 Correct 31 ms 33408 KB Output is correct
14 Correct 48 ms 38264 KB Output is correct
15 Correct 115 ms 38392 KB Output is correct
16 Correct 53 ms 38652 KB Output is correct
17 Correct 51 ms 38648 KB Output is correct
18 Correct 56 ms 39252 KB Output is correct
19 Correct 60 ms 40312 KB Output is correct
20 Correct 58 ms 40312 KB Output is correct
21 Correct 49 ms 38392 KB Output is correct
22 Correct 50 ms 38392 KB Output is correct
23 Correct 51 ms 38520 KB Output is correct
24 Correct 539 ms 130712 KB Output is correct
25 Correct 988 ms 168448 KB Output is correct
26 Correct 743 ms 167752 KB Output is correct
27 Correct 722 ms 167884 KB Output is correct
28 Correct 442 ms 117432 KB Output is correct
29 Correct 458 ms 118968 KB Output is correct
30 Correct 509 ms 120588 KB Output is correct
31 Correct 54 ms 40312 KB Output is correct
32 Correct 707 ms 167672 KB Output is correct
33 Correct 30 ms 32376 KB Output is correct
34 Correct 33 ms 33400 KB Output is correct
35 Correct 513 ms 138544 KB Output is correct
36 Correct 28 ms 32384 KB Output is correct
37 Correct 43 ms 38776 KB Output is correct
38 Correct 387 ms 128480 KB Output is correct
39 Correct 339 ms 109024 KB Output is correct