Page 2 of 2

Re: 278 Chess

Posted: Wed Jun 29, 2011 7:48 pm
by plamplam
Nice problem :D The knights part is quite tricky though. Shafaet DU....thanks a lot for the test cases :).....the Knights caught me off-guard hehe :wink:

Re: 278 Chess

Posted: Thu Feb 27, 2014 1:55 pm
by uDebug
Shafaet_du wrote:This will certainly help you:
Thanks for sharing these test cases.

Re: 278 Chess

Posted: Sun May 18, 2014 3:49 am
by facug91
Hi! I have a problem with the code in the input part.

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <iterator>
#include <utility>
#include <list>
#include <stack>
#include <iomanip>
#include <bitset>

using namespace std;

int t, m, n, ans;
char piece;

int main() {
	
	scanf("%d", &t);
	
	while (t--) {
		
		scanf("%c %d %d", &piece, &m, &n);
		
		/* code to solve the problem */
		
	}
	
	return 0;
} 
I've had AC using 'cin' instead of 'scanf', but I don't know what the problem is there. When I execute it, it's like the first time it doesn't read anything, so it finishes in less turns than it should.
As I sad, I've already had AC with 'cin', but I would be glad to know why it doesn't work with 'scanf'.
Thanks! :)

----------------------------------------------------------------------------------

Well, I've already found why. 'scanf' ignore white spaces, but evidently not newlines, so 'piece' reads the newlines instead the char it should. I solved it putting 'getchar()' after each 'scanf' :lol:

Re: 278 Chess

Posted: Mon May 19, 2014 9:20 am
by uDebug
facug91 wrote:Well, I've already found why. 'scanf' ignore white spaces, but evidently not newlines, so 'piece' reads the newlines instead the char it should. I solved it putting 'getchar()' after each 'scanf'
You're correct in your observation.

What you did for this problem might work but don't count on putting a getchar() at the end of each line to "catch" the newline every time. The better way to do this is to use "\n" in scanf like so:

Code: Select all

int t, m, n, ans;
char piece;
int main() {
    /* The "\n" after the "%d" handles the newline character so it's not left in the input stream */
	scanf("%d\n", &t);
	
	while (t--) {
		 /* The "\n" after the "%d" handles the newline character so it's not left in the input stream */
		scanf("%c %d %d\n", &piece, &m, &n);
		
		/* Code to solve the problem */
	}
	return 0;
} 

278 - Chess

Posted: Mon Jan 19, 2015 3:49 am
by jeffprog
What is wrong at my code?

Code: Select all

#include <stdio.h>

int minimo(int a, int b);

int main(){

	int t;
	char p;
	int m;
	int n;
	int i;

	scanf("%d",&t);

	for(i=0;i<t;i++){

		fflush(stdin);

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

		if(p == 'Q'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'r'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'k'){

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

		if(p == 'K'){

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

	return 0;
}

int minimo(int a, int b){

	if(a < b){

		return a;
	}
	else{

		return b;
	}
}

Re: 278 - Chess

Posted: Mon Jan 19, 2015 9:28 pm
by brianfry713
Next time post in the existing thread.
Don't use: fflush(stdin);

Problem 278 (Chess)

Posted: Wed Jan 21, 2015 3:22 am
by jeffprog
What is wrong at my code? Please Help me...

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int minimo(int a, int b);

int main(){

	int t;
	char p;
	int m;
	int n;
	int i;

	scanf("%d",&t);
	setbuf(stdin, NULL);

	for(i=0;i<t;i++){

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

		setbuf(stdin, NULL);

		if(p == 'Q'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'r'){

			printf("%d\n", minimo(m,n));
		}

		if(p == 'k'){

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

		if(p == 'K'){

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

	return 0;
}

int minimo(int a, int b){

	if(a < b){

		return a;
	}
	else{

		return b;
	}
}

Re: 278 - Chess

Posted: Wed Jan 21, 2015 11:17 pm
by brianfry713
Your code isn't working correctly for the sample input on my machine.
On way to get AC is to remove the lines: setbuf(stdin, NULL);
And change your input parsing to:
scanf("%d\n",&t);
and
scanf("%c %d %d\n", &p, &m, &n);