//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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19920 KB |
Output is correct |
2 |
Correct |
8 ms |
19956 KB |
Output is correct |
3 |
Correct |
10 ms |
19924 KB |
Output is correct |
4 |
Correct |
10 ms |
19924 KB |
Output is correct |
5 |
Correct |
10 ms |
19956 KB |
Output is correct |
6 |
Correct |
9 ms |
19924 KB |
Output is correct |
7 |
Correct |
12 ms |
19924 KB |
Output is correct |
8 |
Correct |
8 ms |
19924 KB |
Output is correct |
9 |
Correct |
10 ms |
19924 KB |
Output is correct |
10 |
Correct |
9 ms |
19864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19924 KB |
Output is correct |
2 |
Correct |
8 ms |
19896 KB |
Output is correct |
3 |
Correct |
12 ms |
19880 KB |
Output is correct |
4 |
Correct |
8 ms |
19924 KB |
Output is correct |
5 |
Correct |
8 ms |
19924 KB |
Output is correct |
6 |
Correct |
8 ms |
19924 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 |
19932 KB |
Output is correct |
10 |
Correct |
8 ms |
19924 KB |
Output is correct |
11 |
Correct |
9 ms |
19924 KB |
Output is correct |
12 |
Correct |
10 ms |
19928 KB |
Output is correct |
13 |
Correct |
12 ms |
19928 KB |
Output is correct |
14 |
Correct |
8 ms |
19924 KB |
Output is correct |
15 |
Correct |
8 ms |
19924 KB |
Output is correct |
16 |
Correct |
8 ms |
19924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19924 KB |
Output is correct |
2 |
Correct |
10 ms |
19924 KB |
Output is correct |
3 |
Correct |
12 ms |
19924 KB |
Output is correct |
4 |
Correct |
8 ms |
19980 KB |
Output is correct |
5 |
Correct |
12 ms |
20068 KB |
Output is correct |
6 |
Correct |
13 ms |
20180 KB |
Output is correct |
7 |
Correct |
14 ms |
20188 KB |
Output is correct |
8 |
Correct |
14 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19924 KB |
Output is correct |
2 |
Correct |
8 ms |
19924 KB |
Output is correct |
3 |
Correct |
8 ms |
19924 KB |
Output is correct |
4 |
Correct |
11 ms |
19924 KB |
Output is correct |
5 |
Correct |
9 ms |
19924 KB |
Output is correct |
6 |
Correct |
9 ms |
19880 KB |
Output is correct |
7 |
Correct |
9 ms |
19892 KB |
Output is correct |
8 |
Correct |
19 ms |
19880 KB |
Output is correct |
9 |
Correct |
9 ms |
19924 KB |
Output is correct |
10 |
Correct |
11 ms |
19924 KB |
Output is correct |
11 |
Correct |
9 ms |
19968 KB |
Output is correct |
12 |
Correct |
11 ms |
19924 KB |
Output is correct |
13 |
Correct |
10 ms |
19924 KB |
Output is correct |
14 |
Correct |
13 ms |
20168 KB |
Output is correct |
15 |
Correct |
15 ms |
20180 KB |
Output is correct |
16 |
Correct |
15 ms |
20180 KB |
Output is correct |
17 |
Correct |
12 ms |
20088 KB |
Output is correct |
18 |
Correct |
13 ms |
20124 KB |
Output is correct |
19 |
Correct |
19 ms |
20212 KB |
Output is correct |
20 |
Correct |
15 ms |
20188 KB |
Output is correct |
21 |
Correct |
12 ms |
20180 KB |
Output is correct |
22 |
Correct |
12 ms |
20060 KB |
Output is correct |
23 |
Correct |
17 ms |
20100 KB |
Output is correct |
24 |
Correct |
14 ms |
20192 KB |
Output is correct |
25 |
Correct |
10 ms |
19936 KB |
Output is correct |
26 |
Correct |
8 ms |
19936 KB |
Output is correct |
27 |
Correct |
11 ms |
19936 KB |
Output is correct |
28 |
Correct |
14 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19924 KB |
Output is correct |
2 |
Correct |
9 ms |
19924 KB |
Output is correct |
3 |
Correct |
9 ms |
19940 KB |
Output is correct |
4 |
Correct |
9 ms |
19924 KB |
Output is correct |
5 |
Correct |
11 ms |
19916 KB |
Output is correct |
6 |
Correct |
9 ms |
19924 KB |
Output is correct |
7 |
Correct |
13 ms |
19924 KB |
Output is correct |
8 |
Correct |
12 ms |
19924 KB |
Output is correct |
9 |
Correct |
14 ms |
19924 KB |
Output is correct |
10 |
Correct |
9 ms |
20000 KB |
Output is correct |
11 |
Correct |
10 ms |
19948 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 |
20140 KB |
Output is correct |
15 |
Correct |
13 ms |
20064 KB |
Output is correct |
16 |
Correct |
12 ms |
20128 KB |
Output is correct |
17 |
Correct |
13 ms |
20180 KB |
Output is correct |
18 |
Correct |
15 ms |
20220 KB |
Output is correct |
19 |
Correct |
15 ms |
20180 KB |
Output is correct |
20 |
Correct |
16 ms |
20180 KB |
Output is correct |
21 |
Correct |
12 ms |
20052 KB |
Output is correct |
22 |
Correct |
20 ms |
20052 KB |
Output is correct |
23 |
Correct |
15 ms |
20184 KB |
Output is correct |
24 |
Correct |
129 ms |
21068 KB |
Output is correct |
25 |
Correct |
208 ms |
21240 KB |
Output is correct |
26 |
Correct |
201 ms |
21092 KB |
Output is correct |
27 |
Correct |
205 ms |
21044 KB |
Output is correct |
28 |
Correct |
100 ms |
20932 KB |
Output is correct |
29 |
Correct |
118 ms |
20940 KB |
Output is correct |
30 |
Correct |
128 ms |
20948 KB |
Output is correct |
31 |
Correct |
14 ms |
20180 KB |
Output is correct |
32 |
Correct |
187 ms |
21012 KB |
Output is correct |
33 |
Correct |
9 ms |
19924 KB |
Output is correct |
34 |
Correct |
9 ms |
20028 KB |
Output is correct |
35 |
Correct |
149 ms |
21072 KB |
Output is correct |
36 |
Correct |
9 ms |
19924 KB |
Output is correct |
37 |
Correct |
12 ms |
20052 KB |
Output is correct |
38 |
Correct |
58 ms |
20936 KB |
Output is correct |
39 |
Correct |
94 ms |
20948 KB |
Output is correct |