#include <bits/stdc++.h>
using namespace std;
int const maxK=9;
int const maxN=5e2;
int const inf=0x3f3f3f3f;
int K,N,M;
char c[maxN+3][maxN+3];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
pair<int,int> F[maxN+3][maxN+3][4],F_null={0,0};
int G[maxK+3][maxK+3][maxN+3][maxN+3];
pair<int,int> Cal_F(int i,int j,int d){
if(F[i][j][d]!=F_null){
return F[i][j][d];
}
pair<int,int>&Res=F[i][j][d];
int newd=d;
if(c[i][j]=='A'){
--newd;
if(newd<0){
newd+=4;
}
}
if(c[i][j]=='C'){
++newd;
if(newd>=4){
newd-=4;
}
}
int u=i+dx[newd];
int v=j+dy[newd];
if(u<1||N<u||v<1||M<v||c[u][v]=='x'){
Res={i,j};
}
else{
Res=Cal_F(u,v,newd);
}
return Res;
}
struct Data{
int i,j,x;
Data(int i=0,int j=0,int x=0):i(i),j(j),x(x){}
};
bool Gmin(int&x,int y){
return x>y?x=y,true:false;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>K>>M>>N;
for(int i=1;i<=N;++i){
cin>>c[i]+1;
}
for(int i=1;i<=N;++i){
for(int j=1;j<=M;++j){
for(int d=0;d<4;++d){
if(c[i][j]!='x'){
Cal_F(i,j,d);
}
}
}
}
memset(G,0x3f,sizeof G);
for(int i=1;i<=N;++i){
for(int j=1;j<=M;++j){
if(isdigit(c[i][j])){
G[c[i][j]-'0'][c[i][j]-'0'][i][j]=0;
}
}
}
vector<Data> Sta,Tmp;
Sta.reserve(N*M);
Tmp.reserve(N*M);
for(int r=1;r<=K;++r){
for(int l=r;l>=1;--l){
for(int i=1;i<=N;++i){
for(int j=1;j<=M;++j){
for(int k=l;k<r;++k){
G[l][r][i][j]=min(G[l][r][i][j],G[l][k][i][j]+G[k+1][r][i][j]);
}
if(G[l][r][i][j]!=inf){
Sta.push_back(Data(i,j,G[l][r][i][j]));
}
}
}
sort(Sta.begin(),Sta.end(),
[&](Data const&lhs,Data const&rhs){
return lhs.x>rhs.x;
});
while(!Sta.empty()){
int Val=Sta.back().x;
while(!Sta.empty()&&Sta.back().x==Val){
Tmp.push_back(Sta.back());
Sta.pop_back();
}
while(!Tmp.empty()){
int i=Tmp.back().i;
int j=Tmp.back().j;
int x=Tmp.back().x;
Tmp.pop_back();
for(int k=0;k<4;++k){
int u=F[i][j][k].first;
int v=F[i][j][k].second;
if(Gmin(G[l][r][u][v],x+1)){
Sta.push_back(Data(u,v,G[l][r][u][v]));
}
}
}
}
}
}
int Res=inf;
for(int i=1;i<=N;++i){
for(int j=1;j<=M;++j){
Gmin(Res,G[1][K][i][j]);
}
}
cout<<(Res==inf?-1:Res);
return 0;
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | cin>>c[i]+1;
| ~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
143100 KB |
Output is correct |
2 |
Correct |
60 ms |
142840 KB |
Output is correct |
3 |
Correct |
62 ms |
142864 KB |
Output is correct |
4 |
Correct |
66 ms |
142924 KB |
Output is correct |
5 |
Correct |
60 ms |
142896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
143100 KB |
Output is correct |
2 |
Correct |
60 ms |
142840 KB |
Output is correct |
3 |
Correct |
62 ms |
142864 KB |
Output is correct |
4 |
Correct |
66 ms |
142924 KB |
Output is correct |
5 |
Correct |
60 ms |
142896 KB |
Output is correct |
6 |
Correct |
62 ms |
142788 KB |
Output is correct |
7 |
Correct |
70 ms |
142928 KB |
Output is correct |
8 |
Correct |
72 ms |
143008 KB |
Output is correct |
9 |
Correct |
60 ms |
142792 KB |
Output is correct |
10 |
Correct |
60 ms |
142884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
143100 KB |
Output is correct |
2 |
Correct |
60 ms |
142840 KB |
Output is correct |
3 |
Correct |
62 ms |
142864 KB |
Output is correct |
4 |
Correct |
66 ms |
142924 KB |
Output is correct |
5 |
Correct |
60 ms |
142896 KB |
Output is correct |
6 |
Correct |
62 ms |
142788 KB |
Output is correct |
7 |
Correct |
70 ms |
142928 KB |
Output is correct |
8 |
Correct |
72 ms |
143008 KB |
Output is correct |
9 |
Correct |
60 ms |
142792 KB |
Output is correct |
10 |
Correct |
60 ms |
142884 KB |
Output is correct |
11 |
Correct |
132 ms |
146812 KB |
Output is correct |
12 |
Correct |
75 ms |
146340 KB |
Output is correct |
13 |
Correct |
84 ms |
147072 KB |
Output is correct |
14 |
Correct |
230 ms |
146852 KB |
Output is correct |
15 |
Correct |
98 ms |
146656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
143100 KB |
Output is correct |
2 |
Correct |
60 ms |
142840 KB |
Output is correct |
3 |
Correct |
62 ms |
142864 KB |
Output is correct |
4 |
Correct |
66 ms |
142924 KB |
Output is correct |
5 |
Correct |
60 ms |
142896 KB |
Output is correct |
6 |
Correct |
62 ms |
142788 KB |
Output is correct |
7 |
Correct |
70 ms |
142928 KB |
Output is correct |
8 |
Correct |
72 ms |
143008 KB |
Output is correct |
9 |
Correct |
60 ms |
142792 KB |
Output is correct |
10 |
Correct |
60 ms |
142884 KB |
Output is correct |
11 |
Correct |
132 ms |
146812 KB |
Output is correct |
12 |
Correct |
75 ms |
146340 KB |
Output is correct |
13 |
Correct |
84 ms |
147072 KB |
Output is correct |
14 |
Correct |
230 ms |
146852 KB |
Output is correct |
15 |
Correct |
98 ms |
146656 KB |
Output is correct |
16 |
Correct |
128 ms |
151200 KB |
Output is correct |
17 |
Correct |
546 ms |
151112 KB |
Output is correct |
18 |
Correct |
141 ms |
150840 KB |
Output is correct |
19 |
Correct |
122 ms |
151332 KB |
Output is correct |
20 |
Correct |
375 ms |
151088 KB |
Output is correct |