278 - Chess

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan

Post by FlyDeath » Fri Jan 04, 2002 5:39 pm

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

Post by htl » Wed Jul 03, 2002 6:22 am

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:

Post by Picard » Wed Jul 03, 2002 10:12 am

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

Post by htl » Wed Jul 03, 2002 12:06 pm

I finally got accepted, thanks.

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

278 - Chess

Post by Shantanu » Tue Dec 02, 2003 6:45 am

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

Thanks in advanced

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

Post by sohel » Tue Dec 02, 2003 7:05 am

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.
:wink:

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

Post by Shantanu » Thu Dec 04, 2003 8:06 pm

I used this algorithm. But still WA. I think the Knigths causing me the problem. Can anyone help me with some input.

Thanks for the reply

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

Post by sohel » Sat Dec 06, 2003 7:32 am

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.
:wink:

Minilek
Learning poster
Posts: 90
Joined: Tue Jul 27, 2004 9:34 am
Location: Cambridge, MA
Contact:

Post by Minilek » Wed Aug 04, 2004 7:23 am

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?

[Edit] 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

Post by serendipity » Sat Jul 01, 2006 1:37 pm

#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;
}

User avatar
Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra » Mon Dec 04, 2006 5:18 pm

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

Post by Uttam Dwivedi » Thu Mar 22, 2007 2:33 pm

the minimum possible size of the board is 4*4...

jbernadas
New poster
Posts: 16
Joined: Tue Apr 24, 2007 11:23 pm
Location: Caracas, Venezuela

Post by jbernadas » Thu Aug 16, 2007 2:03 am

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

Post by tasnif » Sun Apr 05, 2009 8:30 pm

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
Location: University Of Dhaka,Bangladesh
Contact:

Re: 278 Chess

Post by Shafaet_du » Fri Oct 22, 2010 12:33 pm

This will certainly help you:

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!!

Post Reply

Return to “Volume 2 (200-299)”