#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using tuplis = array<int, 3>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(n) for(int i=0;i<n;++i)
#define rep2(i,n) for(int i=0;i<n;++i)
#define rep3(i,a,b) for(int i=a;i<b;++i)
#define rep4(i,a,b,c) for(int i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i : a)
#define all(a) begin(a), end(a)
template<class T, class U> bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int INF = 0x33333333;
int n, w, h;
pii mem[500][500][4];
char s[501][501];
vector<pii> g[500][500];
int cost[10][10][500][500];
pii dfs(int x, int y, int d, int cnt = 0){
if(x < 0) return {x + 1, y};
if(y < 0) return {x, y + 1};
if(x >= h) return {x - 1, y};
if(y >= w) return {x, y - 1};
if(s[x][y] == 'x') return {x + dx[d ^ 2], y + dy[d ^ 2]};
if(mem[x][y][d] != pii{-1, -1}) return mem[x][y][d];
if(cnt > 1000000) return mem[x][y][d] = {-2, -2};
int d2 = d;
if(s[x][y] == 'A'){
d2 += 3;
d2 &= 3;
}
else if(s[x][y] == 'C'){
d2 += 1;
d2 &= 3;
}
return mem[x][y][d] = dfs(x + dx[d2], y + dy[d2], d2, cnt + 1);
}
void dijkstra(int from, int to){
auto c = cost[from][to];
rep(cen, from + 1, to) rep(h) rep(j, w) chmin(c[i][j], cost[from][cen][i][j] + cost[cen][to][i][j]);
queue<tuplis> q;
vector<tuplis> q2;
rep(h) rep(j, w) if(c[i][j] != INF) q2.push_back({c[i][j], i, j});
sort(all(q2), greater<tuplis>());
while(q.size() || q2.size()){
bool flag = 0;
if(!q2.size()) flag = 1;
else if(q.size() && q.front()[0] < q2.back()[0]) flag = 1;
auto [cost, x, y] = flag ? q.front() : q2.back();
if(flag) q.pop();
else q2.pop_back();
for(auto [x2, y2] : g[x][y]) if(chmin(c[x2][y2], cost + 1)){
q.push({c[x2][y2], x2, y2});
}
}
}
int main(){
memset(mem, 0xff, sizeof(mem));
memset(cost, 0x33, sizeof(cost));
scanf("%d%d%d", &n, &w, &h);
rep(h) scanf("%s", s[i]);
rep(h) rep(j, w) rep(d, 4) dfs(i, j, d);
rep(h) rep(j, w) rep(d, 4) if(mem[i][j][d] != pii{-2, -2} && mem[i][j][d] != pii{i, j}) g[i][j].push_back(mem[i][j][d]);
rep(h) rep(j, w) if(isdigit(s[i][j]))cost[s[i][j] - '1'][s[i][j] - '0'][i][j] = 0;
rep(i, 1, n + 1) rep(j, 0, n + 1 - i) dijkstra(j, j + i);
int ans = INF;
rep(h) rep(j, w) chmin(ans, cost[0][n][i][j]);
if(ans == INF) ans = -1;
printf("%d\n", ans);
}
Compilation message
robots.cpp: In function 'void dijkstra(int, int)':
robots.cpp:58:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
auto [cost, x, y] = flag ? q.front() : q2.back();
^
robots.cpp:61:18: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for(auto [x2, y2] : g[x][y]) if(chmin(c[x2][y2], cost + 1)){
^
robots.cpp: In function 'int main()':
robots.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &w, &h);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:70:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
rep(h) scanf("%s", s[i]);
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
111864 KB |
Output is correct |
2 |
Correct |
55 ms |
111792 KB |
Output is correct |
3 |
Correct |
56 ms |
111864 KB |
Output is correct |
4 |
Correct |
55 ms |
111868 KB |
Output is correct |
5 |
Correct |
54 ms |
111864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
111864 KB |
Output is correct |
2 |
Correct |
55 ms |
111792 KB |
Output is correct |
3 |
Correct |
56 ms |
111864 KB |
Output is correct |
4 |
Correct |
55 ms |
111868 KB |
Output is correct |
5 |
Correct |
54 ms |
111864 KB |
Output is correct |
6 |
Correct |
55 ms |
111864 KB |
Output is correct |
7 |
Correct |
54 ms |
111864 KB |
Output is correct |
8 |
Correct |
53 ms |
111864 KB |
Output is correct |
9 |
Correct |
56 ms |
111864 KB |
Output is correct |
10 |
Correct |
54 ms |
111864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
111864 KB |
Output is correct |
2 |
Correct |
55 ms |
111792 KB |
Output is correct |
3 |
Correct |
56 ms |
111864 KB |
Output is correct |
4 |
Correct |
55 ms |
111868 KB |
Output is correct |
5 |
Correct |
54 ms |
111864 KB |
Output is correct |
6 |
Correct |
55 ms |
111864 KB |
Output is correct |
7 |
Correct |
54 ms |
111864 KB |
Output is correct |
8 |
Correct |
53 ms |
111864 KB |
Output is correct |
9 |
Correct |
56 ms |
111864 KB |
Output is correct |
10 |
Correct |
54 ms |
111864 KB |
Output is correct |
11 |
Correct |
160 ms |
116260 KB |
Output is correct |
12 |
Correct |
68 ms |
115576 KB |
Output is correct |
13 |
Correct |
80 ms |
115704 KB |
Output is correct |
14 |
Correct |
399 ms |
116852 KB |
Output is correct |
15 |
Correct |
119 ms |
115984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
111864 KB |
Output is correct |
2 |
Correct |
55 ms |
111792 KB |
Output is correct |
3 |
Correct |
56 ms |
111864 KB |
Output is correct |
4 |
Correct |
55 ms |
111868 KB |
Output is correct |
5 |
Correct |
54 ms |
111864 KB |
Output is correct |
6 |
Correct |
55 ms |
111864 KB |
Output is correct |
7 |
Correct |
54 ms |
111864 KB |
Output is correct |
8 |
Correct |
53 ms |
111864 KB |
Output is correct |
9 |
Correct |
56 ms |
111864 KB |
Output is correct |
10 |
Correct |
54 ms |
111864 KB |
Output is correct |
11 |
Correct |
160 ms |
116260 KB |
Output is correct |
12 |
Correct |
68 ms |
115576 KB |
Output is correct |
13 |
Correct |
80 ms |
115704 KB |
Output is correct |
14 |
Correct |
399 ms |
116852 KB |
Output is correct |
15 |
Correct |
119 ms |
115984 KB |
Output is correct |
16 |
Correct |
148 ms |
124024 KB |
Output is correct |
17 |
Correct |
1121 ms |
125236 KB |
Output is correct |
18 |
Correct |
208 ms |
123788 KB |
Output is correct |
19 |
Correct |
138 ms |
122152 KB |
Output is correct |
20 |
Correct |
715 ms |
124724 KB |
Output is correct |