#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define Rep(i, n) for(int i = 1; i <= n; ++i)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define Forv(v, h) for(auto &v : h)
#define Bit(x, i) ((x) >> (i) & 1ll)
#define onbit(x, i) ((x) (1ll << i))
#define offbit(x, i) ((x) &~ (1ll << i))
#define cnt_bit(x) __builtin_popcountll(x)
#define Log2(x) (63 - __builtin_clzll(x))
#define all(h) h.begin(), h.end()
#define reset(h, v) (memset(h, v, sizeof(h)))
#define memoryof(v) (sizeof(v) / 1024 / 1024)
using ii = pair<int, int>;
using ull = unsigned long long;
using db = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vii = vector<ii>;
const int dx[] = {0, +1, -1, 0};
const int dy[] = {-1, 0, 0, +1};
const int MAXN = 2e3 + 10;
const int MOD = 1e9 + 7;
const int oo = 1e18 + 1;
const int base = 311;
template <class X, class Y> bool maximize(X &a, const Y &b) {
if(a < b) return a = b, true;
return false;
}
template <class X, class Y> bool minimize(X &a, const Y &b) {
if(a > b) return a = b, true;
return false;
}
// (x, y) -> (u, v) = Ckn(u - x, x + y - u - v);
// Ckn(k, a) * Ckn(k, b) = C(a, a + b); (k : 1 -> min(a, b))
void fixmod(int &val) {
if (val < 0) val += MOD*MOD;
val %= MOD;
}
int n, m;
char a[MAXN][MAXN];
map<char, int> id;
int d[MAXN][MAXN];
ii s, t;
int trace[MAXN][MAXN];
void bfs()
{
deque <ii> dq;
For (i, 1, n) For (j, 1, m) d[i][j] = oo;
d[s.fi][s.se] = 0;
dq.push_front(s);
while (!dq.empty()) {
int u = dq.front().fi;
int v = dq.front().se;
dq.pop_front();
For (k, 0, 3) {
int x = u + dx[k];
int y = v + dy[k];
if (1 <= x && x <= n && 1 <= y && y <= m) {
int w = 1;
if (id[a[u][v]] == k || a[u][v] == 'o') w = 0;
if (d[x][y] > d[u][v] + w)
{
d[x][y] = d[u][v] + w;
if (w) dq.push_back({x, y});
else dq.push_front({x, y});
trace[x][y] = k;
}
}
}
}
}
void Progess() {
cin >> n >> m;
For (i, 1, n) For (j, 1, m) {
cin >> a[i][j];
if (a[i][j] == 'o') s = {i, j};
if (a[i][j] == 'x') t = {i, j};
}
id['<'] = 0, id['v'] = 1, id['^'] = 2, id['>'] = 3, id['.'] = 5;
bfs();
cout << d[t.fi][t.se] << '\n';
while (t != s) {
// cerr << t.fi << ' ' << t.se << ' ';
ii pre = {t.fi - dx[trace[t.fi][t.se]], t.se - dy[trace[t.fi][t.se]]};
if (d[t.fi][t.se] != d[pre.fi][pre.se])
{
int k = trace[t.fi][t.se];
// cerr << k << '\n';
if (k == 0) a[pre.fi][pre.se] = '<';
if (k == 1) a[pre.fi][pre.se] = 'v';
if (k == 2) a[pre.fi][pre.se] = '^';
if (k == 3) a[pre.fi][pre.se] = '>';
}
t = pre;
}
For (i, 1, n) {
For (j, 1, m) cout << a[i][j];
cout << '\n';
}
}
signed main(void) {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
cout.tie(nullptr);
#define task "a"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}//_______________________________
Progess();
cerr << "\nTime elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s.\n";
return (0 ^ 0);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:126:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:127:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4568 KB |
Output is correct |
3 |
Correct |
2 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
6480 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4568 KB |
Output is correct |
3 |
Correct |
2 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
6480 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
43 ms |
26168 KB |
Output is correct |
7 |
Correct |
50 ms |
39988 KB |
Output is correct |
8 |
Correct |
126 ms |
44336 KB |
Output is correct |
9 |
Correct |
188 ms |
62544 KB |
Output is correct |
10 |
Correct |
290 ms |
67912 KB |
Output is correct |
11 |
Correct |
330 ms |
83908 KB |
Output is correct |