UVA 11459 – Snakes and Ladders

Simple simulation game. Don’t forget to read through the entire input if the game ends early! That cost me a couple of tries :[

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P11459 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int cases = sc.nextInt();
    while (cases > 0) {
      int players = sc.nextInt();
      int cheats = sc.nextInt();
      int rolls = sc.nextInt();

      int[] map = new int[101];
      for (int i = 0; i < cheats; i++) {
        map[sc.nextInt()] = sc.nextInt();
      }

      int playerIdx = 0;
      int[] pos = new int[players];
      Arrays.fill(pos, 1);
      for (int i = 0; i < rolls; i++) {
        pos[playerIdx] += sc.nextInt();
        if (map[pos[playerIdx]] != 0)
          pos[playerIdx] = map[pos[playerIdx]];
        if (pos[playerIdx] >= 100) {
          pos[playerIdx] = 100;
          while (i + 1 < rolls) {
            sc.nextInt();
            i++;
          }
          break;
        }
        playerIdx++;
        if (playerIdx == players)
          playerIdx = 0;
      }
      for (int i = 0; i < players; i++) {
        out.println("Position of player " + (i + 1) + " is " + pos[i] + ".");
      }
      cases--;
    }

    out.close();
    sc.close();
  }
}

5 thoughts on “UVA 11459 – Snakes and Ladders”

  1. import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.Map;

    /**
    *
    * @author Administrator
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException {
    // TODO code application logic here
    BufferedReader r= new BufferedReader (new InputStreamReader (System.in));
    int t=Integer.parseInt(r.readLine());
    for(int i=0 ;i<t;i++){
    String[] s =r.readLine().split((" "));
    int players=Integer.parseInt(s[0]);
    int play[]=new int [players];
    for(int j=0 ;j<players;j++){
    play[j]=1;}
    int snake = Integer.parseInt(s[1]);
    int rolls = Integer.parseInt(s[2]);

    Map map =new HashMap();
    for(int j=0 ;j<snake;j++){
    String[] s1 =r.readLine().split(" ");

    map.put(Integer.parseInt(s1[0]),Integer.parseInt(s1[1]));

    }

    int x=0;

    for(int j=0 ;j=100) { play[x]=100;
    while(j+1<rolls){
    r.readLine();
    j++;
    } break;}
    x++;
    if(x==players) x=0;
    }

    int ii=0;
    for(;ii<players;ii++){

    System.out.printf("Position of player %d is %d.%n",(ii+1),play[ii]);
    }

    }
    r.close();
    }}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.