#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=505;
const int H=N*N;
const int inf=1e9+7;
int n,m,k,nm;
int ID(int x, int y){ return (x-1)*m+y;}
int go[N][N][4],Move[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
char base[N][N];
int Get(int x, int y, int d)
{
if(base[x][y]=='x' || x==0 || y==0 || x>n || y>m) return ID(x-Move[d][0],y-Move[d][1]);
if(go[x][y][d]) return go[x][y][d];
go[x][y][d]=-1;
if(base[x][y]=='A') go[x][y][d]=Get(x+Move[(d+1)%4][0],y+Move[(d+1)%4][1],(d+1)%4);
else if(base[x][y]=='C') go[x][y][d]=Get(x+Move[(d+3)%4][0],y+Move[(d+3)%4][1],(d+3)%4);
else go[x][y][d]=Get(x+Move[d][0],y+Move[d][1],d);
return go[x][y][d];
}
vector<int> E[H];
int dist[10][10][H];
void BFS(int st, int id)
{
queue<int> q;
for(int i=1;i<=nm;i++) dist[id][id][i]=-1;
dist[id][id][st]=0;
q.push(st);
while(q.size())
{
int x=q.front();
q.pop();
for(int y:E[x]) if(dist[id][id][y]==-1)
{
dist[id][id][y]=dist[id][id][x]+1;
q.push(y);
}
}
}
int tmp_d[H];
bool in[H];
void Dijkstra()
{
queue<int> q;
for(int i=1;i<=nm;i++) if(tmp_d[i]!=inf) q.push(i),in[i]=1;
while(q.size())
{
int x=q.front();
q.pop();
in[x]=0;
for(int y:E[x]) if(tmp_d[y]>tmp_d[x]+1)
{
tmp_d[y]=tmp_d[x]+1;
if(!in[y]) q.push(y);
in[y]=1;
}
}
/*priority_queue<pair<int,int>> pq;
for(int i=1;i<=nm;i++) if(tmp_d[i]!=inf) pq.push({-tmp_d[i],i});
while(pq.size())
{
int x=pq.top().second;
int d=-pq.top().first;
pq.pop();
if(d!=tmp_d[x]) continue;
for(int y:E[x]) if(tmp_d[y]>tmp_d[x]+1)
{
tmp_d[y]=tmp_d[x]+1;
pq.push({-tmp_d[y],y});
}
}*/
}
int main()
{
scanf("%i %i %i",&k,&m,&n);nm=n*m;
for(int i=1;i<=n;i++) scanf("%s",base[i]+1);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
{
for(int d=0;d<4;d++)
{
if(Get(i,j,d)!=-1) E[ID(i,j)].pb(go[i][j][d]);
}
}
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(base[i][j]>='1' && base[i][j]<='9')
{
int id=base[i][j]-'0';
BFS(ID(i,j),id);
}
for(int len=2;len<=k;len++) for(int i=1;i<=k-len+1;i++)
{
int j=i+len-1;
for(int u=1;u<=nm;u++) dist[i][j][u]=-1;
for(int z=i;z<j;z++)
{
for(int u=1;u<=nm;u++) if(dist[i][z][u]!=-1 && dist[z+1][j][u]!=-1)
{
int sum=dist[i][z][u]+dist[z+1][j][u];
if(dist[i][j][u]==-1 || sum<dist[i][j][u]) dist[i][j][u]=sum;
}
}
for(int u=1;u<=nm;u++) tmp_d[u]=dist[i][j][u]==-1?inf:dist[i][j][u];
Dijkstra();
for(int u=1;u<=nm;u++) dist[i][j][u]=tmp_d[u]==inf?-1:tmp_d[u];
}
int ans=-1;
for(int u=1;u<=nm;u++) if(dist[1][k][u]!=-1)
{
if(ans==-1 || ans>dist[1][k][u]) ans=dist[1][k][u];
}
printf("%i\n",ans);
return 0;
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:75:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i",&k,&m,&n);nm=n*m;
~~~~~^~~~~~~~~~~~~~~~~~~~~
robots.cpp:76:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%s",base[i]+1);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6264 KB |
Output is correct |
2 |
Correct |
7 ms |
6392 KB |
Output is correct |
3 |
Correct |
7 ms |
6392 KB |
Output is correct |
4 |
Correct |
7 ms |
6392 KB |
Output is correct |
5 |
Correct |
7 ms |
6392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6264 KB |
Output is correct |
2 |
Correct |
7 ms |
6392 KB |
Output is correct |
3 |
Correct |
7 ms |
6392 KB |
Output is correct |
4 |
Correct |
7 ms |
6392 KB |
Output is correct |
5 |
Correct |
7 ms |
6392 KB |
Output is correct |
6 |
Correct |
8 ms |
6316 KB |
Output is correct |
7 |
Correct |
8 ms |
6384 KB |
Output is correct |
8 |
Correct |
7 ms |
6392 KB |
Output is correct |
9 |
Correct |
7 ms |
6392 KB |
Output is correct |
10 |
Correct |
7 ms |
6364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6264 KB |
Output is correct |
2 |
Correct |
7 ms |
6392 KB |
Output is correct |
3 |
Correct |
7 ms |
6392 KB |
Output is correct |
4 |
Correct |
7 ms |
6392 KB |
Output is correct |
5 |
Correct |
7 ms |
6392 KB |
Output is correct |
6 |
Correct |
8 ms |
6316 KB |
Output is correct |
7 |
Correct |
8 ms |
6384 KB |
Output is correct |
8 |
Correct |
7 ms |
6392 KB |
Output is correct |
9 |
Correct |
7 ms |
6392 KB |
Output is correct |
10 |
Correct |
7 ms |
6364 KB |
Output is correct |
11 |
Execution timed out |
1561 ms |
17144 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6264 KB |
Output is correct |
2 |
Correct |
7 ms |
6392 KB |
Output is correct |
3 |
Correct |
7 ms |
6392 KB |
Output is correct |
4 |
Correct |
7 ms |
6392 KB |
Output is correct |
5 |
Correct |
7 ms |
6392 KB |
Output is correct |
6 |
Correct |
8 ms |
6316 KB |
Output is correct |
7 |
Correct |
8 ms |
6384 KB |
Output is correct |
8 |
Correct |
7 ms |
6392 KB |
Output is correct |
9 |
Correct |
7 ms |
6392 KB |
Output is correct |
10 |
Correct |
7 ms |
6364 KB |
Output is correct |
11 |
Execution timed out |
1561 ms |
17144 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |