UVA 278 – Chess

Pretty easy problem. Kings, Queens and Rooks are easy; with Knights you need to realize that knigns basically attack boxes of the opposite color. Once you figure that out, you are golden.

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

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

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

    int N = sc.nextInt();
    for (int zz = 0; zz < N; zz++) {
      String type = sc.next();
      int R = sc.nextInt();
      int C = sc.nextInt();
      int min = Math.min(R, C);
      int max = Math.max(R, C);
      if (type.equals("r") || type.equals("Q")) {
        out.println(min);
      } else if (type.equals("k")) {
        int a = max / 2;
        int b = (max - a);
        int c = min / 2;
        int d = (min - c);
        out.println(Math.max(a * c + b * d, a * d + b * c));
      } else if (type.equals("K")) {
        out.println(((max + 1) / 2) * ((min + 1) / 2));
      }
    }

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

3 thoughts on “UVA 278 – Chess”

  1. How do you convince yourself that the Knight solution is Optimum.
    I mean how do you figured out that was the optimal way to distribute the Knights.
    Thanks!

  2. Hey Man, Sorry for the late response. Effectively for an 8×8 board this works well and is easy to justify if you draw it all out. You can place knights on all black squares or on all white squares. :) This problem is particularly easy as m and n >= 4 which removes the tricky cases.

Leave a Reply

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