// $Id: maze2.java 53 2008-05-01 05:50:20Z edsko $ // $Source: /home/edsko/acm/2004/maze/source/RCS/maze2.java,v $ import java.io.*; import java.util.*; class XY { int x; int y; } class Maze { static final int MAXW=20; static final int WSIZE=MAXW*2 + 2; static final int MAXH=20; static final int HSIZE=MAXH*2 + 2; static final int INITIAL_X=MAXW + 1; static final int INITIAL_Y=MAXH + 1; static final int UNK=0; static final int EMPTY=1; static final int FULL=2; static final int EXIT=3; int [][] g; Maze() { g=new int[WSIZE][HSIZE]; for (int x=0; x < WSIZE; x++) { for (int y=0; y < HSIZE; y++) { g[x][y]=UNK; } } } static XY Nextxy(XY cxy, int d) { final int [] dx= { 0, 1, 1, 0,-1,-1}; final int [][] dy={{-1, 0, 1, 1, 1, 0} ,{-1,-1, 0, 1, 0,-1}}; XY nxy=new XY(); nxy.x=cxy.x + dx[d - 1]; nxy.y=cxy.y + dy[cxy.x%2][d - 1]; return nxy; } void Display() { for (int y=0; y < HSIZE; y++) { for (int x=0; x < WSIZE; x++) { switch (g[x][y]) { case UNK: System.err.print('?'); break; case EMPTY: System.err.print(' '); break; case FULL: System.err.print('*'); break; case EXIT: System.err.print('E'); break; } } System.err.println(); } } void Explore(BufferedReader f,XY cxy) throws IOException { XY nxy; final int [] reverse={4,5,6,1,2,3}; String s; // System.out.println("--- [" + cxy.x + "," + cxy.y + "]"); g[cxy.x][cxy.y]=EMPTY; for (int d=1; d <= 6; d++) { nxy=Nextxy(cxy,d); if (g[nxy.x][nxy.y] == UNK) { System.err.println(d); System.err.flush(); System.out.println(d); System.out.flush(); s=f.readLine(); if (s.equals("E")) { g[nxy.x][nxy.y]=EXIT; Display(); System.exit(0); } // System.out.println("nx=" + nxy.x + " ny=" + nxy.y + " cx=" + cxy.x + " cy=" + cxy.y + " d=" + d); if (s.equals("y")) { if (g[nxy.x][nxy.y] == UNK) { Explore(f,nxy); // recurse System.out.println(reverse[d - 1]); System.out.flush(); // back out --- I'm done exploring that cell s=f.readLine(); } else { // back out --- this node has been or is in the process of exploration System.out.println(reverse[d - 1]); System.out.flush(); s=f.readLine(); } } else { g[nxy.x][nxy.y]=FULL; } } } // System.out.println(" [" + cxy.x + "," + cxy.y + "] <<<"); } } class aout { private static final String rcsid="$Id: maze2.java 53 2008-05-01 05:50:20Z edsko $"; public static void main(String [] args) throws IOException { XY cxy=new XY(); cxy.x=Maze.MAXW + 1; cxy.y=Maze.MAXH + 1; Maze m=new Maze(); BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); m.Explore(stdin,cxy); } }