## 278 - Chess

Moderator: Board moderators

FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan
I don't know what exactly the solution is , but this is what I thought:

n,m are integers

Rook:the smaller of n,m
Knight: n*m/2
Queen: the smaller of n,m
King: (n/2)*(m*2)

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

### ACM 278

I don't know why this code can't get accepted. It always gets WA. But I think the algorithm isn't wrong.Could someone find out the bug?
[c]
#include<stdio.h>
void main(void)
{
int i,x,m,n;
char chess;
scanf("%d",&i);
for(x=0;x<i;x++)
{
fflush(stdin);
scanf("%c %d %d",&chess,&m,&n);
if(chess=='r')
{
if(m>=n)
printf("%d\n",n);
else
printf("%d\n",m);
}
if(chess=='k')
{
if(m*n%2==1)
printf("%d\n",(m*n+1)/2);
else
printf("%d\n",m*n/2);
}
if(chess=='Q')
{
if(m>=n)
printf("%d\n",n);
else
printf("%d\n",m);
}
if(chess=='K')
{
if(m%2==1)
m=(m+1)/2;
else
m/=2;
if(n%2==1)
n=(n+1)/2;
else
n/=2;
printf("%d\n",m*n);
}
}
}
[/c]

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:
put a space in scanf's formating before reading %c, or else it will read white spaces too.

btw:
if (a%2==1) b=(a+1)/2; else b=a/2
has the same result as only:
b=(a+1)/2

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan
I finally got accepted, thanks.

Shantanu
New poster
Posts: 8
Joined: Sun Oct 20, 2002 6:44 am

### 278 - Chess

Can anyone give me some critical input. I am trying this quite a some time. I am getting WA.

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York
For rook and queen it is the minimum of ( row, column).

For knight - picture the board as a grid consisting of black and white colours and count the number of black or white squares.

For king - do the same thing as knight but skip the alternate row.

Hope it helps.

Shantanu
New poster
Posts: 8
Joined: Sun Oct 20, 2002 6:44 am
I used this algorithm. But still WA. I think the Knigths causing me the problem. Can anyone help me with some input.

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York
HI Shantanu,

Here is some input that might come to a help;

k 5 5
k 4 4
k 5 4
k 4 5

output:

13
8
10
10

Hope it helps.

Minilek
Learning poster
Posts: 90
Joined: Tue Jul 27, 2004 9:34 am
Location: Cambridge, MA
Contact:
sohel wrote: For rook and queen it is the minimum of ( row, column).
I don't understand..say the board is 2 by 2. Then how can you possibly put 2 queens on the board so that they don't attack each other? Should the answer here not be 1?

 I am foolish..I didn't realize the problem constraints say that m and n are both greater than or equal to 4[/Edit]

serendipity
New poster
Posts: 6
Joined: Tue May 09, 2006 9:22 pm

### 278-CE

#include<stdio.h>
#include<conio.h>

int find_min(int p,int q)
{
if(p>q)
return q;
else
return p;

}

int main(void)
{
long cs;
int m, n;
char chpcs;

scanf("%ld", &cs);

while(cs)
{
chpcs=getch();
scanf("%d %d",&m,&n);

if(chpcs=='r'||chpcs=='Q')
printf("%d\n", find_min(m,n));

else if(chpcs=='k')
printf("%d\n",m*n/2);

else
printf("%d\n",( ((m+1)/2) * ((n+1)/2) ) );

cs--;

}
return 0;
}

Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:
just solve this problem without using conio.h and getch().

I think you will be able to get AC after this

And remove your code after AC
Akash chhoyar swopno
Dream to touch the sky

Uttam Dwivedi
New poster
Posts: 5
Joined: Fri Jun 30, 2006 10:02 pm
the minimum possible size of the board is 4*4...

New poster
Posts: 16
Joined: Tue Apr 24, 2007 11:23 pm
Location: Caracas, Venezuela
I used differents formulas for the King and the Knight.

tasnif
New poster
Posts: 3
Joined: Fri Apr 03, 2009 5:53 am

### Re: 278 Chess

the following code is giving wrong answer what is the problem?
#include<iostream>
using namespace std;
int main(void)
{
int testno,testcase=1,product,m,n;
char a;
cin>>testno;
while(testcase<=testno)
{

cin>>a>>m>>n;

product=1;
if(a=='r'||a=='q')
{
if(m<=n)cout<<m<<'\n';
else cout<<n<<'\n';
}
else if(a=='k')
{
if((m*n)%2==0)cout<<(m*n)/2<<'\n';
else cout<<(m*n)/2+1<<'\n';
}
else if(a=='K')
{
if((m%2)==0 )product*=(m/2);
else product*=(m/2)+1;
if((n%2)==0)product*=(n/2);
else product*=(n/2)+1;
cout<<product<<'\n';

}
testcase++;
}
return 0;
}

Shafaet_du
Experienced poster
Posts: 147
Joined: Mon Jun 07, 2010 11:43 am
Contact:

### Re: 278 Chess

Code: Select all

``````24
K 8 10
K 7 9
K 7 7
K 8 8
K 6 10
K 5 10

Q 8 10
Q 7 9
Q 7 7
Q 8 8
Q 6 10
Q 5 10

k 8 10
k 7 9
k 7 7
k 8 8
k 6 10
k 5 10

r 8 10
r 7 9
r 7 7
r 8 8
r 6 10
r 5 10
``````
output:

Code: Select all

``````20
20
16
16
15
15
8
7
7
8
6
5
40
32
25
32
30
25
8
7
7
8
6
5
``````
happy programming!!