이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define INF (int)1e9
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
const int N = 1005;
bool a[N][N];
int d[N][N];
int x, y;
vector <pair<int, int>> ok[N][N];
bool good(int x, int y){
if (!a[x - 1][y]) return true;
if (!a[x + 1][y]) return true;
if (!a[x][y - 1]) return true;
if (!a[x][y + 1]) return true;
return false;
}
void Solve()
{
cin >> n >> m;
queue <pair<int, int>> q;
for (int i = 1; i <= n; i++){
string s; cin >> s;
for (int j = 1; j <= m; j++){
ok[i][j].push_back({i - 1, j});
ok[i][j].push_back({i, j + 1});
ok[i][j].push_back({i + 1, j});
ok[i][j].push_back({i, j - 1});
d[i][j] = INF;
if (s[j - 1] == 'S'){
a[i][j] = true;
d[i][j] = 0;
q.push({i, j});
} else if (s[j - 1] == 'C'){
a[i][j] = true;
x = i;
y = j;
} else if (s[j - 1] == '.'){
a[i][j] = true;
}
}
}
for (int i = 1; i <= n; i++){
int last = 0;
for (int j = 1; j <= m; j++){
if (good(i, j))
ok[i][j].push_back({i, last});
if (!a[i][j]) last = 0;
else if (last == 0) last = j;
}
last = 0;
for (int j = m; j >= 1; j--){
if (good(i, j))
ok[i][j].push_back({i, last});
if (!a[i][j]) last = 0;
else if (last == 0) last = j;
}
}
for (int j = 1; j <= m; j++){
int last = 0;
for (int i = 1; i <= n; i++){
if (good(i, j))
ok[i][j].push_back({last, j});
if (!a[i][j]) last = 0;
else if (last == 0) last = i;
}
last = 0;
for (int i = n; i >= 1; i--){
if (good(i, j))
ok[i][j].push_back({last, j});
if (!a[i][j]) last = 0;
else if (last == 0) last = i;
}
}
while (!q.empty()){
auto pi = q.front(); q.pop();
int ux = pi.first, uy = pi.second;
// cout << ux << " " << uy << "\n";
for (auto [vx, vy] : ok[ux][uy]){
if (!a[vx][vy]) continue;
if (d[vx][vy] == INF){
d[vx][vy] = d[ux][uy] + 1;
q.push({vx, vy});
}
}
}
cout << d[x][y] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
portals.cpp: In function 'void Solve()':
portals.cpp:54:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
54 | if (good(i, j))
| ^~
portals.cpp:56:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
56 | if (!a[i][j]) last = 0;
| ^~
portals.cpp:62:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
62 | if (good(i, j))
| ^~
portals.cpp:64:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
64 | if (!a[i][j]) last = 0;
| ^~
portals.cpp:72:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
72 | if (good(i, j))
| ^~
portals.cpp:74:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
74 | if (!a[i][j]) last = 0;
| ^~
portals.cpp:80:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
80 | if (good(i, j))
| ^~
portals.cpp:82:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
82 | if (!a[i][j]) last = 0;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |