//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
using namespace std;
const int N = 1e3 + 1;
int d[N + 1][N + 1];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
char a[N + 1][N + 1];
int up[N + 1][N + 1], dw[N + 1][N + 1], rt[N + 1][N + 1], lt[N + 1][N + 1];
int n, m;
pair<int, int> fn;
struct TPQItem
{
int dlab, x, y;
bool operator < (const TPQItem &other) const
{
return other.dlab < dlab;
}
};
priority_queue<TPQItem> PQ;
void read()
{
cin >> n >> m;
memset(up, -1, sizeof(up));
memset(dw, -1, sizeof(dw));
memset(rt, -1, sizeof(rt));
memset(lt, -1, sizeof(lt));
memset(d, 0x3f, sizeof(d));
for (int i = 1; i <= n; ++ i)
{
for (int j = 1; j <= m; ++ j)
{
cin >> a[i][j];
if (a[i][j] != '#')
{
up[i][j] = up[i - 1][j] + 1;
lt[i][j] = lt[i][j - 1] + 1;
}
if (a[i][j] == 'S')
{
PQ.push({0, i, j});
d[i][j] = 0;
}
if (a[i][j] == 'C')
{
fn = {i, j};
}
}
}
}
int occho(int t, int i, int j)
{
if (t == 0)
{
return up[i][j];
}
if (t == 1)
{
return rt[i][j];
}
if (t == 2)
{
return dw[i][j];
}
if (t == 3)
{
return lt[i][j];
}
}
void solve()
{
for (int i = n; i >= 1; -- i)
{
for (int j = m; j >= 1; -- j)
{
if (a[i][j] != '#')
{
dw[i][j] = dw[i + 1][j] + 1;
rt[i][j] = rt[i][j + 1] + 1;
}
}
}
while(!PQ.empty())
{
TPQItem p = PQ.top();
PQ.pop();
int x = p.x, y = p.y;
if (d[x][y] != p.dlab)
{
continue;
}
for (int k = 0; k < 4; ++ k)
{
int i = x + dx[k], j = y + dy[k];
if (i < 1 || i > n || j < 1 || j > m || a[i][j] == '#')
{
continue;
}
if (d[i][j] > d[x][y] + 1)
{
d[i][j] = d[x][y] + 1;
PQ.push({d[i][j], i, j});
}
}
int walk = 1e9;
for (int i = 0; i < 4; ++ i)
{
walk = min(walk, occho(i, x, y));
}
for (int k = 0; k < 4; ++ k)
{
int i = x + dx[k] * occho(k, x, y), j = y + dy[k] * occho(k, x, y);
/*if (x == 4 && y == 3)
{
cerr << i << ' ' << j << ' ' << walk + 1 << '\n';
}*/
if (i < 1 || i > n || j < 1 || j > m || a[i][j] == '#')
{
continue;
}
if (d[i][j] > d[x][y] + walk + 1)
{
d[i][j] = d[x][y] + walk + 1;
PQ.push({d[i][j], i, j});
}
}
}
cout << d[fn.first][fn.second];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message
portals.cpp: In function 'int occho(int, int, int)':
portals.cpp:71:1: warning: control reaches end of non-void function [-Wreturn-type]
71 | }
| ^
portals.cpp: In function 'int main()':
portals.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19924 KB |
Output is correct |
2 |
Correct |
8 ms |
19984 KB |
Output is correct |
3 |
Correct |
9 ms |
19920 KB |
Output is correct |
4 |
Correct |
8 ms |
19924 KB |
Output is correct |
5 |
Correct |
9 ms |
19924 KB |
Output is correct |
6 |
Correct |
9 ms |
19964 KB |
Output is correct |
7 |
Correct |
9 ms |
19924 KB |
Output is correct |
8 |
Correct |
8 ms |
19924 KB |
Output is correct |
9 |
Correct |
8 ms |
19920 KB |
Output is correct |
10 |
Correct |
8 ms |
19924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19896 KB |
Output is correct |
2 |
Correct |
8 ms |
19924 KB |
Output is correct |
3 |
Correct |
9 ms |
19972 KB |
Output is correct |
4 |
Correct |
8 ms |
19924 KB |
Output is correct |
5 |
Correct |
9 ms |
19884 KB |
Output is correct |
6 |
Correct |
8 ms |
19924 KB |
Output is correct |
7 |
Correct |
8 ms |
19888 KB |
Output is correct |
8 |
Correct |
8 ms |
19920 KB |
Output is correct |
9 |
Correct |
8 ms |
19924 KB |
Output is correct |
10 |
Correct |
10 ms |
19944 KB |
Output is correct |
11 |
Correct |
8 ms |
19920 KB |
Output is correct |
12 |
Correct |
8 ms |
19924 KB |
Output is correct |
13 |
Correct |
9 ms |
19952 KB |
Output is correct |
14 |
Correct |
9 ms |
19892 KB |
Output is correct |
15 |
Correct |
9 ms |
19924 KB |
Output is correct |
16 |
Correct |
8 ms |
19916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19924 KB |
Output is correct |
2 |
Correct |
9 ms |
19924 KB |
Output is correct |
3 |
Correct |
8 ms |
19924 KB |
Output is correct |
4 |
Correct |
8 ms |
19924 KB |
Output is correct |
5 |
Correct |
12 ms |
20120 KB |
Output is correct |
6 |
Correct |
13 ms |
20188 KB |
Output is correct |
7 |
Correct |
13 ms |
20180 KB |
Output is correct |
8 |
Correct |
11 ms |
20184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19924 KB |
Output is correct |
2 |
Correct |
10 ms |
19924 KB |
Output is correct |
3 |
Correct |
8 ms |
19924 KB |
Output is correct |
4 |
Correct |
8 ms |
19924 KB |
Output is correct |
5 |
Correct |
8 ms |
19924 KB |
Output is correct |
6 |
Correct |
10 ms |
19924 KB |
Output is correct |
7 |
Correct |
9 ms |
19916 KB |
Output is correct |
8 |
Correct |
8 ms |
19924 KB |
Output is correct |
9 |
Correct |
8 ms |
19924 KB |
Output is correct |
10 |
Correct |
9 ms |
19924 KB |
Output is correct |
11 |
Correct |
9 ms |
19948 KB |
Output is correct |
12 |
Correct |
9 ms |
19924 KB |
Output is correct |
13 |
Correct |
8 ms |
19924 KB |
Output is correct |
14 |
Correct |
13 ms |
20180 KB |
Output is correct |
15 |
Correct |
12 ms |
20180 KB |
Output is correct |
16 |
Correct |
12 ms |
20184 KB |
Output is correct |
17 |
Correct |
13 ms |
20180 KB |
Output is correct |
18 |
Correct |
13 ms |
20212 KB |
Output is correct |
19 |
Correct |
13 ms |
20208 KB |
Output is correct |
20 |
Correct |
13 ms |
20244 KB |
Output is correct |
21 |
Correct |
12 ms |
20180 KB |
Output is correct |
22 |
Correct |
12 ms |
20224 KB |
Output is correct |
23 |
Correct |
13 ms |
20180 KB |
Output is correct |
24 |
Correct |
13 ms |
20180 KB |
Output is correct |
25 |
Correct |
8 ms |
19924 KB |
Output is correct |
26 |
Correct |
9 ms |
19984 KB |
Output is correct |
27 |
Correct |
9 ms |
19920 KB |
Output is correct |
28 |
Correct |
10 ms |
20180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19924 KB |
Output is correct |
2 |
Correct |
9 ms |
19924 KB |
Output is correct |
3 |
Correct |
8 ms |
19924 KB |
Output is correct |
4 |
Correct |
8 ms |
19924 KB |
Output is correct |
5 |
Correct |
9 ms |
19924 KB |
Output is correct |
6 |
Correct |
9 ms |
19976 KB |
Output is correct |
7 |
Correct |
10 ms |
19920 KB |
Output is correct |
8 |
Correct |
8 ms |
19924 KB |
Output is correct |
9 |
Correct |
11 ms |
20040 KB |
Output is correct |
10 |
Correct |
9 ms |
19952 KB |
Output is correct |
11 |
Correct |
10 ms |
19952 KB |
Output is correct |
12 |
Correct |
9 ms |
19924 KB |
Output is correct |
13 |
Correct |
10 ms |
19924 KB |
Output is correct |
14 |
Correct |
12 ms |
20180 KB |
Output is correct |
15 |
Correct |
12 ms |
20180 KB |
Output is correct |
16 |
Correct |
12 ms |
20180 KB |
Output is correct |
17 |
Correct |
14 ms |
20180 KB |
Output is correct |
18 |
Correct |
14 ms |
20240 KB |
Output is correct |
19 |
Correct |
14 ms |
20180 KB |
Output is correct |
20 |
Correct |
14 ms |
20180 KB |
Output is correct |
21 |
Correct |
11 ms |
20188 KB |
Output is correct |
22 |
Correct |
15 ms |
20180 KB |
Output is correct |
23 |
Correct |
11 ms |
20180 KB |
Output is correct |
24 |
Correct |
125 ms |
22048 KB |
Output is correct |
25 |
Correct |
204 ms |
22200 KB |
Output is correct |
26 |
Correct |
166 ms |
22208 KB |
Output is correct |
27 |
Correct |
169 ms |
22104 KB |
Output is correct |
28 |
Correct |
92 ms |
21836 KB |
Output is correct |
29 |
Correct |
105 ms |
21916 KB |
Output is correct |
30 |
Correct |
116 ms |
21836 KB |
Output is correct |
31 |
Correct |
14 ms |
20180 KB |
Output is correct |
32 |
Correct |
170 ms |
22092 KB |
Output is correct |
33 |
Correct |
8 ms |
19916 KB |
Output is correct |
34 |
Correct |
9 ms |
19928 KB |
Output is correct |
35 |
Correct |
122 ms |
21940 KB |
Output is correct |
36 |
Correct |
8 ms |
19924 KB |
Output is correct |
37 |
Correct |
10 ms |
20188 KB |
Output is correct |
38 |
Correct |
55 ms |
21848 KB |
Output is correct |
39 |
Correct |
78 ms |
21836 KB |
Output is correct |