#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 18;
const ll MAXN = 1e5 + 100;
vector<pair<int,pair<int,int>>> graph[2005][2005], rev[2005][2005];
char grid[2005][2005];
int dist[2005][2005];
pair<int,int> s, e;
void change(int row, int col, int nrow, int ncol) {
if (nrow > row)
grid[row][col] = 'v';
else if (nrow < row)
grid[row][col] = '^';
else if (ncol < col)
grid[row][col] = '<';
else
grid[row][col] = '>';
}
void dfs(int row, int col, int old_row, int old_col, int target) {
for (auto u : rev[row][col]) {
int w = u.f;
int nrow = u.s.f;
int ncol = u.s.s;
if (nrow == old_row && ncol == old_col)
continue;
if (dist[nrow][ncol] + w == target) {
change(nrow, ncol, row, col);
dfs(nrow, ncol, row, col, target - w);
return ;
}
}
}
int r, c;
bool check(int row, int col) {
if (row >= 0 && col >= 0 && r > row && c > col)
return true;
return false;
}
int main() {
fast
cin >> r >> c;
for (int i = 0; i < 2005; i++) {
for (int j = 0; j < 2005; j++)
dist[i][j] = 1e8;
}
for (int i = 0; i < r; i++) {
string s;
cin >> s;
for (int j = 0; j < c; j++)
grid[i][j] = s[j];
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (grid[i][j] == '>') {
if (check(i, j+1))
graph[i][j].push_back({0, {i, j+1}});
if (check(i+1, j))
graph[i][j].push_back({1, {i+1, j}});
if (check(i, j-1))
graph[i][j].push_back({1, {i, j-1}});
if (check(i-1, j))
graph[i][j].push_back({1, {i-1, j}});
} else if (grid[i][j] == '<') {
if (check(i, j+1))
graph[i][j].push_back({1, {i, j+1}});
if (check(i+1, j))
graph[i][j].push_back({1, {i+1, j}});
if (check(i, j-1))
graph[i][j].push_back({0, {i, j-1}});
if (check(i-1, j))
graph[i][j].push_back({1, {i-1, j}});
} else if (grid[i][j] == 'v') {
if (check(i, j+1))
graph[i][j].push_back({1, {i, j+1}});
if (check(i+1, j))
graph[i][j].push_back({0, {i+1, j}});
if (check(i, j-1))
graph[i][j].push_back({1, {i, j-1}});
if (check(i-1, j))
graph[i][j].push_back({1, {i-1, j}});
} else if (grid[i][j] == '^') {
if (check(i, j+1))
graph[i][j].push_back({1, {i, j+1}});
if (check(i+1, j))
graph[i][j].push_back({1, {i+1, j}});
if (check(i, j-1))
graph[i][j].push_back({1, {i, j-1}});
if (check(i-1, j))
graph[i][j].push_back({0, {i-1, j}});
} else if (grid[i][j] == '.') {
if (check(i, j+1))
graph[i][j].push_back({1, {i, j+1}});
if (check(i+1, j))
graph[i][j].push_back({1, {i+1, j}});
if (check(i, j-1))
graph[i][j].push_back({1, {i, j-1}});
if (check(i-1, j))
graph[i][j].push_back({1, {i-1, j}});
} else if (grid[i][j] == 'o') {
s = {i, j};
if (check(i, j+1))
graph[i][j].push_back({0, {i, j+1}});
if (check(i+1, j))
graph[i][j].push_back({0, {i+1, j}});
if (check(i, j-1))
graph[i][j].push_back({0, {i, j-1}});
if (check(i-1, j))
graph[i][j].push_back({0, {i-1, j}});
} else
e = {i, j};
}
}
deque<pair<int,int>> dq;
dq.push_back(s);
dist[s.f][s.s] = 0;
while (!dq.empty()) {
int row = dq.front().f;
int col = dq.front().s;
dq.pop_front();
for (auto u : graph[row][col]) {
int w = u.f;
int nrow = u.s.f;
int ncol = u.s.s;
if (dist[nrow][ncol] > w + dist[row][col]) {
dist[nrow][ncol] = w + dist[row][col];
if (w == 1)
dq.push_back({nrow, ncol});
else
dq.push_front({nrow, ncol});
}
}
}
cout << dist[e.f][e.s] << "\n";
for (int i = 0; i < 2005; i++) {
for (int j = 0; j < 2005; j++) {
while (graph[i][j].size()) {
pair<int,pair<int,int>> here = graph[i][j].back();
graph[i][j].pop_back();
//rev[here.s.f][here.s.s].push_back({here.f, {here.s.f, here.s.s}});
}
}
}
/*
dfs(e.f, e.s, e.f, e.s, dist[e.f][e.s]);
grid[s.f][s.s] = 'o';
for (int i = 0; i < r; i++)
cout << grid[i] << "\n";
*/
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:154:29: warning: variable 'here' set but not used [-Wunused-but-set-variable]
154 | pair<int,pair<int,int>> here = graph[i][j].back();
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
61 ms |
206672 KB |
Partially correct |
2 |
Partially correct |
51 ms |
206864 KB |
Partially correct |
3 |
Partially correct |
62 ms |
206680 KB |
Partially correct |
4 |
Partially correct |
53 ms |
206676 KB |
Partially correct |
5 |
Partially correct |
51 ms |
206844 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
61 ms |
206672 KB |
Partially correct |
2 |
Partially correct |
51 ms |
206864 KB |
Partially correct |
3 |
Partially correct |
62 ms |
206680 KB |
Partially correct |
4 |
Partially correct |
53 ms |
206676 KB |
Partially correct |
5 |
Partially correct |
51 ms |
206844 KB |
Partially correct |
6 |
Partially correct |
149 ms |
240212 KB |
Partially correct |
7 |
Partially correct |
150 ms |
242428 KB |
Partially correct |
8 |
Partially correct |
264 ms |
289900 KB |
Partially correct |
9 |
Partially correct |
474 ms |
344092 KB |
Partially correct |
10 |
Partially correct |
615 ms |
406852 KB |
Partially correct |
11 |
Partially correct |
797 ms |
437156 KB |
Partially correct |