/**
* author : pham van sam
* created : 15 June 2023 (Thursday)
**/
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)
template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
const int MAXN = 505;
const int dir[] = {-1, 0, 1, 0, 0, 1, 0, -1};
int N, W, H, dp[2][MAXN][MAXN][5];
vector <pair <int, int>> points;
char a[MAXN][MAXN];
inline bool inside(int x, int y) {
return (x > 0 && y > 0 && x <= W && y <= H);
}
namespace sub1 {
void bfs(int x, int y, int id) {
queue <array <int, 3>> q;
memset(dp[id], 0x3f, sizeof dp[id]);
dp[id][x][y][4] = 0;
q.push({x, y, 4});
while(!q.empty()) {
auto [u, v, i] = q.front(); q.pop();
REP(t, 4) {
int x = u + dir[t];
int y = v + dir[t + 4];
if(!inside(x, y)) continue;
if(a[x][y] == 'x') continue;
if(minimize(dp[id][x][y][t], dp[id][u][v][i] + (t != i))) {
q.push({x, y, t});
}
}
}
}
bool check(int i, int j, int t) {
if(t > 3) return true;
int x = i + dir[t];
int y = j + dir[t + 4];
if(!inside(x, y)) return true;
return a[x][y] == 'x';
}
void Main() {
bfs(points[0].first, points[0].second, 0);
bfs(points[1].first, points[1].second, 1);
int ans = 1e9;
FOR(i, 1, W) FOR(j, 1, H) {
REP(t1, 5) {
REP(t2, 5) {
if(check(i, j, t1) || check(i, j, t2)) {
minimize(ans, dp[0][i][j][t1] + dp[1][i][j][t2]);
}
}
}
}
if(ans >= 1e9) ans = -1;
cout << ans; exit(0);
}
}
void process(void) {
cin >> N >> H >> W;
FOR(i, 1, W) FOR(j, 1, H) {
cin >> a[i][j];
if(a[i][j] >= '1' && a[i][j] <= '9') points.emplace_back(i, j);
}
if(N == 2) sub1::Main();
}
signed main() {
#define TASK "TASK"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
auto start_time = steady_clock::now();
int test = 1;
// cin >> test;
for (int i = 1; i <= test; ++i) {
process();
// cout << '\n';
}
auto end_time = steady_clock::now();
cerr << "\nExecution time : " << duration_cast<milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10196 KB |
Output is correct |
2 |
Correct |
5 ms |
10196 KB |
Output is correct |
3 |
Incorrect |
4 ms |
10196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10196 KB |
Output is correct |
2 |
Correct |
5 ms |
10196 KB |
Output is correct |
3 |
Incorrect |
4 ms |
10196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10196 KB |
Output is correct |
2 |
Correct |
5 ms |
10196 KB |
Output is correct |
3 |
Incorrect |
4 ms |
10196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10196 KB |
Output is correct |
2 |
Correct |
5 ms |
10196 KB |
Output is correct |
3 |
Incorrect |
4 ms |
10196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |