lightoj 1010 - Knights in Chessboard




Problem link

This problem says Given an m x n chessboard and finds the number of maximum knights in the chessboard that no two knights attack each other

Answer: This answer is easy but row or column is 2  or 1 then we can need to use one logic otherwise our answer is ( row * column ) / 2, but the answer is odd then we need to increase 1. because this odd position we can add one knight in this chessboard. otherwise, our answer is row*column. 

Special test case 1:
Now we talk about the special case in this problem if our row or column is 1 then we set max number or knight in our chessboard Example: row = 1 and column = 5, so we can set 5 knights in this chessboard or  row = 5 and column = 1 so we can set 5 knights in this chessboard.

Special test case 2:
Now we talk about the special case in this problem if our row or column is 2 then we set 
(knight = king, sorry for mistake)



we set 2*1 chessboard 2 knights

we set 2*2 chessboard 4 knights

we set 2*3 chessboard 4 knights because of 1*3 and 2*3 number index attack in 1*1 and 2*1 knights

we set 2*4 chessboard 4 knights because of 1*3 and 2*3 number index attack in 1*1 and 2*1 knights and 1*4 and 2*4 number index attack in 1*2 and 2*2 knights

we set 2*5 chessboard 6 knights because of 1*3 and 2*3 number index attack in 1*1 and 2*1 knights and 1*4 and 2*4 number index attack in 1*2 and 2*2 knights

we set 2*6 chessboard 8 knights because of 1*3 and 2*3 number index attack in 1*1 and 2*1 knights and 1*4 and 2*4 number index attack in 1*2 and 2*2 knights

we set 2*7 chessboard 8 knights because of 1*3 and 2*3 number index attack in 1*1 and 2*1 knights and 1*4 and 2*4 number index attack in 1*2 and 2*2 knights and 1*7 and 2*7
number index attack in 1*5 and 2*5 knights

we set 2*8 chessboard 8 knights because of 1*3 and 2*3 number index attack in 1*1 and 2*1 knights and 1*4 and 2*4 number index attack in 1*2 and 2*2 knights and1*7 and 2*7 number index attack in 1*5 and 2*5 knights and 1*8 and 2*8 number index attack in 1*6 and 2*6 knights.

Maybe you understand this problem and solution :)

Happy coding guys :)


A solution in c++

#include<bits/stdc++.h>

using namespace std;

/// Typedef
typedef long long ll;

#define sc1(a) scanf("%lld",&a)
#define sc2(a,b) scanf("%lld %lld",&a,&b)

int main()
{
    ll row, col, tc, t = 1;

    sc1(tc);

    while (tc--){
        sc2(row, col);

        cout << "Case " << t++ << ": " ;

        ll ans = row * col;
        if(row == 1 || col == 1){
            cout << max(row, col) << endl;
        }
        else if(row == 2 || col == 2){
            ll big = max(row, col);
            ans = big;
            if(big % 4 == 1 || big % 4 == 3) ans++;
            if(big % 4 == 2 ) ans+=2;
            cout << ans << endl;
        }
        else if(ans %2 == 1){
            cout << ans/2 + 1 << endl;
        }
        else
            cout << ans/2 << endl;
    }
}

Post a Comment

8 Comments

If you have any doubts, Please let me know