Submission #47094

# Submission time Handle Problem Language Result Execution time Memory
47094 2018-04-27T12:00:39 Z tmwilliamlin168 Robots (APIO13_robots) C++14
100 / 100
633 ms 138616 KB
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<queue>
 
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define REP(i,m) for(int i=0;i<(int)m;++i)
#define REPN(i,m,in) for(int i=in;i<(int)m;++i)
#define ALL(t) (t).begin(),(t).end()
#define dump(x) cerr<<#x<<" = "<<x<<endl
#define prl cerr<<"LINE "<<__LINE__<<" is called"<<endl
 
using namespace std;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' '; cerr<<endl ; }
 
typedef pair<int,int> pi;
typedef long long int lint;
const int INF=510000000;

inline int in() {
	int result = 0;
	char ch = getchar_unlocked();
	while (true) {
		if(ch >= '0' && ch <= '9')
			break;
		ch = getchar_unlocked();
	}
	result = ch-'0';
	while(true) {
		ch = getchar_unlocked();
		if (ch < '0' || ch > '9')
			break;
		result = result*10 + (ch - '0');
	}
	return result;
}
inline void out(int x) {
	if(x==-1) {
		putchar_unlocked('-');
		putchar_unlocked('1');
		return;
	}
	int rev=x, count = 0;
	if(x == 0) {
putchar_unlocked('0');
return;
}
	while((rev % 10) == 0) {
++count;
rev /= 10;
} //obtain the count of the number of 0s
	rev = 0;
	while(x != 0) {
rev = rev*10 + x % 10;
x /= 10;
} //store reverse of N in rev
	while(rev != 0) {
putchar_unlocked(rev % 10 + '0');
rev /= 10;
}
	while(count--)
putchar_unlocked('0');
}

char buf[505][505];
int dp[50][505][505];
 
pi g[505][505][4];
 
bool vis[505][505][4];
 
int n,w,h;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
pi rec(int y,int x,int d){
    pi& res=g[y][x][d];
	if(res.fr!=-1) return res;
	if(vis[y][x][d]) return res=mp(-2,-2);
	vis[y][x][d]=1;
 
	if(buf[y][x]=='A') d=(d+1)%4;
	if(buf[y][x]=='C') d=(d+3)%4;
 
	int px=x+dx[d],py=y+dy[d];
	if(px<0 || py<0 || px>=w || py>=h || buf[py][px]=='x')
		return res=mp(y,x);
	return res=rec(py,px,d);
}
 
pi pos[10];
int hs[10][10];
 
void prep(int sy,int sx,int id){
	queue<pi> q;
	dp[hs[id][id+1]][sy][sx]=0;
	q.push(mp(sy,sx));
	while(!q.empty()){
		pi cur=q.front();q.pop();
 
		REP(d,4){
			pi to=g[cur.fr][cur.sc][d];
 
			if(to.fr==-2) continue;
 
			if(dp[hs[id][id+1]][to.fr][to.sc]!=-1) continue;
 
			dp[hs[id][id+1]][to.fr][to.sc]=dp[hs[id][id+1]][cur.fr][cur.sc]+1;
 
			q.push(to);
		}
	}
	REP(i,h) REP(j,w) if(dp[hs[id][id+1]][i][j]==-1) dp[hs[id][id+1]][i][j]=INF;
}
 
vector<pi> posByCost[500*501*10];
vector<int> used;
 
int main(){
	int cntt=0;
	REP(i,9) REPN(j,10,i+1) hs[i][j]=cntt++;
	n=in(), w=in(), h=in();
	REP(i,h) {
		char c=getchar_unlocked();
		while(c=='\n'||c=='\r'||c==' ')
			c=getchar_unlocked();
		REP(j, w) {
			buf[i][j]=c;
			c=getchar_unlocked();
		}
	}
	REP(i,h) REP(j,w){
		if(buf[i][j]>='1' && buf[i][j]<='9'){
			pos[buf[i][j]-'1']=mp(i,j);
		}
	}
	memset(g,-1,sizeof(g));
	REP(i,h) REP(j,w) REP(d,4) rec(i,j,d);
	memset(dp,-1,sizeof(dp));
	REP(i,n)
		prep(pos[i].fr,pos[i].sc,i);
	queue<pi> q;
	for(int len=2;len<=n;++len) REP(i,n-len+1){
		int j=i+len;
		pair<int,pi> mini;mini.fr=INF;
		REP(y,h) REP(x,w){
			dp[hs[i][j]][y][x]=INF;
			REP(k,len-1){
				int div=i+k+1;
				dp[hs[i][j]][y][x]=min(dp[hs[i][j]][y][x],dp[hs[i][div]][y][x]+dp[hs[div][j]][y][x]);
			}
			if(dp[hs[i][j]][y][x]!=INF){
				used.pb(dp[hs[i][j]][y][x]);
				posByCost[dp[hs[i][j]][y][x]].pb(mp(y,x));
				mini=min(mini,mp(dp[hs[i][j]][y][x],mp(y,x)));
			}
		}
		if(mini.fr==INF) continue;
		REP(k,posByCost[mini.fr].size()) q.push(posByCost[mini.fr][k]);
		posByCost[mini.fr].clear();
		while(!q.empty()){
			pi p=q.front();q.pop();
			int c=dp[hs[i][j]][p.fr][p.sc];
			if(!posByCost[c+1].empty()){
				REP(k,posByCost[c+1].size()){
					pi p2=posByCost[c+1][k];
					if(dp[hs[i][j]][p2.fr][p2.sc]<c+1) continue;
					q.push(p2);
				}
				posByCost[c+1].clear();
			}
			REP(d,4){
				pi nxt=g[p.fr][p.sc][d];
				if(nxt.fr==-2 || dp[hs[i][j]][nxt.fr][nxt.sc]<=c+1) continue;
				dp[hs[i][j]][nxt.fr][nxt.sc]=c+1;
				q.push(nxt);
			}
		}
		REP(k,used.size()) posByCost[used[k]].clear();
		used.clear();
	}
	int res=INF;
	REP(i,h) REP(j,w) res=min(res,dp[hs[0][n]][i][j]);
	out(res==INF?-1:res);
}
# Verdict Execution time Memory Grader output
1 Correct 94 ms 117116 KB Output is correct
2 Correct 90 ms 117192 KB Output is correct
3 Correct 89 ms 117192 KB Output is correct
4 Correct 89 ms 117352 KB Output is correct
5 Correct 88 ms 117352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 117116 KB Output is correct
2 Correct 90 ms 117192 KB Output is correct
3 Correct 89 ms 117192 KB Output is correct
4 Correct 89 ms 117352 KB Output is correct
5 Correct 88 ms 117352 KB Output is correct
6 Correct 89 ms 117352 KB Output is correct
7 Correct 89 ms 117352 KB Output is correct
8 Correct 89 ms 117352 KB Output is correct
9 Correct 91 ms 117352 KB Output is correct
10 Correct 93 ms 117352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 117116 KB Output is correct
2 Correct 90 ms 117192 KB Output is correct
3 Correct 89 ms 117192 KB Output is correct
4 Correct 89 ms 117352 KB Output is correct
5 Correct 88 ms 117352 KB Output is correct
6 Correct 89 ms 117352 KB Output is correct
7 Correct 89 ms 117352 KB Output is correct
8 Correct 89 ms 117352 KB Output is correct
9 Correct 91 ms 117352 KB Output is correct
10 Correct 93 ms 117352 KB Output is correct
11 Correct 173 ms 121540 KB Output is correct
12 Correct 96 ms 121540 KB Output is correct
13 Correct 114 ms 121540 KB Output is correct
14 Correct 269 ms 124036 KB Output is correct
15 Correct 148 ms 124036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 117116 KB Output is correct
2 Correct 90 ms 117192 KB Output is correct
3 Correct 89 ms 117192 KB Output is correct
4 Correct 89 ms 117352 KB Output is correct
5 Correct 88 ms 117352 KB Output is correct
6 Correct 89 ms 117352 KB Output is correct
7 Correct 89 ms 117352 KB Output is correct
8 Correct 89 ms 117352 KB Output is correct
9 Correct 91 ms 117352 KB Output is correct
10 Correct 93 ms 117352 KB Output is correct
11 Correct 173 ms 121540 KB Output is correct
12 Correct 96 ms 121540 KB Output is correct
13 Correct 114 ms 121540 KB Output is correct
14 Correct 269 ms 124036 KB Output is correct
15 Correct 148 ms 124036 KB Output is correct
16 Correct 222 ms 124036 KB Output is correct
17 Correct 633 ms 138616 KB Output is correct
18 Correct 246 ms 138616 KB Output is correct
19 Correct 208 ms 138616 KB Output is correct
20 Correct 412 ms 138616 KB Output is correct