Page **2** of **2**

### 696

Posted: **Sun Jun 18, 2006 12:28 pm**

by **sakhassan**

Can any1 tell me whats wrong with this code? Why WA??

#include<stdio.h>

#include<math.h>

int main()

{

long int m,n,num;

while(1)

{

scanf("%ld%ld",&m,&n);

if(m==0 && n==0)

break;

//if(m==0)

// continue;

//if(n==0)

// continue;

if(m==1 || n==1)

{

if(m<n)

num=n;

else

num=m;

}

else if(m>=3&&n>=3)

num=((m*n)+1)/2;

else if(m==2 || n==2)

{

if(m<n)

num=2*(n/2 + 1);

else

num=2*(m/2 + 1);

}

if(num>1)

printf("%ld knights may be placed on a %ld row %ld column board.\n",num,m,n);

else

printf("%ld knight may be placed on a %ld row %ld column board.\n",num,m,n);

}

return 0;

}

the out put 0f 2*100 board = 102 pieces isnt it true??

Posted: **Tue Jun 20, 2006 12:03 pm**

by **putput**

hi, as far as i can tell your M or N = 2 part is wrong

else if(m==2 || n==2)

{

if(m<n)

num=2*(n/2 + 1);

else

num=2*(m/2 + 1);

}

let's use some easy samples

2x2

2x4

2 x 2

num = 2*(2/2 +1) = 4 this one is correct!

2 x 3

num = 2*(4/2 +1) = 6 <-- WRONG!

point is in a 2 x X board you can place 4 knights every 4 layers

it's something like this

- - <-- from this point we are be able to start inserting more

x x the - are free spaces

x x if you place a K in any of the x spaces in this sample

K K one of the first K will kill it

K K

### Please help, getting WA!

Posted: **Thu Aug 09, 2007 6:15 am**

by **rMegaS**

Here's my code, please help!

#include <stdio.h>

int max(int a, int b) {

return a>b? a:b;

}

int mid(int p) {

int d = p/2;

return max(d, p-d);

}

int main() {

while (true) {

int n,m,l;

scanf("%d%d", &n, &m);

if (n==0 && m==0)

break;

l=max(n,m);

int r = mid(n*m);

if (n>0)

r=max(r,m);

if (m>0)

r=max(r,n);

if (n>=2 && m>=2) {

int t=(l/4) * 4;

if (l%4==1)

t+=2;

if (l%4>1)

t+=4;

r=max(r,t);

}

printf("%d knight%s may be placed on a %d row %d column board.\n",r, ((r==1)?"":"s"), n,m);

}

return 0;

}

### I've fixed it

Posted: **Thu Aug 09, 2007 6:41 am**

by **rMegaS**

Finally found a bug:

there should be 'knights' string all the time, even if result is 1

### Re: 696 - How Many Knights

Posted: **Thu Jun 30, 2011 11:02 pm**

by **plamplam**

Any hints? Hell yeah I got a hint for ya. You can always draw a m x n chessboard in your notebook(or whatever) and try to find out the cases. Say draw a 2 x 7 chessboard and think what can be the optimal arrangement. And one more hint: If you place a knight on a standard 8 x 8 chessboard, it will attack only squares of the opposite color. Say a knight is placed on a dark square, then it will only attack light squares and vice versa. This works for any values of m x n, however consider the case when m = 2 and n = 2, i.e, a 2 x 2 board. Here, wherever you place a knight, it doesn't actually attack any of the squares inside the board(if there were imaginary squares outside the board, it would attack imaginary squares of opposite color as mentioned above). So here, the maximum number of knights is 4. There are special cases like this in the problem. Before you read other posts here, try to find it on your own.

### Re: 696 - How Many Knights

Posted: **Thu Jul 19, 2012 10:16 pm**

by **PromeNabid**

@plamplam

yeah, thats the big hint indeed, draw it down into notebook and it worked really great.

### Re: 696 - How Many Knights

Posted: **Thu Jul 19, 2012 11:30 pm**

by **kier.guevara**

I've tried every test cases. I don't know what is wrong with my code..

Can you guys help me on this?

### Re: 696 - How Many Knights

Posted: **Thu Jul 19, 2012 11:54 pm**

by **brianfry713**

2x100 AC output 100.

### Re: 696 - How Many Knights

Posted: **Fri Jul 20, 2012 9:36 am**

by **kier.guevara**

I fixed it and my output for 2 100 is 100 knights.

here is my code:

It is still WA.

### Re: 696 - How Many Knights

Posted: **Fri Jul 20, 2012 9:49 pm**

by **brianfry713**

tempCounter should be initialized inside the while loop.

### Re: 696 - How Many Knights

Posted: **Sat Jul 21, 2012 8:12 am**

by **kier.guevara**

Thank you so much! i got AC now!

### Run time error

Posted: **Sun Apr 28, 2013 11:13 am**

by **csegura**

Hi,

I am obtaining Run Time Error in a very simple program for 696. The program is:

#include <iostream>

using namespace std;

int main(){

while(true){

int rows, columns;

cin >> rows >> columns;

int greater = max(rows, columns);

int lower = min(rows, columns);

if ((rows == 0) && (columns == 0)){

break;

}

int solution;

if (lower <= 0){

solution = 0;

} else if (lower == 1){

solution = greater;

} else if (lower == 2){

int blocks = greater / 4;

int remaining = min((greater % 4), 2);

solution = blocks * 4 + remaining * 2;

} else {

solution = (rows * columns + 1) / 2;

}

cout << solution << " knights may be placed on a " << rows << " row " << columns << " column board." << endl;

}

}

Can anyone help me?

Thank you in advance.

### Re: Run time error

Posted: **Fri May 17, 2013 3:56 am**

by **brianfry713**

That is AC code.

### Re: Run time error

Posted: **Fri May 17, 2013 4:09 am**

by **csegura**

Yes, there was a general error in the system some weeks ago, while I was sending the solution.

Thank you for your response.

### Re: 696 - How Many Knights

Posted: **Thu Feb 27, 2014 3:37 pm**

by **uDebug**

First, I found it useful to do Problem #278 (Chess) before solving this one.

Next, Here's some input / output that I found useful during testing / debugging.

Input:

Code: Select all

```
1 1
1 0
0 1
2 1
1 2
2 2
2 3
3 2
500 500
45 6
7 9
4 10
10 11
2 5
2 283
484 2
123 2
2 500
13 24
2 100
100 2
2 0
0 2
1 6
1 10
10 1
0 0
```

AC Output:

Code: Select all

```
1 knights may be placed on a 1 row 1 column board.
0 knights may be placed on a 1 row 0 column board.
0 knights may be placed on a 0 row 1 column board.
2 knights may be placed on a 2 row 1 column board.
2 knights may be placed on a 1 row 2 column board.
4 knights may be placed on a 2 row 2 column board.
4 knights may be placed on a 2 row 3 column board.
4 knights may be placed on a 3 row 2 column board.
125000 knights may be placed on a 500 row 500 column board.
135 knights may be placed on a 45 row 6 column board.
32 knights may be placed on a 7 row 9 column board.
20 knights may be placed on a 4 row 10 column board.
55 knights may be placed on a 10 row 11 column board.
6 knights may be placed on a 2 row 5 column board.
284 knights may be placed on a 2 row 283 column board.
484 knights may be placed on a 484 row 2 column board.
124 knights may be placed on a 123 row 2 column board.
500 knights may be placed on a 2 row 500 column board.
156 knights may be placed on a 13 row 24 column board.
100 knights may be placed on a 2 row 100 column board.
100 knights may be placed on a 100 row 2 column board.
0 knights may be placed on a 2 row 0 column board.
0 knights may be placed on a 0 row 2 column board.
6 knights may be placed on a 1 row 6 column board.
10 knights may be placed on a 1 row 10 column board.
10 knights may be placed on a 10 row 1 column board.
```