File Annotation
Not logged in
52e6fbcc8d 2012-07-14        kinaba: import util;
52e6fbcc8d 2012-07-14        kinaba: import game;
52e6fbcc8d 2012-07-14        kinaba: import output;
52e6fbcc8d 2012-07-14        kinaba: 
52e6fbcc8d 2012-07-14        kinaba: int g_wc = 0;
52e6fbcc8d 2012-07-14        kinaba: 
52e6fbcc8d 2012-07-14        kinaba: void act(Game g)
52e6fbcc8d 2012-07-14        kinaba: {
52e6fbcc8d 2012-07-14        kinaba: 	Pos   ro = g.map.robot;
52e6fbcc8d 2012-07-14        kinaba: 	Pos[] la = g.map.lambdas();
52e6fbcc8d 2012-07-14        kinaba: 	Pos   li = g.map.lift;
52e6fbcc8d 2012-07-14        kinaba: 
52e6fbcc8d 2012-07-14        kinaba: 	char c = 'W';
52e6fbcc8d 2012-07-14        kinaba: 	if( la.empty ) {
52e6fbcc8d 2012-07-14        kinaba: 		auto r = search(g, ro, li);
52e6fbcc8d 2012-07-14        kinaba: 		c = r[0];
52e6fbcc8d 2012-07-14        kinaba: 	} else {
52e6fbcc8d 2012-07-14        kinaba: 		Tuple!(char,int)[] cand;
52e6fbcc8d 2012-07-14        kinaba: 		foreach(lam; la)
52e6fbcc8d 2012-07-14        kinaba: 			cand ~= search(g, ro, lam);
52e6fbcc8d 2012-07-14        kinaba: 		sort!((Tuple!(char,int) c1, Tuple!(char,int) c2){
52e6fbcc8d 2012-07-14        kinaba: 			if(c1[1] != c2[1])
52e6fbcc8d 2012-07-14        kinaba: 				return c1[1] < c2[1];
52e6fbcc8d 2012-07-14        kinaba: 			return c1[0] < c2[0];
52e6fbcc8d 2012-07-14        kinaba: 		})(cand);
52e6fbcc8d 2012-07-14        kinaba: 		c = cand[0][0];
52e6fbcc8d 2012-07-14        kinaba: 	}
52e6fbcc8d 2012-07-14        kinaba: 	if(c=='W') {
52e6fbcc8d 2012-07-14        kinaba: 		g_wc++;
52e6fbcc8d 2012-07-14        kinaba: 		if(g_wc > 10)
52e6fbcc8d 2012-07-14        kinaba: 			c = 'A';
52e6fbcc8d 2012-07-14        kinaba: 	}
52e6fbcc8d 2012-07-14        kinaba: 	else
52e6fbcc8d 2012-07-14        kinaba: 		g_wc = 0;
52e6fbcc8d 2012-07-14        kinaba: 	g.command(c);
52e6fbcc8d 2012-07-14        kinaba: }
52e6fbcc8d 2012-07-14        kinaba: 
52e6fbcc8d 2012-07-14        kinaba: Tuple!(char,int) search(Game g, Pos s, Pos o)
52e6fbcc8d 2012-07-14        kinaba: {
52e6fbcc8d 2012-07-14        kinaba: 	Pos[] q = [o];
52e6fbcc8d 2012-07-14        kinaba: 	bool[][] v = new bool[][](g.map.H+2, g.map.W+2);
52e6fbcc8d 2012-07-14        kinaba: 	for(int step=1; q.length; ++step) {
52e6fbcc8d 2012-07-14        kinaba: 		Pos[] q2;
52e6fbcc8d 2012-07-14        kinaba: 		foreach(p; q) {
52e6fbcc8d 2012-07-14        kinaba: 			int[] dy=[-1,+1,0,0];
52e6fbcc8d 2012-07-14        kinaba: 			int[] dx=[0,0,-1,+1];
52e6fbcc8d 2012-07-14        kinaba: 			for(int i=0; i<4; ++i) {
52e6fbcc8d 2012-07-14        kinaba: 				int y = p.y+dy[i];
52e6fbcc8d 2012-07-14        kinaba: 				int x = p.x+dx[i];
52e6fbcc8d 2012-07-14        kinaba: 				if(v[y][x]) continue;
52e6fbcc8d 2012-07-14        kinaba: 				if(y==s.y && x==s.x) {
52e6fbcc8d 2012-07-14        kinaba: 					if(i==0) return tuple('U',step);
52e6fbcc8d 2012-07-14        kinaba: 					if(i==1) return tuple('D',step);
52e6fbcc8d 2012-07-14        kinaba: 					if(i==2) return tuple('R',step);
52e6fbcc8d 2012-07-14        kinaba: 					if(i==3) return tuple('L',step);
52e6fbcc8d 2012-07-14        kinaba: 				} else if(g.map[y,x]==' '||g.map[y,x]=='\\') {
52e6fbcc8d 2012-07-14        kinaba: 					q2 ~= new Pos(y,x);
52e6fbcc8d 2012-07-14        kinaba: 					v[y][x]=true;
52e6fbcc8d 2012-07-14        kinaba: 				} else if(g.map[y,x]=='.' && g.map[y-1,x]!='*') {
52e6fbcc8d 2012-07-14        kinaba: 					q2 ~= new Pos(y,x);
52e6fbcc8d 2012-07-14        kinaba: 					v[y][x]=true;
52e6fbcc8d 2012-07-14        kinaba: 				}
52e6fbcc8d 2012-07-14        kinaba: 			}
52e6fbcc8d 2012-07-14        kinaba: 		}
52e6fbcc8d 2012-07-14        kinaba: 		q = q2;
52e6fbcc8d 2012-07-14        kinaba: 	}
52e6fbcc8d 2012-07-14        kinaba: 	q = [o];
52e6fbcc8d 2012-07-14        kinaba: 	v = new bool[][](g.map.H+2, g.map.W+2);
52e6fbcc8d 2012-07-14        kinaba: 	for(int step=1000; q.length; ++step) {
52e6fbcc8d 2012-07-14        kinaba: 		Pos[] q2;
52e6fbcc8d 2012-07-14        kinaba: 		foreach(p; q) {
52e6fbcc8d 2012-07-14        kinaba: 			int[] dy=[-1,+1,0,0];
52e6fbcc8d 2012-07-14        kinaba: 			int[] dx=[0,0,-1,+1];
52e6fbcc8d 2012-07-14        kinaba: 			for(int i=0; i<4; ++i) {
52e6fbcc8d 2012-07-14        kinaba: 				int y = p.y+dy[i];
52e6fbcc8d 2012-07-14        kinaba: 				int x = p.x+dx[i];
52e6fbcc8d 2012-07-14        kinaba: 				if(v[y][x]) continue;
52e6fbcc8d 2012-07-14        kinaba: 				if(y==s.y && x==s.x) {
52e6fbcc8d 2012-07-14        kinaba: 					if(i==0) return tuple('U',step);
52e6fbcc8d 2012-07-14        kinaba: 					if(i==1) return tuple('D',step);
52e6fbcc8d 2012-07-14        kinaba: 					if(i==2) return tuple('R',step);
52e6fbcc8d 2012-07-14        kinaba: 					if(i==3) return tuple('L',step);
52e6fbcc8d 2012-07-14        kinaba: 				} else if(g.map[y,x]==' '||g.map[y,x]=='\\') {
52e6fbcc8d 2012-07-14        kinaba: 					q2 ~= new Pos(y,x);
52e6fbcc8d 2012-07-14        kinaba: 					v[y][x]=true;
52e6fbcc8d 2012-07-14        kinaba: 				} else if(g.map[y,x]=='.'/* && g[y-1,x]!='*'*/) {
52e6fbcc8d 2012-07-14        kinaba: 					q2 ~= new Pos(y,x);
52e6fbcc8d 2012-07-14        kinaba: 					v[y][x]=true;
52e6fbcc8d 2012-07-14        kinaba: 				}
52e6fbcc8d 2012-07-14        kinaba: 			}
52e6fbcc8d 2012-07-14        kinaba: 		}
52e6fbcc8d 2012-07-14        kinaba: 		q = q2;
52e6fbcc8d 2012-07-14        kinaba: 	}
52e6fbcc8d 2012-07-14        kinaba: 	return tuple('W', int.max);
52e6fbcc8d 2012-07-14        kinaba: }
52e6fbcc8d 2012-07-14        kinaba: 
52e6fbcc8d 2012-07-14        kinaba: void main(string[] args)
52e6fbcc8d 2012-07-14        kinaba: {
52e6fbcc8d 2012-07-14        kinaba: 	auto g = Game.load(File(args[1]));
52e6fbcc8d 2012-07-14        kinaba: 	g.set_output(new GuardedOutput(g));
52e6fbcc8d 2012-07-14        kinaba: 
52e6fbcc8d 2012-07-14        kinaba: 	while(!g.dead && !g.cleared)
52e6fbcc8d 2012-07-14        kinaba: 		act(g);
52e6fbcc8d 2012-07-14        kinaba: }