Submission #1366623

#TimeUsernameProblemLanguageResultExecution timeMemory
1366623053thousandRobots (APIO13_robots)C++20
60 / 100
1599 ms154004 KiB
#include<bits/stdc++.h>
using namespace std;
	int a,b,c,d,e,f,g,xx,yy,dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}},gr[505][505][4][2],p[505][505][9][9];
	string chh[505];
	char ch[505][505];
	priority_queue<pair<int,pair<pair<int,int>,int>>>pp;
void dfs(int x,int y,int di){
	if(gr[x][y][di][0]!=-1) return;
	int dz=di;
	if(ch[x][y]=='C') dz=(dz+1)%4;
	if(ch[x][y]=='A') dz=(dz+3)%4;
	int nx=x+dir[dz][0],ny=y+dir[dz][1];
//	cout<<nx<<' '<<ny<<endl;
	if(ch[nx][ny]!='x' and nx>=0 and nx<xx and ny>=0 and ny<yy){
		dfs(nx,ny,dz);
		gr[x][y][di][0]=gr[nx][ny][dz][0];
		gr[x][y][di][1]=gr[nx][ny][dz][1];	
	}
	else gr[x][y][di][0]=x,gr[x][y][di][1]=y;
}
int main(){
//	for(int z=0;z<4;z++){
//		cout<<dir[z][0]<<' '<<dir[z][1]<<"\n";
//	}
    cin>>a>>xx>>yy;
    for(int i=0;i<yy;i++) cin>>chh[i];
    for(int i=0;i<xx;i++){
    	for(int h=0;h<yy;h++){
    		ch[i][h]=chh[h][i];
    		for(int z=0;z<4;z++){
    			gr[i][h][z][0]=-1;
    			gr[i][h][z][1]=-1;
			}
    		if(ch[i][h]>'0' and ch[i][h]<='9'){
    			pp.push({0,{{i,h},(ch[i][h]-'1')*11}});
			}
		}
	}
//	dfs(0,0,3);
	for(int i=0;i<xx;i++){
		for(int h=0;h<yy;h++){
			for(int z=0;z<9;z++){
				for(int zz=z;zz<9;zz++){
					p[i][h][z][zz]=-1e9;
				}
			}
			for(int z=0;z<4;z++){
				if(gr[i][h][z][0]==-1){
					dfs(i,h,z);
				}
//				cout<<i<<' '<<h<<' '<<z<<' '<<gr[i][h][z][0]<<' '<<gr[i][h][z][1]<<"\n";
			}
		}
	}
//	cout<<"\n\n\n\n";
	while(!pp.empty()){
		int p1=pp.top().first,x1=pp.top().second.first.first,y1=pp.top().second.first.second,rob=pp.top().second.second;
		pp.pop();
		if(rob==a-1){
			cout<<-p1;
			return 0;
		}
		if(p[x1][y1][rob/10][rob%10]<0){	
		p[x1][y1][rob/10][rob%10]=-p1;
//		cout<<x1<<' '<<y1<<' '<<rob/10<<' '<<rob%10<<' '<<-p1<<"\n";
			for(int i=0;i<rob/10;i++){
				if(p[x1][y1][i][(rob/10)-1]>=0 and p[x1][y1][i][rob%10]<0){
					if(p1-p[x1][y1][i][(rob/10)-1]>p[x1][y1][i][rob%10]){
						p[x1][y1][i][rob%10]=p1-p[x1][y1][i][(rob/10)-1];
						pp.push({p1-p[x1][y1][i][(rob/10)-1],{{x1,y1},i*10+rob%10}});
					}
				}
			}
			for(int i=rob%10+1;i<9;i++){
				if(p[x1][y1][rob%10+1][i]>=0 and p[x1][y1][rob/10*10][i]<0){
					if(p1-p[x1][y1][rob%10+1][i]>p[x1][y1][rob/10][i]){
						p[x1][y1][rob/10][i]=p1-p[x1][y1][rob%10+1][i];
						pp.push({p1-p[x1][y1][rob%10+1][i],{{x1,y1},(rob/10)*10+i}});
					}
				}
			}
			for(int i=0;i<4;i++){
				int nx=gr[x1][y1][i][0],ny=gr[x1][y1][i][1];
				if(p[nx][ny][rob/10][rob%10]<0){
					if(p[nx][ny][rob/10][rob%10]<p1-1){
						p[nx][ny][rob/10][rob%10]=p1-1;
						pp.push({p1-1,{{nx,ny},rob}});
					}
				}
			}
		}
	}
	cout<<-1;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...