Submission #151999

# Submission time Handle Problem Language Result Execution time Memory
151999 2019-09-05T20:07:35 Z TadijaSebez Robots (APIO13_robots) C++11
60 / 100
1500 ms 63872 KB
#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];
void Dijkstra()
{
	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:60: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:61: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 6392 KB Output is correct
2 Correct 7 ms 6392 KB Output is correct
3 Correct 9 ms 6392 KB Output is correct
4 Correct 7 ms 6396 KB Output is correct
5 Correct 7 ms 6392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6392 KB Output is correct
2 Correct 7 ms 6392 KB Output is correct
3 Correct 9 ms 6392 KB Output is correct
4 Correct 7 ms 6396 KB Output is correct
5 Correct 7 ms 6392 KB Output is correct
6 Correct 7 ms 6364 KB Output is correct
7 Correct 8 ms 6392 KB Output is correct
8 Correct 7 ms 6392 KB Output is correct
9 Correct 7 ms 6360 KB Output is correct
10 Correct 7 ms 6392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6392 KB Output is correct
2 Correct 7 ms 6392 KB Output is correct
3 Correct 9 ms 6392 KB Output is correct
4 Correct 7 ms 6396 KB Output is correct
5 Correct 7 ms 6392 KB Output is correct
6 Correct 7 ms 6364 KB Output is correct
7 Correct 8 ms 6392 KB Output is correct
8 Correct 7 ms 6392 KB Output is correct
9 Correct 7 ms 6360 KB Output is correct
10 Correct 7 ms 6392 KB Output is correct
11 Correct 157 ms 27848 KB Output is correct
12 Correct 29 ms 11752 KB Output is correct
13 Correct 47 ms 22264 KB Output is correct
14 Correct 593 ms 28452 KB Output is correct
15 Correct 99 ms 27648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6392 KB Output is correct
2 Correct 7 ms 6392 KB Output is correct
3 Correct 9 ms 6392 KB Output is correct
4 Correct 7 ms 6396 KB Output is correct
5 Correct 7 ms 6392 KB Output is correct
6 Correct 7 ms 6364 KB Output is correct
7 Correct 8 ms 6392 KB Output is correct
8 Correct 7 ms 6392 KB Output is correct
9 Correct 7 ms 6360 KB Output is correct
10 Correct 7 ms 6392 KB Output is correct
11 Correct 157 ms 27848 KB Output is correct
12 Correct 29 ms 11752 KB Output is correct
13 Correct 47 ms 22264 KB Output is correct
14 Correct 593 ms 28452 KB Output is correct
15 Correct 99 ms 27648 KB Output is correct
16 Correct 158 ms 63872 KB Output is correct
17 Execution timed out 1570 ms 60420 KB Time limit exceeded
18 Halted 0 ms 0 KB -