# 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 &lt; 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(&quot;r&quot;) || type.equals(&quot;Q&quot;)) {
out.println(min);
} else if (type.equals(&quot;k&quot;)) {
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(&quot;K&quot;)) {
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.

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