/******************************
* author : @marvinthang *
* date : 10 / 02 / 2022 *
******************************/
#include <bits/stdc++.h>
using namespace std;
#define superspeed ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
#define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
template <class U, class V> ostream & operator << (ostream& out, const pair<U, V> &p) { return out << '(' << p.first << ", " << p.second << ')'; }
template <class T> ostream & operator << (ostream &out, const vector<T> &vt) { out << '{'; for (size_t i = 0; i + 1 < vt.size(); i++) out << vt[i] << ", "; if (!vt.empty()) out << vt.back(); return out << '}'; }
const int MOD = 1e9 + 7;
const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const int dir[] = {0, 1, 0, -1, 0}; // {0, 1, 1, -1, -1, 1, 0, -1, 0};
const string DIR = ">v<^";
const long long oo = 1e18;
const int MAX = 2003;
int M, N, D[MAX][MAX];
pair <int, int> trace[MAX][MAX];
char c[MAX][MAX];
bool inside(int x, int y) {
return 1 <= x && x <= M && 1 <= y && y <= N;
}
void bfs(int a, int b) {
memset(D, 0x3f, sizeof(D));
deque <pair <int, int>> dq;
D[a][b] = 0;
dq.push_back(make_pair(a, b));
while (!dq.empty()) {
auto [x, y] = dq.front(); dq.pop_front();
for (int k = 0; k < 4; ++k) {
int u = x + dir[k];
int v = y + dir[k + 1];
int cost = 1;
if (c[x][y] == 'o' || c[x][y] == DIR[k]) cost = 0;
if (inside(u, v) && D[u][v] > D[x][y] + cost) {
D[u][v] = D[x][y] + cost;
trace[u][v] = make_pair(x, y);
if (c[u][v] == 'x') {
a = u; b = v;
break;
}
if (!cost) dq.push_front(make_pair(u, v));
else dq.push_back(make_pair(u, v));
}
}
}
cout << D[a][b] << '\n';
while (c[a][b] != 'o') {
auto [x, y] = trace[a][b];
if (D[x][y] + 1 == D[a][b]) {
for (int k = 0; k < 4; ++k) {
if (x + dir[k] == a && y + dir[k + 1] == b) {
c[x][y] = DIR[k];
break;
}
}
}
a = x; b = y;
}
c[a][b] = 'o';
}
int main(void) {
superspeed;
file("coci2021_r4_patkice2");
cin >> M >> N;
for (int i = 1; i <= M; ++i)
for (int j = 1; j <= N; ++j)
cin >> c[i][j];
for (int i = 1; i <= M; ++i)
for (int j = 1; j <= N; ++j)
if (c[i][j] == 'o')
bfs(i, j);
for (int i = 1; i <= M; ++i) {
for (int j = 1; j <= N; ++j) {
cout << c[i][j];
}
cout << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:11:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:73:5: note: in expansion of macro 'file'
73 | file("coci2021_r4_patkice2");
| ^~~~
Main.cpp:11:95: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:73:5: note: in expansion of macro 'file'
73 | file("coci2021_r4_patkice2");
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
16084 KB |
Output is correct |
3 |
Correct |
7 ms |
16084 KB |
Output is correct |
4 |
Correct |
8 ms |
16084 KB |
Output is correct |
5 |
Correct |
9 ms |
15956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
16084 KB |
Output is correct |
3 |
Correct |
7 ms |
16084 KB |
Output is correct |
4 |
Correct |
8 ms |
16084 KB |
Output is correct |
5 |
Correct |
9 ms |
15956 KB |
Output is correct |
6 |
Correct |
53 ms |
24456 KB |
Output is correct |
7 |
Correct |
49 ms |
27332 KB |
Output is correct |
8 |
Correct |
115 ms |
35008 KB |
Output is correct |
9 |
Correct |
171 ms |
48084 KB |
Output is correct |
10 |
Correct |
269 ms |
53072 KB |
Output is correct |
11 |
Correct |
337 ms |
63112 KB |
Output is correct |