We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
Snakes and Ladders: The Quickest Way Up
You are viewing a single comment's thread. Return to all 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));
// System.out.println(getMinMoves(board)); } }
private static void createAdjacencyList(byte[] board){
}
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();
}
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); } }