Search Here

Wednesday, June 10, 2015

UVa - 696 ( How Many Knights Solution ) C Code

Try Yourself First. For help Scroll Down .

Tip : A same problem is present at LightOJ . You can find it there . To find the solution you have to consider different cases. For [ M = 1 or N = 1 ], [ M = 2 or N = 2 ], and for All others . Find out the Formula . If you Can't find it yet then Google it . Even if you won't find it the see ,

For[ M = 1 or N = 1] Ans is M*N. 

For [ M = 2 or N = 2 ] 

If M = 2 then k=N. If not k=M. Then  C = (k%4==0)?k/4:(k/4)+1 and 

D = ((k-1)%4==0)?(k-1)/4:((k-1)/4)+1.   Ans is 2*(C+D). 

For Others Ans is ((m*n)%2==0)?(m*n)/2:((m*n)/2)+1.



Code :

#include<stdio.h>
#include<math.h>

int main()
{
    int m, n, t, j=0, ans, k, c, d;

    while(scanf("%d %d", &m, &n)==2)
    {
        if(m==0 && n==0) break;
        if(m==1 || n==1)
            ans = m*n;
        else if(m==2 || n==2)
        {
            if(m==2)
                k=n;
            else
                k=m;
            c = (k%4==0)?k/4:(k/4)+1;
            d = ((k-1)%4==0)?(k-1)/4:((k-1)/4)+1;
            ans= 2*(c+d);
        }
        else
            ans = ((m*n)%2==0)?(m*n)/2:((m*n)/2)+1;
        printf("%d knights may be placed on a %d row %d column board.\n", ans, m, n);
    }
    return 0;
}


=> Need Help . Leave a Comment .

No comments:

Post a Comment