22 November 2015

Lightoj 1010-Knights in Chessboard

Though it is simple but has a twist when any dimension of the board is 2.Let a & b be the dimension of the board. It will be better if you 1st draw some board where a=2 and  b may be an odd or even number greater than or equal to a .  After that just calculate the number of knight with the condition which has been given in problem. If a!=2 & b!=2 then the calculation is quite easy.Just multiply a&b and take their ceil. 

/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

int main()
{
    int test ;
    sc("%d",&test) ;
    int cnt =1 ;
    while(test--)
    {
        int a,b ;
        sc("%d %d",&a,&b) ;
        if(a>b)
            swap(a,b) ;
        int x =0 ;
        if(a==1 ||b==1)
          x = a*b ;

          else if(a==2 ||b==2)
          {
              int z =0 ;
              if(a==b)
                x = a*b ;
              else if(b%2==1)
              {
                  x = b+1 ;
              }
              else if(b%2==0)
                {
                    int z =0 ;
                    z = b/2 ;
                    if(z%2==0)
                    {
                       x = (a*b)/2 ;
                    }
                    else
                        x = a+b ;
                }

          }
          else
            x = ((a*b)+1)/2 ;
          pf("Case %d: %d\n",cnt++,x) ;

    }
    return 0;
}

2 comments:

  1. #include

    int main() {
    int tst, i, mn,n,m,ans;
    scanf("%d",&tst);
    for (i=1;i<=tst;i++)
    {
    scanf("%d %d", &m, &n);
    mn=m*n;
    ans=mn;
    if (mn%2==0) ans=mn/2;
    else
    ans=(mn/2)+1;

    printf ("case %d:%d\n",i,ans);
    }
    return 0;
    }

    ReplyDelete
  2. Easiest Solution for this problem:

    int solve(int p){
    if(p % 4 == 1) return p + 1;
    if(p % 4 == 2) return p + 2;
    else return solve(p - 1);
    }

    int main(){

    setIO();

    int j = 1;

    Tcase(){

    cout << "Case " << j++ << ": ";

    int a, b;
    cin >> a >> b;

    if(a == 1) cout << b << "\n";
    else if(b == 1) cout << a << "\n";
    else if(a == 2) cout << solve(b) << "\n";
    else if(b == 2) cout << solve(a) << "\n";
    else cout << (a * b + 1) / 2 << "\n";
    }
    }

    ReplyDelete

UVA 10679 - I Love Strings!!