Index: src/solver.d ================================================================== --- src/solver.d +++ src/solver.d @@ -1,10 +1,19 @@ // // http://en.wikipedia.org/wiki/F%C5%ABrinkazan // import util; import game; + + +interface Solver +{ + // this(in Game g); + char single_step(); + void force(char c); +} + bool is_spacy(char c) { return c==' ' || c=='.' || c=='R' || c=='!' || c=='\\' || c=='O'; } @@ -97,15 +106,23 @@ } return tuple(death, breath); } -interface Solver +class Queue(T) { - // this(in Game g); - char single_step(); - void force(char c); + alias Tuple!(T,int) t; + + t[] cur, next; + + void push(T v, int p) { (cur.empty ? cur : next) ~= tuple(v,p); } + bool empty() { return cur.empty; } + t pop() { + t v = cur[0]; cur = cur[1..$]; + if(cur.empty) { cur = next; next = null; } + return v; + } } /// /// Solver "Mountain": be immovable like a mountain. /// @@ -593,25 +610,10 @@ else if(s.g.dead) state = Tentative_Stuck; return tuple(s, log, state); } } -class Queue(T) -{ - alias Tuple!(T,int) t; - - t[] cur, next; - - void push(T v, int p) { (cur.empty ? cur : next) ~= tuple(v,p); } - bool empty() { return cur.empty; } - t pop() { - t v = cur[0]; cur = cur[1..$]; - if(cur.empty) { cur = next; next = null; } - return v; - } -} - /// /// Solver "Wind": let your rapidity be that of the wind. /// class 疾如風 : Solver { @@ -618,16 +620,25 @@ Game g; this(in Game g) { this.g = g.clone(); } + + string plan; char single_step() { auto dm = death_move(g); + if( plan.empty || dm[0].count(plan[0]) ) { + plan = think(g, dm[0]); + if( plan.empty ) + plan = "W"; + } - char c = think(g, dm[0]); + char c = plan[0]; + plan = plan[1..$]; + if(c == 'W') { wait_counter++; if(dm[0].count(c) || wait_counter>=3) { c = 'A'; foreach(char cc; "DLRU") @@ -650,11 +661,11 @@ g.command(c); } int wait_counter = 0; - char think(in Game g, string death) + string think(in Game g, string death) { auto Q = new Queue!(Tuple!(Pos,Pos)); Q.push(tuple(g.map.robot.clone(), g.map.robot.clone()), 0); Pos[][] V = new Pos[][](g.map.H+2, g.map.W+2); while(!Q.empty) { @@ -665,20 +676,19 @@ if(V[p.y][p.x]) continue; V[p.y][p.x] = prev; if(g.map[p]=='\\' || g.map[p]=='O') { - Pos goback(Pos p) { - return V[p.y][p.x]; - } + char[] trace; for(;;) { - Pos q = goback(p); - if(q == g.map.robot) - if(q.y==p.y) - return q.x