// #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 (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[nr][nc] = dist[row][col];
q.emplace_front(nr, nc);
} else
dist[nr][nc] = min(dist[nr][nc], dist[row][col]);
}
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:23: warning: 'ec' may be used uninitialized in this function [-Wmaybe-uninitialized]
81 | if (row == er && col == ec) {
| ~~~~~~~~~~^~~~~~~~~~~~
Main.cpp:81:9: warning: 'er' may be used uninitialized in this function [-Wmaybe-uninitialized]
81 | 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]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
7 ms |
15948 KB |
Partially correct |
2 |
Incorrect |
6 ms |
16048 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
7 ms |
15948 KB |
Partially correct |
2 |
Incorrect |
6 ms |
16048 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |