278 - Chess
Moderator: Board moderators
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]
[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]
278 - Chess
Can anyone give me some critical input. I am trying this quite a some time. I am getting WA.
Thanks in advanced
Thanks in advanced
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?sohel wrote: For rook and queen it is the minimum of ( row, column).
[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]
-
- 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;
}
#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;
}
-
- Learning poster
- Posts: 62
- Joined: Sun Jul 09, 2006 8:31 am
- Location: University of Dhaka
- Contact:
-
- New poster
- Posts: 5
- Joined: Fri Jun 30, 2006 10:02 pm
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;
}
#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;
}
-
- Experienced poster
- Posts: 147
- Joined: Mon Jun 07, 2010 11:43 am
- Location: University Of Dhaka,Bangladesh
- Contact:
Re: 278 Chess
This will certainly help you:
output:
happy programming!!
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
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
UVa stats: http://felix-halim.net/uva/hunting.php?id=63448
My blog on programming: http://www.shafaetsplanet.com/planetcoding/
My blog on programming: http://www.shafaetsplanet.com/planetcoding/