Friday 25 March 2016

Solution of LIGHT OJ 1010 :: Knights in Chessboard


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int tst, i;
    int m , n, mn, ans, x;
    scanf("%d", &tst);
    for(i=1; i<=tst; i++)
    {
        scanf("%d%d", &m, &n);
        mn = m * n;
        if(m==1 || n==1)    ans = mn;
        else if(m==2 || n==2)
        {
            if(m==2) x = n;
            else     x = m;
            if(x%4 == 1)      ans = x+1;
            else if(x%4 == 2) ans = x+2;
            else if(x%4 == 3) ans = x+1;
            else if(x%4 == 0) ans = x;
        }
        else if(mn%2 == 0) ans = mn/2;
        else ans = (mn/2)+1;
        printf("Case %d: %d\n", i, ans);
    }
    return 0;
}

7 comments:

  1. can u tell the logic of this code plzz??

    ReplyDelete
  2. #include
    #include

    int main()
    {

    int i , j,m,n,l,p,t,r;
    scanf("%d",&i);
    for(j = 0;j<i; j++){
    scanf("%d %d",&m,&n);
    l = (n+1)/2;
    p = l*m;
    if(n%2==0){
    t = p;
    }
    else{
    r = m/2;
    t = p - r;
    }

    printf("Case %d: %d\n",j+1,t);
    }
    return 0;
    }


    whats wrong in this

    ReplyDelete
  3. Can you explain what's happen when n or m is 2.Please explain this case.

    ReplyDelete
    Replies
    1. mainly it will make a circle of n=2 radius which makes at least m space for king vice versa then if m%4==1 then space++,if m%4==2 space+=2,otherwise if m%4==3 space++;as if m&&n!=2 then space will be m*n/2

      Delete
  4. If u please explain the logic, it will highly helpful for us.

    ReplyDelete
    Replies
    1. mainly it will make a circle of n=2 radius which makes at least m space for king vice versa then if m%4==1 then space++,if m%4==2 space+=2,otherwise if m%4==3 space++;as if m&&n!=2 then space will be m*n/2

      Delete

Note: only a member of this blog may post a comment.