// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int INF = 1'000'000'007;
int rows, cols, dist[2001][2001];
int dr[] = {1, -1, 0, 0, 0};
int dc[] = {0, 0, 1, -1, 0};
string grid[2001];
bool valid(int row, int col) { return 0 <= row && row < rows && 0 <= col && col < cols; }
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
memset(dist, -1, sizeof(dist));
cin >> rows >> cols;
int sr, sc, er, ec;
rep(i, 0, rows) {
cin >> grid[i];
int j = 0;
trav(cell, grid[i]) {
if (cell == 'o') {
sr = i;
sc = j;
} else if (cell == 'x') {
er = i;
ec = j;
}
cell = cell == 'v' ? 0 : cell == '^' ? 1
: cell == '>' ? 2
: cell == '<' ? 3
: 4;
++j;
}
}
deque<ii> q;
dist[sr][sc] = 0;
rep(i, 0, 4) if (valid(sr + dr[i], sc + dc[i])) {
q.emplace_front(sr + dr[i], sc + dc[i]);
dist[sr + dr[i]][sc + dc[i]] = 0;
}
while (!q.empty()) {
int row, col;
tie(row, col) = q.front();
// cout << "row = " << row << " col = " << col << " dist = " << dist[row][col] << endl;
q.pop_front();
if(!dist[row][col]==-2)continue;
dist[row][col]=-2;
if (row == er && col == ec) {
cout << dist[row][col] << endl;
return 0;
}
int cell = grid[row][col];
int nr = row + dr[cell], nc = col + dc[cell];
if (valid(nr, nc)) {
if (dist[nr][nc] == -1||dist[row][col]<dist[nr][nc]) {
dist[nr][nc] = dist[row][col];
q.emplace_front(nr, nc);
}
}
rep(i, 0, 4) {
nr = row + dr[i], nc = col + dc[i];
if (!valid(nr, nc) || dist[nr][nc] != -1) continue;
dist[nr][nc] = dist[row][col] + 1;
q.emplace_back(nr, nc);
}
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:81:27: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses]
81 | if(!dist[row][col]==-2)continue;
| ^~
Main.cpp:81:12: note: add parentheses around left hand side expression to silence this warning
81 | if(!dist[row][col]==-2)continue;
| ^~~~~~~~~~~~~~~
| ( )
Main.cpp:81:27: warning: comparison of constant '-2' with boolean expression is always false [-Wbool-compare]
81 | if(!dist[row][col]==-2)continue;
| ~~~~~~~~~~~~~~~^~~~
Main.cpp:84:23: warning: 'ec' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | if (row == er && col == ec) {
| ~~~~~~~~~~^~~~~~~~~~~~
Main.cpp:84:9: warning: 'er' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | if (row == er && col == ec) {
| ^~
Main.cpp:70:27: warning: 'sc' may be used uninitialized in this function [-Wmaybe-uninitialized]
70 | rep(i, 0, 4) if (valid(sr + dr[i], sc + dc[i])) {
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:70:27: warning: 'sr' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
15948 KB |
Integer -2 violates the range [0, 63] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
15948 KB |
Integer -2 violates the range [0, 63] |
2 |
Halted |
0 ms |
0 KB |
- |