Snakes and Ladders: The Quickest Way Up

  • + 0 comments

    Thanks for the wonderful link. Here is my solution in java entirely based on the idea provied in the link

    import java.io.; import java.util.;

    public class QuickestWayUpLadder { private static byte MAX_ROLL = 6; private static byte BOARD_LEN = 100; private static Map> adjencyMap=new HashMap>(); private static Map listSnakes=new HashMap(); private static Map listLadders=new HashMap();

    public static void main(String[] args) throws IOException { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    //Create board
    final byte[] board = new byte[BOARD_LEN];
    
    //For every test case
    for(byte T = Byte.parseByte(br.readLine()); T > 0; --T){
    
      //Initialize board
      for(byte i = 0; i < BOARD_LEN; ++i){
        board[i] = i;
      }
    
      //Get ladder
      for(byte N = Byte.parseByte(br.readLine()); N > 0; --N){
        final String[] line = br.readLine().split(" ");
        final byte S = (byte)(Byte.parseByte(line[0]) - 1);
        final byte E = (byte)(Byte.parseByte(line[1]) - 1);
        listLadders.put(S,E);
      }
    
      //Get snakes
      for(byte M = Byte.parseByte(br.readLine()); M > 0; --M){
        final String[] line = br.readLine().split(" ");
        final byte S = (byte)(Byte.parseByte(line[0]) - 1);
        final byte E = (byte)(Byte.parseByte(line[1]) - 1);
        listSnakes.put(S,E);
      }
      System.out.println();
      for(byte i = 0; i < BOARD_LEN; ++i){
          System.out.print(board[i]+" ");
        }
    
      createAdjacencyList(board);
      replace();
      printAdjencyList();
    
      breathFirstSearch();
      //Solve and print output
    

    // System.out.println(getMinMoves(board)); } }

    private static void createAdjacencyList(byte[] board){

       for(byte i=0;i<board.length;i++){
    
           for(byte j=0;j<7;j++){
             if(i+j>99)
                 break;
               if(adjencyMap.containsKey(board[i]))
                adjencyMap.get(board[i]).add(board[i+j]);
               else
                adjencyMap.put(board[i],new ArrayList(board[i+j]));
           }
       }
    

    }

    private static void printAdjencyList(){ for(byte i :adjencyMap.keySet()){ System.out.println(); System.out.print("key: "+i+"values: "); for(byte temp: adjencyMap.get(i)) { System.out.print(temp+" "); } } }

    private static void replace(){ for(Byte t: adjencyMap.keySet()){ List temp=new ArrayList();

           for(Byte t1: adjencyMap.get(t))
       {
    
           if(listLadders.containsKey(t1)){
                temp.add(listLadders.get(t1));
           }
           else if(listSnakes.containsKey(t1)){
            temp.add(listSnakes.get(t1));
           }
           else
           temp.add(t1);
       }
    
       adjencyMap.put(t,temp);
       }
    

    }

    private static void breathFirstSearch(){ int count=0; byte i=0; while(i!=99){ List temp=adjencyMap.get(i); Collections.sort(temp); i=temp.get(temp.size()-1); count++; if(i>99) break; } System.out.println(); System.out.println("number of steps: "+count); } }