#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e3 + 5;
const int di[] = {0, 0, 1, -1};
const int dj[] = {-1, 1, 0, 0};
const string C = "<>v^";
vector grid(N, string());
vector par(N, vector<int>(N, -1));
int n, m, si, sj, ei, ej;
bool valid(int i, int j)
{
return i >= 0 && j >= 0 && i < n && j < m && par[i][j] == -1;
}
int bfs()
{
priority_queue<array<int, 4>, deque<array<int, 4>>, greater<array<int, 4>>> q;
par[si][sj] = -5;
for (int k = 0; k < 4; k++)
{
int ni = si + di[k], nj = sj + dj[k];
if (valid(si + di[k], sj + dj[k]))
{
q.push({0, ni, nj, k});
}
}
while (!q.empty())
{
auto [c, i, j, p] = q.top();
q.pop();
if (par[i][j] != -1)
continue;
par[i][j] = p;
if (i == ei && j == ej)
return c;
for (int k = 0; k < 4; k++)
{
int ni = i + di[k], nj = j + dj[k];
if (!valid(ni, nj))
continue;
q.push({c + (grid[i][j] != C[k]), ni, nj, k});
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> grid[i];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == 'o')
si = i, sj = j;
else if (grid[i][j] == 'x')
ei = i, ej = j;
}
}
cout << bfs() << endl;
while (par[ei][ej] != -5)
{
int k = par[ei][ej];
int i = ei - di[k], j = ej - dj[k];
grid[i][j] = C[k];
ei = i;
ej = j;
}
grid[si][sj] = 'o';
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << grid[i][j];
}
cout << '\n';
}
cout.flush();
system("pause");
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:85:11: warning: ignoring return value of 'int system(const char*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | system("pause");
| ~~~~~~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |