Submission #257354

# Submission time Handle Problem Language Result Execution time Memory
257354 2020-08-04T06:56:29 Z 임성재(#5052) None (JOI16_skating) C++17
78 / 100
3000 ms 72092 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(NULL)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define em emplace
#define all(v) (v).begin(), (v).end()
#define pre(a) cout<<fixed; cout.precision(a)
#define mp make_pair

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<pii,pii> pipii;
const int inf = 1e9;
const ll INF = 1e18;

int n, m;
string s[1010];
priority_queue<pipii, vector<pipii>, greater<pipii>> pQ;
pii S;
pii E;
bool chk[1010][1010];
int dis[1010][1010];

int main() {
	fast;

	cin >> n >> m;

	for(int i=0; i<n; i++) {
		cin >> s[i];
	}

	cin >> S.fi >> S.se;
	cin >> E.fi >> E.se;

	S.fi--;
	S.se--;
	E.fi--;
	E.se--;

	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			dis[i][j] = n * m;
		}
	}

	int ans = inf;
	pQ.em(mp(0, 1), S);

	while(pQ.size()) {
		int cost = pQ.top().fi.fi;
		int dir = pQ.top().fi.se;
		pii cur = pQ.top().se;

		pQ.pop();

		if(cur == E) {
			ans = min(cost, ans);
			break;
		}
		if(chk[cur.fi][cur.se]) continue;
		chk[cur.fi][cur.se] = true;
		
		if(dir!=1) {
			int l, r;
			for(l = cur.fi; 0 <= l && s[l][cur.se] == '.'; l--);
			for(r = cur.fi; r < n && s[r][cur.se] == '.'; r++);

			l++;
			r--;

			for(int i=l; i<=r; i++) {
				if(i < cur.fi) {
					int d = min(2 * i - 2 * l + 1, 2 * cur.fi - 2 * i);

					if(cost + d >= dis[i][cur.se]) continue;
					dis[i][cur.se] = cost + d;
					pQ.em(mp(cost + d, 1), mp(i, cur.se));
				}
				else if(i > cur.fi) {
					int d = min(2 * r - 2 * i + 1, 2 * i - 2 * cur.fi);				
					
					if(cost + d >= dis[i][cur.se]) continue;
					dis[i][cur.se] = cost + d;
					pQ.em(mp(cost + d, 1), mp(i, cur.se));
				}
			}
		}

		if(dir != 2) {
			int l, r;
			for(l = cur.se; 0 <= l && s[cur.fi][l] == '.'; l--);
			for(r = cur.se; r < m && s[cur.fi][r] == '.'; r++);

			l++;
			r--;

			for(int i=l; i<=r; i++) {
				if(i < cur.se) {
					int d = min(2 * i - 2 * l + 1, 2 * cur.se - 2 * i);

					if(cost + d >= dis[cur.fi][i]) continue;
					dis[cur.fi][i] = cost + d;
					pQ.em(mp(cost + d, 2), mp(cur.fi, i));
				}
				else if(i > cur.se) {
					int d = min(2 * r - 2 * i + 1, 2 * i - 2 * cur.se);				
					
					if(cost + d >= dis[cur.fi][i]) continue;
					dis[cur.fi][i] = cost + d;
					pQ.em(mp(cost + d, 2), mp(cur.fi, i));
				}
			}
		}
	}

	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			dis[i][j] = n * m;
			chk[i][j] = false;
		}
	}

	pQ.em(mp(0, 2), S);

	while(pQ.size()) {
		int cost = pQ.top().fi.fi;
		int dir = pQ.top().fi.se;
		pii cur = pQ.top().se;

		pQ.pop();

		if(cur == E) {
			ans = min(cost, ans);
			break;
		}
		if(chk[cur.fi][cur.se]) continue;
		chk[cur.fi][cur.se] = true;
		
		if(dir!=1) {
			int l, r;
			for(l = cur.fi; 0 <= l && s[l][cur.se] == '.'; l--);
			for(r = cur.fi; r < n && s[r][cur.se] == '.'; r++);

			l++;
			r--;

			for(int i=l; i<=r; i++) {
				if(i < cur.fi) {
					int d = min(2 * i - 2 * l + 1, 2 * cur.fi - 2 * i);

					if(cost + d >= dis[i][cur.se]) continue;
					dis[i][cur.se] = cost + d;
					pQ.em(mp(cost + d, 1), mp(i, cur.se));
				}
				else if(i > cur.fi) {
					int d = min(2 * r - 2 * i + 1, 2 * i - 2 * cur.fi);				
					
					if(cost + d >= dis[i][cur.se]) continue;
					dis[i][cur.se] = cost + d;
					pQ.em(mp(cost + d, 1), mp(i, cur.se));
				}
			}
		}

		if(dir != 2) {
			int l, r;
			for(l = cur.se; 0 <= l && s[cur.fi][l] == '.'; l--);
			for(r = cur.se; r < m && s[cur.fi][r] == '.'; r++);

			l++;
			r--;

			for(int i=l; i<=r; i++) {
				if(i < cur.se) {
					int d = min(2 * i - 2 * l + 1, 2 * cur.se - 2 * i);

					if(cost + d >= dis[cur.fi][i]) continue;
					dis[cur.fi][i] = cost + d;
					pQ.em(mp(cost + d, 2), mp(cur.fi, i));
				}
				else if(i > cur.se) {
					int d = min(2 * r - 2 * i + 1, 2 * i - 2 * cur.se);				
					
					if(cost + d >= dis[cur.fi][i]) continue;
					dis[cur.fi][i] = cost + d;
					pQ.em(mp(cost + d, 2), mp(cur.fi, i));
				}
			}
		}
	}

	if(ans == inf) cout << "-1";
	else cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 44 ms 3572 KB Output is correct
12 Correct 50 ms 5608 KB Output is correct
13 Correct 21 ms 5616 KB Output is correct
14 Correct 13 ms 1664 KB Output is correct
15 Correct 1 ms 1408 KB Output is correct
16 Correct 46 ms 3560 KB Output is correct
17 Correct 18 ms 1408 KB Output is correct
18 Correct 7 ms 1408 KB Output is correct
19 Correct 23 ms 1728 KB Output is correct
20 Correct 24 ms 3708 KB Output is correct
21 Correct 2 ms 1408 KB Output is correct
22 Correct 2 ms 1408 KB Output is correct
23 Correct 14 ms 1408 KB Output is correct
24 Correct 38 ms 2036 KB Output is correct
25 Correct 7 ms 1536 KB Output is correct
26 Correct 6 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 44 ms 3572 KB Output is correct
12 Correct 50 ms 5608 KB Output is correct
13 Correct 21 ms 5616 KB Output is correct
14 Correct 13 ms 1664 KB Output is correct
15 Correct 1 ms 1408 KB Output is correct
16 Correct 46 ms 3560 KB Output is correct
17 Correct 18 ms 1408 KB Output is correct
18 Correct 7 ms 1408 KB Output is correct
19 Correct 23 ms 1728 KB Output is correct
20 Correct 24 ms 3708 KB Output is correct
21 Correct 2 ms 1408 KB Output is correct
22 Correct 2 ms 1408 KB Output is correct
23 Correct 14 ms 1408 KB Output is correct
24 Correct 38 ms 2036 KB Output is correct
25 Correct 7 ms 1536 KB Output is correct
26 Correct 6 ms 1408 KB Output is correct
27 Execution timed out 3076 ms 72092 KB Time limit exceeded
28 Halted 0 ms 0 KB -