UVA 10050 – Hartals

Simultaed all the holidays and then removed them from the list of total days.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P10050 {
  public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);

    int numTestCases = Integer.parseInt(br.readLine().trim());
    for (int testCase = 1; testCase <= numTestCases; testCase++) {
      int N = Integer.parseInt(br.readLine().trim());
      int P = Integer.parseInt(br.readLine().trim());
      HashSet<Integer> ans = new HashSet<Integer>();
      for (int i = 0; i < P; i++) {
        int tmp = Integer.parseInt(br.readLine().trim());
        int max = N / tmp;
        for (int j = 1; j <= max; j++) {
          ans.add(tmp * j);
        }
      }
      int max = N / 7 + 1;
      for (int j = 1; j <= max; j++) {
        int tmp = 7 * j;
        if (ans.contains(tmp))
          ans.remove(tmp);
        if (ans.contains(tmp - 1))
          ans.remove(tmp - 1);
      }
      out.println(ans.size());
    }
    out.close();
  }
}

UVA 10038 – Jolly Jumpers

Easy problem. Take all differences, put them in a HashSet and then check if all the requred numbers are in the HashSet or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.StringTokenizer;

/**
 *
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 *
 */
public class P10038 {
  public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);
    StringTokenizer st;

    String line = null;
    while ((line = br.readLine()) != null) {
      st = new StringTokenizer(line.trim());
      int N = Integer.parseInt(st.nextToken());
      if (N == 1) {
        out.println("Jolly");
      } else {
        HashSet<Integer> ans = new HashSet<Integer>();
        int last = 0;
        for (int i = 0; i < N; i++) {
          if (i == 0) {
            last = Integer.parseInt(st.nextToken());
          } else {
            int tmp = Integer.parseInt(st.nextToken());
            ans.add(Math.abs(tmp - last));
            last = tmp;
          }
        }

        if (ans.size() != N - 1) {
          out.println("Not jolly");
        } else {
          boolean found = true;
          for (int i = 1; i < N; i++) {
            if (!ans.contains(i)) {
              found = false;
              break;
            }
          }
          if (found) {
            out.println("Jolly");
          } else {
            out.println("Not jolly");
          }
        }
      }
    }
    out.close();
    br.close();
  }
}

UVA 11340 – Newspaper

Super easy problem if you’re used to a bit of C++ and familiar to the printf method.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;
 
/**
 *
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 *
 */
public class P11340 {
 
  public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);
 
    int N = Integer.parseInt(sc.readLine());
    for (int zz = 0; zz < N; zz++) {
      int K = Integer.parseInt(sc.readLine());
      HashMap<Character, Integer> map = new HashMap<Character, Integer>();
      StringTokenizer st = null;
      for (int yy = 0; yy < K; yy++) {
        st = new StringTokenizer(sc.readLine());
        map.put(st.nextToken().charAt(0), Integer.parseInt(st.nextToken()));
      }
      double ans = 0;
      int M = Integer.parseInt(sc.readLine());
      for (int xx = 0; xx < M; xx++) {
        char[] line = sc.readLine().toCharArray();
        for (Character c : line) {
          if (map.containsKey(c)) {
            ans += map.get(c);
          }
        }
      }
      ans /= 100;
      out.printf("%.2f$" + System.lineSeparator(), ans);
    }
 
    out.close();
    sc.close();
  }
}

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();
  }
}

UVA 10189 – Minesweeper

This is a problem that I solved earlier for the Programming Challenges book. Pretty simple problem.

import java.util.Scanner;

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

  private static int[] arr = { -1, 0, 1 };

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int count = 1;
    while (sc.hasNextInt()) {
      int y = sc.nextInt();
      int x = sc.nextInt();
      if (x == 0 && y == 0)
        break;
      if (count != 1)
        System.out.println();
      char[][] field = new char[y + 2][x + 2];
      for (int i = 0; i < y; i++) {
        String t = sc.next();
        for (int j = 0; j < x; j++)
          field[i + 1][j + 1] = t.charAt(j);
      }

      for (int i = 0; i < y; i++) {
        for (int j = 0; j < x; j++) {
          int mines = 0;
          if (field[i + 1][j + 1] == '*')
            continue;
          if (field[i + 1][j + 1] == '.') {
            for (int zz = 0; zz < arr.length; zz++)
              for (int yy = 0; yy < arr.length; yy++)
                if (zz == 1 && yy == 1)
                  continue;
                else if (field[i + 1 + arr[zz]][j + 1 + arr[yy]] == '*')
                  mines++;

            String zxc = "" + mines;
            field[i + 1][j + 1] = (zxc.charAt(0));
          }

        }
      }

      System.out.println("Field #" + count + ":");
      for (int i = 0; i < y; i++) {
        for (int j = 0; j < x; j++) {
          if (j != x - 1)
            System.out.print(field[i + 1][j + 1]);
          else
            System.out.println(field[i + 1][j + 1]);
        }
      }
      count++;
    }
    sc.close();
  }
}

UVA 489 – Hangman Judge

Pretty easy problem. The word to guess can be safely put into a HashSet and we can keep guessing until the set is empty or until we are done with (7) guesses that are allowed. One thing to note is that the order of the input matters but duplicates in the input do not matter. Therefore, I use an ArrayList as well as a HashSet to store the input and get AC.

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

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

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

    while (sc.hasNextInt()) {
      int N = sc.nextInt();
      if (N == -1)
        break;

      char[] word = sc.next().toCharArray();
      HashSet<Character> hax = new HashSet<Character>();
      for (Character c : word) {
        hax.add(c);
      }

      // Order Matters
      char[] guesses = sc.next().toCharArray();
      ArrayList<Character> hax3 = new ArrayList<Character>();
      HashSet<Character> hax2 = new HashSet<Character>();
      for (Character c : guesses) {
        if (!hax2.contains(c)) {
          hax3.add(c);
        }
        hax2.add(c);
      }

      int movesLeft = 7;
      for (Character c : hax3) {
        if (hax.contains(c)) {
          hax.remove(c);
        } else {
          movesLeft--;
        }
        if (hax.size() == 0 || movesLeft == 0)
          break;
      }

      out.println("Round " + N);
      if (movesLeft < 1) {
        out.println("You lose.");
      } else if (hax.size() == 0) {
        out.println("You win.");
      } else {
        out.println("You chickened out.");
      }
    }

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

UVA 10284 – Chessboard in FEN

This is an easy problem as long as you have done UVA 10196 – Check The Check. I added another “deathByKing” method and just basically used 99% of the old code to solve this problem. The time limit is 3 seconds so a lazy O(N^2) works great.

import java.awt.Point;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    while (sc.hasNext()) {
      char[][] grid = new char[8][8];
      StringTokenizer st = new StringTokenizer(sc.next(), "/");
      for (int i = 0; i < 8; i++) {
        char[] row = st.nextToken().trim().toCharArray();
        int idx = 0;
        for (Character c : row) {
          if (Character.isDigit(c)) {
            int tmp = c - '0';
            for (int j = idx; j < idx + tmp; j++) {
              grid[i][j] = '.';
            }
            idx += tmp;
          } else {
            grid[i][idx] = c;
            idx++;
          }
        }
      }
      int ans = 0;
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (grid[i][j] == '.') {
            Point tmp = new Point(i, j);
            if (!deathByKing(tmp, grid)) {
              int count = 0;
              grid[i][j] = 'X';
              if (!deathByBishop(tmp, grid) && !deathByKnight(tmp, grid) && !deathByPawn(tmp, grid) && !deathByRook(tmp, grid)) {
                count++;
              }
              grid[i][j] = 'x';
              if (!deathByBishop(tmp, grid) && !deathByKnight(tmp, grid) && !deathByPawn(tmp, grid) && !deathByRook(tmp, grid)) {
                count++;
              }
              grid[i][j] = '.';
              if (count == 2)
                ans++;
            }
          }
        }
      }
      out.println(ans);
    }
    out.close();
    sc.close();
  }

  private static boolean deathByKing(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    for (int n = i - 1; n <= i + 1; n++)
      for (int m = j - 1; m <= j + 1; m++) {
        if (n >= 0 && n < 8 && m >= 0 && m < 8 && (grid[n][m] == 'k' || grid[n][m] == 'K'))
          return true;
      }
    return false;
  }

  private static boolean deathByBishop(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    char thing2 = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      thing = 'B';
      thing2 = 'Q';
    } else {
      // White King
      thing = 'b';
      thing2 = 'q';
    }

    // NW
    int idx = i - 1;
    int idx2 = j - 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx--;
      idx2--;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    // NE
    idx = i - 1;
    idx2 = j + 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx--;
      idx2++;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    // SW
    idx = i + 1;
    idx2 = j - 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx++;
      idx2--;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    // RIGHT
    idx = i + 1;
    idx2 = j + 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx++;
      idx2++;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    return false;
  }

  private static boolean deathByRook(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    char thing2 = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      thing = 'R';
      thing2 = 'Q';
    } else {
      // White King
      thing = 'r';
      thing2 = 'q';
    }

    // UP
    int idx = i - 1;
    while (idx >= 0 && grid[idx][j] == '.') {
      idx--;
    }
    if (idx >= 0 && idx < 8 && (grid[idx][j] == thing || grid[idx][j] == thing2))
      return true;

    // DOWN
    idx = i + 1;
    while (idx < 8 && grid[idx][j] == '.') {
      idx++;
    }
    if (idx >= 0 && idx < 8 && (grid[idx][j] == thing || grid[idx][j] == thing2))
      return true;

    // LEFT
    idx = j - 1;
    while (idx >= 0 && grid[i][idx] == '.') {
      idx--;
    }
    if (idx >= 0 && idx < 8 && (grid[i][idx] == thing || grid[i][idx] == thing2))
      return true;

    // RIGHT
    idx = j + 1;
    while (idx < 8 && grid[i][idx] == '.') {
      idx++;
    }
    if (idx >= 0 && idx < 8 && (grid[i][idx] == thing || grid[i][idx] == thing2))
      return true;

    return false;
  }

  private static boolean deathByKnight(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      thing = 'N';
    } else {
      // White King
      thing = 'n';
    }
    if (i + 2 < 8) {
      if (j - 1 >= 0 && grid[i + 2][j - 1] == thing)
        return true;
      if (j + 1 < 8 && grid[i + 2][j + 1] == thing)
        return true;
    }
    if (i - 2 >= 0) {
      if (j - 1 >= 0 && grid[i - 2][j - 1] == thing)
        return true;
      if (j + 1 < 8 && grid[i - 2][j + 1] == thing)
        return true;
    }
    if (j + 2 < 8) {
      if (i - 1 >= 0 && grid[i - 1][j + 2] == thing)
        return true;
      if (i + 1 < 8 && grid[i + 1][j + 2] == thing)
        return true;
    }
    if (j - 2 >= 0) {
      if (i - 1 >= 0 && grid[i - 1][j - 2] == thing)
        return true;
      if (i + 1 < 8 && grid[i + 1][j - 2] == thing)
        return true;
    }

    return false;
  }

  private static boolean deathByPawn(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      i++;
      thing = 'P';
    } else {
      // White King
      i--;
      thing = 'p';
    }
    if (i >= 0 && i < 8) {
      if (j - 1 >= 0 && grid[i][j - 1] == thing)
        return true;
      if (j + 1 < 8 && grid[i][j + 1] == thing)
        return true;
    }
    return false;
  }
}

UVA 10196 – Check The Check

This was a fun problem. This is a really good example of a problem where functions make your code extremely easy to read and write. I did a bit of trickery so that I could use the same functions for both white and black kings.

import java.awt.Point;
import java.io.PrintWriter;
import java.util.Scanner;

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

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

    int count = 1;
    while (sc.hasNext()) {
      char[][] grid = new char[8][8];
      Point blackKing = null;
      Point whiteKing = null;
      for (int i = 0; i < 8; i++) {
        char[] tmp = sc.next().trim().toCharArray();
        for (int j = 0; j < 8; j++) {
          grid[i][j] = tmp[j];
          if (tmp[j] == 'k') {
            blackKing = new Point(i, j);
          } else if (tmp[j] == 'K') {
            whiteKing = new Point(i, j);
          }
        }
      }
      if (blackKing == null || whiteKing == null)
        break;
      if (inCheck(blackKing, grid)) {
        out.println("Game #" + count + ": black king is in check.");
      } else if (inCheck(whiteKing, grid)) {
        out.println("Game #" + count + ": white king is in check.");
      } else {
        out.println("Game #" + count + ": no king is in check.");
      }
      count++;
    }

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

  private static boolean inCheck(Point king, char[][] grid) {
    // Death by queen has been incorporated into deathByRook and deathByBishop
    if (deathByPawn(king, grid) || deathByKnight(king, grid) || deathByRook(king, grid) || deathByBishop(king, grid))
      return true;
    return false;
  }

  private static boolean deathByBishop(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    char thing2 = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      thing = 'B';
      thing2 = 'Q';
    } else {
      // White King
      thing = 'b';
      thing2 = 'q';
    }

    // NW
    int idx = i - 1;
    int idx2 = j - 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx--;
      idx2--;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    // NE
    idx = i - 1;
    idx2 = j + 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx--;
      idx2++;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    // SW
    idx = i + 1;
    idx2 = j - 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx++;
      idx2--;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    // RIGHT
    idx = i + 1;
    idx2 = j + 1;
    while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') {
      idx++;
      idx2++;
    }
    if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2))
      return true;

    return false;
  }

  private static boolean deathByRook(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    char thing2 = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      thing = 'R';
      thing2 = 'Q';
    } else {
      // White King
      thing = 'r';
      thing2 = 'q';
    }

    // UP
    int idx = i - 1;
    while (idx >= 0 && grid[idx][j] == '.') {
      idx--;
    }
    if (idx >= 0 && idx < 8 && (grid[idx][j] == thing || grid[idx][j] == thing2))
      return true;

    // DOWN
    idx = i + 1;
    while (idx < 8 && grid[idx][j] == '.') {
      idx++;
    }
    if (idx >= 0 && idx < 8 && (grid[idx][j] == thing || grid[idx][j] == thing2))
      return true;

    // LEFT
    idx = j - 1;
    while (idx >= 0 && grid[i][idx] == '.') {
      idx--;
    }
    if (idx >= 0 && idx < 8 && (grid[i][idx] == thing || grid[i][idx] == thing2))
      return true;

    // RIGHT
    idx = j + 1;
    while (idx < 8 && grid[i][idx] == '.') {
      idx++;
    }
    if (idx >= 0 && idx < 8 && (grid[i][idx] == thing || grid[i][idx] == thing2))
      return true;

    return false;
  }

  private static boolean deathByKnight(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      thing = 'N';
    } else {
      // White King
      thing = 'n';
    }
    if (i + 2 < 8) {
      if (j - 1 >= 0 && grid[i + 2][j - 1] == thing)
        return true;
      if (j + 1 < 8 && grid[i + 2][j + 1] == thing)
        return true;
    }
    if (i - 2 >= 0) {
      if (j - 1 >= 0 && grid[i - 2][j - 1] == thing)
        return true;
      if (j + 1 < 8 && grid[i - 2][j + 1] == thing)
        return true;
    }
    if (j + 2 < 8) {
      if (i - 1 >= 0 && grid[i - 1][j + 2] == thing)
        return true;
      if (i + 1 < 8 && grid[i + 1][j + 2] == thing)
        return true;
    }
    if (j - 2 >= 0) {
      if (i - 1 >= 0 && grid[i - 1][j - 2] == thing)
        return true;
      if (i + 1 < 8 && grid[i + 1][j - 2] == thing)
        return true;
    }

    return false;
  }

  private static boolean deathByPawn(Point king, char[][] grid) {
    int i = king.x;
    int j = king.y;
    char thing = '\u000F';
    if (Character.isLowerCase(grid[i][j])) {
      // Black King
      i++;
      thing = 'P';
    } else {
      // White King
      i--;
      thing = 'p';
    }
    if (i >= 0 && i < 8) {
      if (j - 1 >= 0 && grid[i][j - 1] == thing)
        return true;
      if (j + 1 < 8 && grid[i][j + 1] == thing)
        return true;
    }
    return false;
  }
}

UVA 462 – Bridge Hand Evaluator

Just an implementation problem. Pretty lengthy though.

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

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

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

    while (sc.hasNextLine()) {
      String line = sc.nextLine();
      if (line == null)
        break;
      if (line.equals(""))
        continue;

      StringTokenizer st = new StringTokenizer(line);

      int sum = 0;
      Card[] hand = new Card[13];
      // Step 1
      for (int i = 0; i < 13; i++) {
        hand[i] = new Card(st.nextToken());
        sum += gameCardValue(hand[i]);
      }

      // Step 2
      for (int i = 0; i < 13; i++) {
        if (hand[i].isFaceCard() && hand[i].card.charAt(0) == 'K') {
          char suite = hand[i].card.charAt(1);
          boolean found = false;
          for (int j = 0; j < 13; j++) {
            if (j == i)
              continue;
            if (hand[j].card.charAt(1) == suite) {
              found = true;
              break;
            }
          }
          if (!found)
            sum--;
        }
      }

      // Step 3
      for (int i = 0; i < 13; i++) {
        if (hand[i].isFaceCard() && hand[i].card.charAt(0) == 'Q') {
          char suite = hand[i].card.charAt(1);
          int count = 0;
          for (int j = 0; j < 13; j++) {
            if (j == i)
              continue;
            if (hand[j].card.charAt(1) == suite) {
              count++;
            }
          }
          if (count < 2)
            sum--;
        }
      }

      // Step 4
      for (int i = 0; i < 13; i++) {
        if (hand[i].isFaceCard() && hand[i].card.charAt(0) == 'J') {
          char suite = hand[i].card.charAt(1);
          int count = 0;
          for (int j = 0; j < 13; j++) {
            if (j == i)
              continue;
            if (hand[j].card.charAt(1) == suite) {
              count++;
            }
          }
          if (count < 3)
            sum--;
        }
      }

      // Step 5, 6, 7
      int sum567 = 0;
      int[] suiteCount = new int[4];
      for (int i = 0; i < 13; i++) {
        char suite = hand[i].card.charAt(1);
        switch (suite) {
          case 'S':
            suiteCount[0]++;
            break;
          case 'H':
            suiteCount[1]++;
            break;
          case 'D':
            suiteCount[2]++;
            break;
          case 'C':
            suiteCount[3]++;
            break;
        }
      }
      for (int i = 0; i < 4; i++) {
        if (suiteCount[i] == 2) {
          sum567++;
        } else if (suiteCount[i] == 1) {
          sum567 += 2;
        } else if (suiteCount[i] == 0) {
          sum567 += 2;
        }
      }

      // IsStopped
      boolean[] isStopped = new boolean[4];
      boolean[][] specialCards = new boolean[4][3];
      for (int i = 0; i < 13; i++) {
        char suite = hand[i].card.charAt(1);
        switch (suite) {
          case 'S':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[0][0] = true;
              isStopped[0] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[0][1] = true;
              if (suiteCount[0] > 1)
                isStopped[0] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              if (suiteCount[0] > 2)
                isStopped[0] = true;
              specialCards[0][2] = true;
            }
            break;
          case 'H':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[1][0] = true;
              isStopped[1] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[1][1] = true;
              if (suiteCount[1] > 1)
                isStopped[1] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              if (suiteCount[1] > 2)
                isStopped[1] = true;
              specialCards[1][2] = true;
            }
            break;
          case 'D':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[2][0] = true;
              isStopped[2] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[2][1] = true;
              if (suiteCount[2] > 1)
                isStopped[2] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              specialCards[2][2] = true;
              if (suiteCount[2] > 2)
                isStopped[2] = true;
            }
            break;
          case 'C':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[3][0] = true;
              isStopped[3] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[3][1] = true;
              if (suiteCount[3] > 1)
                isStopped[3] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              specialCards[3][2] = true;
              if (suiteCount[3] > 2)
                isStopped[3] = true;
            }
            break;
        }
      }

      if (sum + sum567 < 14) {
        out.println("PASS");
      } else if (sum + sum567 > 13) {
        if (sum > 15 && isStopped[0] && isStopped[1] && isStopped[2] && isStopped[3]) {
          out.println("BID NO-TRUMP");
        } else {
          int max = -1;
          int maxIndex = -1;
          for (int i = 0; i < 4; i++) {
            if (suiteCount[i] > max) {
              max = suiteCount[i];
              maxIndex = i;
            }
          }
          out.print("BID ");
          switch (maxIndex) {
            case 0:
              out.println("S");
              break;
            case 1:
              out.println("H");
              break;
            case 2:
              out.println("D");
              break;
            case 3:
              out.println("C");
              break;
          }
        }
      }

    }

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

  private static int gameCardValue(Card c) {
    if (c.getCardValue() == 1)
      return 4;
    else if (c.getCardValue() == 13)
      return 3;
    else if (c.getCardValue() == 12)
      return 2;
    else if (c.getCardValue() == 11)
      return 1;
    else
      return 0;
  }

  private static class Card {
    String card;

    public Card(String card) {
      int test1 = getSuitIdx(card.charAt(1));
      if (test1 == -1)
        return;
      int test2 = getNumIdx(card.charAt(0));
      if (test2 == -1)
        return;
      this.card = card;
    }

    public boolean isFaceCard() {
      return getSuitIdx(card.charAt(1)) != -1;
    }

    public int getCardValue() {
      return getNumIdx(card.charAt(0));
    }

    private int getSuitIdx(char suit) {
      switch (suit) {
        case 'H':
          return 0;
        case 'S':
          return 1;
        case 'D':
          return 2;
        case 'C':
          return 3;
      }
      return -1;
    }

    private int getNumIdx(char num) {
      switch (num) {
        case 'A':
          return 1;
        case 'T':
          return 10;
        case 'J':
          return 11;
        case 'Q':
          return 12;
        case 'K':
          return 13;
        default:
          try {
            int val = Integer.parseInt(num + "");
            if (val > 1 && val < 10)
              return val;
            return -1;
          } catch (Exception e) {
            return -1;
          }
      }
    }
  }
}

UVA 696 – How Many Knights

This is extremely similar to UVA 278 – Chess but the tricky case is when min = 2. If you write it out on paper you will figure out the optimal placing for the knights.

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

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

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

    while (sc.hasNext()) {
      int R = sc.nextInt();
      int C = sc.nextInt();
      if (R == 0 && C == 0) {
        break;
      }
      int max = Math.max(R, C);
      int min = Math.min(R, C);
      if (min == 1) {
        out.print(max);
      } else if (min == 2) {
        if (max < 3) {
          out.print(min * max);
        } else if (max == 3) {
          out.print(4);
        } else {
          int tmp = max / 4;
          int rem = max % 4;
          out.print(tmp * min * 2 + Math.min(rem, 2) * 2);
        }
      } else {
        int a = max / 2;
        int b = (max - a);
        int c = min / 2;
        int d = (min - c);
        out.print(Math.max(a * c + b * d, a * d + b * c));
      }
      out.println(" knights may be placed on a " + R + " row " + C + " column board.");
    }

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