Translate

Views

Friday, June 2, 2023

Solution UVA: 278 - Chess

 Click on table to read problem, discuss, debug !

 Last Submissions
  Problem  VerdictLangTimeBestRankSubmit Time
 | discuss278 - Chess AcceptedC++110.0000.000298910 mins ago

Suggest:

Rook : it's easy to see that the best way to place pieces is on the diagonal

Queen: by relying on the classic problem that the problem mentioned, we can google it and find out that although the way is different, the result is still the same as the rook.

King: just placing the pieces one square apart is optimal

Knight: this is the most troublesome piece, after a while of trying several cases I finally realized the optimal way to place it is to place it alternately as shown below



Code:

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

int t, n, m;
char k;

int knight(int n, int m){
    if (n < m) swap(n, m);
    if (n%2)
        return (n/2+n%2)*(m/2+m%2) + (n/2)*(m/2);
    return n/2*m;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
   
    cin >> t;
    while(t--){
        cin >> k >> n >> m;
       
        if (k=='r' || k=='Q')
            cout << min(n, m);
       
        else
        if (k=='K')
            cout << (m/2+m%2)*(n/2+n%2);
       
        else
            cout<< knight(n, m);
        cout<<"\n";
    }
}





No comments: