# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
673534 |
2022-12-20T21:57:23 Z |
taher |
Portals (BOI14_portals) |
C++17 |
|
245 ms |
14440 KB |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n , m;
cin >> n >> m;
vector<vector<char>> g(n , vector<char> (m));
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < m ; j++) {
cin >> g[i][j];
}
}
vector<vector<int>> prefX(n , vector<int> (m));
vector<vector<int>> prefY(m , vector<int> (n));
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < m ; j++) {
if (j > 0) prefX[i][j] = prefX[i][j - 1];
if (g[i][j] == '#') prefX[i][j] += 1;
}
}
for (int j = 0 ; j < m ; j++) {
for (int i = 0 ; i < n ; i++) {
if (i > 0) prefY[j][i] = prefY[j][i - 1];
if (g[i][j] == '#') prefY[j][i] += 1;
}
}
int locX = 0 , locY = 0;
int desX = 0 , desY = 0;
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < m ; j++) {
if (g[i][j] == 'S') {
locX = i;
locY = j;
}
if (g[i][j] == 'C') {
desX = i;
desY = j;
}
}
}
auto Ok = [&](int i , int j) {
if (i >= 0 && i < n && j >= 0 && j < m) {
return true;
}
return false;
};
auto Up = [&](int i , int j) {
int lo = 0 , hi = i - 1;
int at = prefY[j][i];
if (at == 0) return -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (prefY[j][mid] < at) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return lo;
};
auto Down = [&](int i , int j) {
int lo = i + 1 , hi = n - 1;
int at = prefY[j][n - 1] - prefY[j][i];
if (at == 0) return n;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (prefY[j][n - 1] - prefY[j][mid] == at) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return lo;
};
auto Left = [&](int i , int j) {
int lo = 0 , hi = j - 1;
int at = prefX[i][j];
if (at == 0) return -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (prefX[i][mid] < at) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return lo;
};
auto Right = [&](int i , int j) {
int lo = j + 1 , hi = m - 1;
int at = prefX[i][m - 1] - prefX[i][j];
if (at == 0) return m;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (prefX[i][m - 1] - prefX[i][mid] == at) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return lo;
};
const int dx[4] = {1 , 0 , 0 , -1};
const int dy[4] = {0 , 1 , -1 , 0};
#define pi pair<int , int>
priority_queue<pair<int , pi> , vector<pair<int , pi>> , greater<pair<int , pi>>> que;
que.emplace(0 , make_pair(locX , locY));
vector<vector<int>> dist(n , vector<int> (m , inf));
dist[locX][locY] = 0;
auto Update = [&](int i , int j , int ni , int nj , int add) {
if (dist[ni][nj] > dist[i][j] + add) {
dist[ni][nj] = dist[i][j] + add;
que.emplace(dist[ni][nj] , make_pair(ni , nj));
}
return ;
};
while (!que.empty()) {
auto pos = que.top();
que.pop();
int x = pos.second.first;
int y = pos.second.second;
if (dist[x][y] != pos.first) {
continue;
}
for (int it = 0 ; it < 4 ; it++) {
int nx = x + dx[it];
int ny = y + dy[it];
if (Ok(nx , ny)) {
if (g[nx][ny] != '#' && dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
que.emplace(dist[nx][ny] , make_pair(nx , ny));
}
}
}
int x0 = Up(x , y);
int x1 = Down(x , y);
int y0 = Left(x , y);
int y1 = Right(x , y);
int d0 = x - x0;
int d1 = x1 - x;
int d2 = y - y0;
int d3 = y1 - y;
int d = min({d0 , d1 , d2 , d3});
if (Ok(x0 + 1 , y)) {
if (d < d0) {
assert(g[x0 + 1][y] != '#');
Update(x , y , x0 + 1 , y , d);
}
}
if (Ok(x1 - 1 , y)) {
if (d < d1) {
assert(g[x1 - 1][y] != '#');
Update(x , y , x1 - 1 , y , d);
}
}
if (Ok(x , y0 + 1)) {
if (d < d2) {
assert(g[x][y0 + 1] != '#');
Update(x , y , x , y0 + 1 , d);
}
}
if (Ok(x , y1 - 1)) {
if (d < d3) {
assert(g[x][y1 - 1] != '#');
Update(x , y , x , y1 - 1 , d);
}
}
}
cout << dist[desX][desY] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
904 KB |
Output is correct |
6 |
Correct |
9 ms |
852 KB |
Output is correct |
7 |
Correct |
8 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
900 KB |
Output is correct |
15 |
Correct |
7 ms |
852 KB |
Output is correct |
16 |
Correct |
8 ms |
852 KB |
Output is correct |
17 |
Correct |
8 ms |
852 KB |
Output is correct |
18 |
Correct |
11 ms |
852 KB |
Output is correct |
19 |
Correct |
9 ms |
916 KB |
Output is correct |
20 |
Correct |
8 ms |
852 KB |
Output is correct |
21 |
Correct |
7 ms |
852 KB |
Output is correct |
22 |
Correct |
8 ms |
908 KB |
Output is correct |
23 |
Correct |
7 ms |
852 KB |
Output is correct |
24 |
Correct |
7 ms |
840 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
6 ms |
852 KB |
Output is correct |
15 |
Correct |
7 ms |
852 KB |
Output is correct |
16 |
Correct |
8 ms |
852 KB |
Output is correct |
17 |
Correct |
8 ms |
852 KB |
Output is correct |
18 |
Correct |
10 ms |
852 KB |
Output is correct |
19 |
Correct |
8 ms |
852 KB |
Output is correct |
20 |
Correct |
8 ms |
944 KB |
Output is correct |
21 |
Correct |
6 ms |
840 KB |
Output is correct |
22 |
Correct |
6 ms |
852 KB |
Output is correct |
23 |
Correct |
8 ms |
868 KB |
Output is correct |
24 |
Correct |
229 ms |
14300 KB |
Output is correct |
25 |
Correct |
245 ms |
14428 KB |
Output is correct |
26 |
Correct |
193 ms |
14440 KB |
Output is correct |
27 |
Correct |
193 ms |
14332 KB |
Output is correct |
28 |
Correct |
143 ms |
14284 KB |
Output is correct |
29 |
Correct |
169 ms |
14180 KB |
Output is correct |
30 |
Correct |
196 ms |
14176 KB |
Output is correct |
31 |
Correct |
7 ms |
852 KB |
Output is correct |
32 |
Correct |
184 ms |
14108 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
184 ms |
14160 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
3 ms |
852 KB |
Output is correct |
38 |
Correct |
89 ms |
14156 KB |
Output is correct |
39 |
Correct |
123 ms |
14112 KB |
Output is correct |