#include <cstdio>
#include <cstring>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef pair < int, int > pii;
const int N = 515;
const int INF = 0x3f3f3f3f;
const int px[4] = {-1, 0, 1, 0};
const int py[4] = {0, 1, 0, -1};
int dis[N][N][46];
int tmp[N][N][5], obr[N][N][5];
vector < int > kad[N * N];
int L[N], R[N], kod[11][11];
int n, m, rob;
char s[N][N];
inline int encode(int x,int y,int k){
return k * N * N + (x * N + y);
}
void obradi(int k){
memset(obr, 0, sizeof(obr));
memset(tmp, INF, sizeof(tmp));
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
tmp[i][j][4] = dis[i][j][k];
if(tmp[i][j][4] != INF)
kad[tmp[i][j][4]].PB(encode(i, j, 4));
}
}
for(int sad = 0;sad < n * m;sad++){
for(int i = 0;i < (int)kad[sad].size();i++){
int x = (kad[sad][i] % (N * N)) / N, y = kad[sad][i] % N;
int sm = kad[sad][i] / N / N;
//printf("trenutno (%d %d) sm = %d udaljenost = %d\n", x, y, sm, sad);
if(obr[x][y][sm]) continue;
obr[x][y][sm] = 1;
if(sm != 4){
if(s[x][y] == 'C') sm = (sm + 1) % 4;
if(s[x][y] == 'A') sm = (sm + 3) % 4;
int nx = x + px[sm], ny = y + py[sm];
if(s[nx][ny] == 'x'){
if(sad < tmp[x][y][4]){
tmp[x][y][4] = sad;
kad[sad].PB(encode(x, y, 4));
}
}
else{
if(sad < tmp[nx][ny][sm]){
tmp[nx][ny][sm] = sad;
kad[sad].PB(encode(nx, ny, sm));
}
}
}
else{
for(int nsm = 0;nsm < 4;nsm++){
if(sad + 1 < tmp[x][y][nsm]){
tmp[x][y][nsm] = sad + 1;
kad[sad + 1].PB(encode(x, y, nsm));
}
}
}
}
kad[sad].clear();
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(tmp[i][j][4] != INF){
//printf("do (%d %d) u %d koraka\n", i,j, tmp[i][j][4]);
dis[i][j][k] = tmp[i][j][4];
}
}
}
}
void pripremi(){
int gdje = 0;
for(int ln = 1;ln <= rob;ln++){
for(int i = 1;i + ln - 1 <= rob;i++){
L[gdje] = i; R[gdje] = i + ln - 1;
kod[L[gdje]][R[gdje]] = gdje; gdje++;
}
}
}
void spajaj(int k){
int l = L[k], r = R[k];
//printf("spajam %d tj. [%d %d]\n", k, l, r);
if(l != r){
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(s[i][j] == 'x') continue;
for(int ct = l;ct < r;ct++){
//printf("%d + %d\n", dis[i][j][kod[l][ct]], dis[i][j][kod[ct + 1][r]]);
dis[i][j][k] = min(dis[i][j][k], dis[i][j][kod[l][ct]] + dis[i][j][kod[ct + 1][r]]);
}
}
}
}
else{
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(s[i][j] == '0' + l)
dis[i][j][k] = 0;
}
}
}
//printf("obradujem %d tj. [%d %d]\n", k, l, r);
obradi(k);
//printf("obradio\n");
}
int main(){
memset(dis, INF, sizeof(dis));
scanf("%d%d%d", &rob, &m, &n);
n += 2, m += 2;
for(int i = 0;i < m;i++)
s[n - 1][i] = 'x', s[0][i] = 'x';
for(int i = 0;i < n;i++)
s[i][m - 1] = 'x', s[i][0] = 'x';
for(int i = 1;i + 1 < n;i++)
for(int j = 1;j + 1 < m;j++)
scanf(" %c", &s[i][j]);
pripremi();
for(int i = 0;i < rob * (rob + 1) / 2;i++)
spajaj(i);
int ans = INF;
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++)
ans = min(ans, dis[i][j][rob * (rob + 1) / 2 - 1]);
printf("%d\n", (ans == INF ? -1 : ans));
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:125:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &rob, &m, &n);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:133:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c", &s[i][j]);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
64632 KB |
Output is correct |
2 |
Correct |
38 ms |
64640 KB |
Output is correct |
3 |
Correct |
39 ms |
64640 KB |
Output is correct |
4 |
Correct |
39 ms |
64640 KB |
Output is correct |
5 |
Correct |
39 ms |
64640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
64632 KB |
Output is correct |
2 |
Correct |
38 ms |
64640 KB |
Output is correct |
3 |
Correct |
39 ms |
64640 KB |
Output is correct |
4 |
Correct |
39 ms |
64640 KB |
Output is correct |
5 |
Correct |
39 ms |
64640 KB |
Output is correct |
6 |
Correct |
39 ms |
64632 KB |
Output is correct |
7 |
Correct |
39 ms |
64672 KB |
Output is correct |
8 |
Correct |
39 ms |
64636 KB |
Output is correct |
9 |
Correct |
40 ms |
64760 KB |
Output is correct |
10 |
Correct |
40 ms |
64632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
64632 KB |
Output is correct |
2 |
Correct |
38 ms |
64640 KB |
Output is correct |
3 |
Correct |
39 ms |
64640 KB |
Output is correct |
4 |
Correct |
39 ms |
64640 KB |
Output is correct |
5 |
Correct |
39 ms |
64640 KB |
Output is correct |
6 |
Correct |
39 ms |
64632 KB |
Output is correct |
7 |
Correct |
39 ms |
64672 KB |
Output is correct |
8 |
Correct |
39 ms |
64636 KB |
Output is correct |
9 |
Correct |
40 ms |
64760 KB |
Output is correct |
10 |
Correct |
40 ms |
64632 KB |
Output is correct |
11 |
Correct |
364 ms |
75184 KB |
Output is correct |
12 |
Correct |
52 ms |
66168 KB |
Output is correct |
13 |
Correct |
232 ms |
69776 KB |
Output is correct |
14 |
Correct |
499 ms |
79828 KB |
Output is correct |
15 |
Correct |
244 ms |
67380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
64632 KB |
Output is correct |
2 |
Correct |
38 ms |
64640 KB |
Output is correct |
3 |
Correct |
39 ms |
64640 KB |
Output is correct |
4 |
Correct |
39 ms |
64640 KB |
Output is correct |
5 |
Correct |
39 ms |
64640 KB |
Output is correct |
6 |
Correct |
39 ms |
64632 KB |
Output is correct |
7 |
Correct |
39 ms |
64672 KB |
Output is correct |
8 |
Correct |
39 ms |
64636 KB |
Output is correct |
9 |
Correct |
40 ms |
64760 KB |
Output is correct |
10 |
Correct |
40 ms |
64632 KB |
Output is correct |
11 |
Correct |
364 ms |
75184 KB |
Output is correct |
12 |
Correct |
52 ms |
66168 KB |
Output is correct |
13 |
Correct |
232 ms |
69776 KB |
Output is correct |
14 |
Correct |
499 ms |
79828 KB |
Output is correct |
15 |
Correct |
244 ms |
67380 KB |
Output is correct |
16 |
Correct |
465 ms |
65408 KB |
Output is correct |
17 |
Correct |
1219 ms |
113912 KB |
Output is correct |
18 |
Correct |
506 ms |
74092 KB |
Output is correct |
19 |
Correct |
793 ms |
77032 KB |
Output is correct |
20 |
Correct |
994 ms |
103876 KB |
Output is correct |