#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 500;
const int INF = 987654321;
const int MAXVAL = 1e6;
const int dy[]={-1, 1, 0, 0};
const int dx[]={0, 0, -1, 1};
const int dA[]={2, 3, 1, 0};
const int dC[]={3, 2, 0, 1};
int N, W, H;
char A[MAXN+10][MAXN+10];
pii dest[MAXN+10][MAXN+10][4];
int dp[10][10][MAXN+10][MAXN+10];
void dfs(int y, int x, int dir)
{
if(dest[y][x][dir]!=pii(-1, -1)) return;
int ndir;
if(A[y][x]=='A') ndir=dA[dir];
else if(A[y][x]=='C') ndir=dC[dir];
else ndir=dir;
int ny=y+dy[ndir], nx=x+dx[ndir];
if(A[ny][nx]=='x')
{
dest[y][x][dir]={y, x};
return;
}
dfs(ny, nx, ndir);
dest[y][x][dir]=dest[ny][nx][ndir];
}
struct Queue
{
int y, x, w;
bool operator < (const Queue &p) const { return w>p.w; }
};
struct dequeue
{
Queue arr[MAXVAL];
int l, r;
void init() { l=r=MAXVAL/2; }
Queue front() { return arr[l]; }
void push_back(Queue x) { arr[r++]=x; }
void push_front(Queue x) { arr[--l]=x; }
void pop_front () { l++; }
bool empty() { return l==r; }
};
dequeue Q;
int main()
{
int i, j, k, p, q;
scanf("%d%d%d", &N, &W, &H);
for(i=1; i<=H; i++) scanf("%s", A[i]+1);
for(i=0; i<=H+1; i++) A[i][0]=A[i][W+1]='x';
for(j=0; j<=W+1; j++) A[0][j]=A[H+1][j]='x';
for(i=1; i<=H; i++) for(j=1; j<=W; j++) for(k=0; k<4; k++) dest[i][j][k]={-1, -1};
for(i=1; i<=H; i++) for(j=1; j<=W; j++) for(k=0; k<4; k++)
{
if(dest[i][j][k]!=pii(-1, -1)) continue;
dfs(i, j, k);
}
//for(i=1; i<=H; i++) { for(j=1; j<=W; j++) printf("(%d %d / %d %d / %d %d / %d %d) ", dest[i][j][0].first, dest[i][j][0].second, dest[i][j][1].first, dest[i][j][1].second, dest[i][j][2].first, dest[i][j][2].second, dest[i][j][3].first, dest[i][j][3].second); printf("\n"); }
for(q=1; q<=N; q++) for(p=q; p>=1; p--)
{
vector<Queue> PQ;
for(i=1; i<=H; i++) for(j=1; j<=W; j++) dp[p][q][i][j]=INF;
if(p==q)
{
for(i=1; i<=H; i++) for(j=1; j<=W; j++)
{
int now;
if(A[i][j]=='0'+p) now=0;
else now=INF;
PQ.push_back({i, j, now});
}
}
else
{
for(i=1; i<=H; i++) for(j=1; j<=W; j++)
{
int now=INF;
for(k=p; k<q; k++)
{
now=min(now, dp[p][k][i][j]+dp[k+1][q][i][j]);
}
PQ.push_back({i, j, now});
}
}
sort(PQ.begin(), PQ.end());
Q.init();
Q.push_back(PQ.back()); PQ.pop_back();
while(!Q.empty())
{
while(!PQ.empty() && PQ.back().w==Q.front().w) Q.push_front(PQ.back()), PQ.pop_back();
Queue T=Q.front(); Q.pop_front();
//printf("%d %d %d\n", T.y, T.x, T.w);
if(dp[p][q][T.y][T.x]<=T.w) continue;
dp[p][q][T.y][T.x]=T.w;
for(k=0; k<4; k++) if(dp[p][q][dest[T.y][T.x][k].first][dest[T.y][T.x][k].second]>T.w+1) Q.push_back({dest[T.y][T.x][k].first, dest[T.y][T.x][k].second, T.w+1});//, printf("?%d %d %d\n", dest[T.y][T.x][k].first, dest[T.y][T.x][k].second, T.w+1);
}
//printf("=======%d %d========\n", p, q);
//for(i=1; i<=H; i++) { for(j=1; j<=W; j++) printf("%d ", dp[p][q][i][j]); printf("\n"); }
}
int ans=INF;
for(i=1; i<=H; i++) for(j=1; j<=W; j++) ans=min(ans, dp[1][N][i][j]);
if(ans==INF) printf("-1");
else printf("%d", ans);
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:65: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:66:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=H; i++) scanf("%s", A[i]+1);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
265 ms |
34696 KB |
Output is correct |
12 |
Correct |
13 ms |
6900 KB |
Output is correct |
13 |
Correct |
144 ms |
24112 KB |
Output is correct |
14 |
Correct |
348 ms |
34632 KB |
Output is correct |
15 |
Correct |
257 ms |
35316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
265 ms |
34696 KB |
Output is correct |
12 |
Correct |
13 ms |
6900 KB |
Output is correct |
13 |
Correct |
144 ms |
24112 KB |
Output is correct |
14 |
Correct |
348 ms |
34632 KB |
Output is correct |
15 |
Correct |
257 ms |
35316 KB |
Output is correct |
16 |
Correct |
624 ms |
60348 KB |
Output is correct |
17 |
Correct |
926 ms |
61472 KB |
Output is correct |
18 |
Correct |
705 ms |
63744 KB |
Output is correct |
19 |
Correct |
623 ms |
60060 KB |
Output is correct |
20 |
Correct |
869 ms |
61608 KB |
Output is correct |