Tuesday, 4 July 2017

Uva 278 - Chess

This problem  is one of the best ad-hoc problems I have come across - simple but thought provoking. It asks you to the maximum number of  rooks, queens, kings or knights you could place on n*m chess board such that no piece attack another.
Given that 4<=m,n<=10.



Constraints

The constraints make the job easier else you would have to write specific case do deal with especially when n and m have values below 4.

Code


#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;

int main()
{

    int t;
    cin >> t;
    while (t--) {
        char c;
        int m;
        int n;
        cin >> c >> m >> n;
        int mini = min(m, n);

        if (c == 'Q' || c == 'r')
            cout << mini;
        else if (c == 'K')
            cout << ((n + 1) / 2) * ((m + 1) / 2);
        else
            cout << (m * n + 1) / 2;
        cout << "\n";
    } 
} 

No comments:

Post a Comment