227 - Puzzle

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

tgoulart
New poster
Posts: 42
Joined: Sat Oct 21, 2006 8:37 am
Location: Alegrete, Brazil

Post by tgoulart » Sun Jan 21, 2007 7:33 am

So the reason of your PE might be a wrong output. For example, if the line that you must print is:

A B _ C D

but you print:

A B C _ D

(change "_" for " ")
it would give you PE! It's just a guess, I can't think about anything else...
Thiago Sonego Goulart - UFMG/Brazil

stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

Post by stcheung » Sun Jan 21, 2007 7:39 am

Thanks for helping. I don't think that could be my mistake. The following is my latest code, which is adapted from yours. board[j] will be ' ' if it's the blank character.

Code: Select all

if(problemnum > 1)
	printf("\n"); 
printf("Puzzle #%d:\n",problemnum);
if(illegal)
    cout << "This puzzle has no final configuration.";
else  {
    for(int i=0; i<5; i++) {
	  printf("%c",board[i][0]); 
      for(int j=1; j<5; j++) {
		printf(" %c",board[i][j]); 
      }
      printf("\n");
    }
}

stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

Post by stcheung » Sun Jan 21, 2007 7:43 am

haha after screening my code after posting it, I realize I need the "\n" here:

Code: Select all

cout << "This puzzle has no final configuration.\n";
Thanks again for your help.

tgoulart
New poster
Posts: 42
Joined: Sat Oct 21, 2006 8:37 am
Location: Alegrete, Brazil

Post by tgoulart » Sun Jan 21, 2007 7:49 am

I was just going to tell you that! :lol:
I hate when it happens...
Thiago Sonego Goulart - UFMG/Brazil

razor_blue
New poster
Posts: 27
Joined: Mon Nov 27, 2006 4:44 am
Location: Indonesia

Post by razor_blue » Tue May 08, 2007 3:31 am

Why WA?? Someone please help me...
Is there any trick here? Let me know... :cry:

Code: Select all

/* Removed after AC */
Thx...
Last edited by razor_blue on Thu May 10, 2007 5:13 am, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Tue May 08, 2007 8:26 pm

'Z' can appear in the board.
Ami ekhono shopno dekhi...
HomePage

razor_blue
New poster
Posts: 27
Joined: Mon Nov 27, 2006 4:44 am
Location: Indonesia

Post by razor_blue » Thu May 10, 2007 5:11 am

Thank you...
I didn't realize that... Slight modification give me AC. :D

chetan
New poster
Posts: 43
Joined: Sun Sep 24, 2006 2:39 pm

Post by chetan » Mon Jul 16, 2007 7:28 pm

why WA ????? :cry: :cry:

Code: Select all


CODE DELETED 

Last edited by chetan on Tue Jul 17, 2007 11:54 am, edited 1 time in total.

chetan
New poster
Posts: 43
Joined: Sun Sep 24, 2006 2:39 pm

Post by chetan » Tue Jul 17, 2007 4:49 am

please give me some test cases

rhsumon
New poster
Posts: 48
Joined: Wed Aug 23, 2006 12:29 pm
Location: Dhaka

WA------------> Plz help

Post by rhsumon » Sat Sep 01, 2007 9:38 pm

Here is my code ------- What is the wrong i couldn't fine

Code: Select all

#include<stdio.h>
#include<string.h>

char mat[10][10],dir[105];
int br,bc;

int valid(int a,int b){
	if(a>=0 && a<5 && b>=0 && b<5)
		return 1;
	return 0;
}

void pos(char ch){
	if(ch == 'A'){
		if(valid(br-1,bc))
			mat[br][bc] = mat[br-1][bc];
		br--;
	}
	else if(ch == 'B'){
		if(valid(br+1,bc))
			mat[br][bc] = mat[br+1][bc];
		br++;
	}
	else if(ch == 'R'){
		if(valid(br,bc+1))
			mat[br][bc] = mat[br][bc+1];
		bc++;
	}
	else if(ch == 'L'){
		if(valid(br,bc-1))
			mat[br][bc] = mat[br][bc-1];
		bc--;
	}
	mat[br][bc] = ' ';
}

int main()
{
	//freopen("227.out","w",stdout);
	int i,j,flag,flg,cas=0,first=1;
	while(gets(mat[0]) != NULL){
		if(strcmp(mat[0],"Z") == 0) break;
		cas++;
		for(i=1; i<5; i++){
			gets(mat[i]);
			for(j=0; j<5; j++){
				if(mat[i][j] == ' '){
					br = i; bc = j;
				}
			}
		}
		while(gets(dir) != NULL){
			flag = flg = 0;
			for(i=0; dir[i]; i++){
				if(dir[i] == '0'){
					flag = 1;
					break;
				}
				pos(dir[i]);
				if(valid(br,bc) == 0){ // If Not valid.......
					flg = 1;
					break;	
				}				
			}
			if(flag == 1 || flg == 1) break;
		}
		if (!first)
			printf("\n");	// Separeted new line......
		printf("Puzzle #%d:\n",cas);
		if(flg == 0){
			for(i=0; i<5; i++){
				for(j=0; j<4; j++)
					printf("%c ",mat[i][j]);
				printf("%c\n",mat[i][j]);
			}
		}
		else printf("This puzzle has no final configuration.\n");
		first = 0;
	}
	return 0;
}

porker2008
New poster
Posts: 21
Joined: Wed Oct 08, 2008 7:04 am

Re: 227 puzzle

Post by porker2008 » Mon Oct 20, 2008 9:58 am

After reading so many suggestions, I got AC now.

However, I found a very serious problem. The first character of the puzzle might be Z !!!

When you use getchar(), you can't know whether there will be any characters following 'Z'.

I think most of you will search the character and if you found it was 'Z', you quit.

That's why some of you get WA.

For the first line, you should read in all the character together. If there's only a character 'Z', then you can quit.
If the fisrt line is "Z A B C D", you should not quit but read the next line.

P.S. I use getline() to get the character. So I need not to worry about the problem shown above.
However, This will take quite a lot time, because I use string to store the characters.
If somebody have other quicker way, please tell me.

MRH
Learning poster
Posts: 51
Joined: Mon Aug 11, 2008 9:09 pm

Re: 227 puzzle

Post by MRH » Thu Jan 29, 2009 11:11 am

PLZ give me some critical Input-output

calicratis19
Learning poster
Posts: 76
Joined: Mon Jul 21, 2008 8:50 am
Location: SUST,SYLHET,BANGLADESH.
Contact:

Re: 227 puzzle

Post by calicratis19 » Sat Feb 14, 2009 2:57 pm

it should be assumed that the input which gives the map of moves can have space in them.so a condition for 0 should be added in the code.i got wa 1st time when i didnt.when i did i get ac. :D
Heal The World

alamgir kabir
New poster
Posts: 37
Joined: Wed Oct 03, 2007 10:42 am

Re: 227 puzzle

Post by alamgir kabir » Fri May 15, 2009 2:48 pm

I do not find any fault in my code. Anyone can help me to find fault. I am really keen to know my fault.
Please give some special input and output that I get my fault.

Code: Select all

#include<stdio.h>
#include<string.h>

int space_i, space_j;
char str [ 1000 ], puzzle [ 6 ][ 6 ];

void find_space()
{
    int i, j;
    for( i = 0; i < 5; i ++ )
    {
        for( j = 0; j < 5; j ++)
        {
            if( puzzle [ i ][ j ] == ' ' )
            {
                space_i = i;
                space_j = j;
            }
        }
    }
    return;
}

void take_pos( int i, int j, int *x, int *y, char c)
{
    if( c == 'A' )
    {
        *x = i - 1;
        *y = j;
    }
    if( c == 'B' )
    {
        *x = i + 1;
        *y = j;
    }
    if( c == 'L' )
    {
        *x = i;
        *y = j - 1;
    }
    if( c == 'R' )
    {
        *x = i;
        *y = j + 1;
    }
    return;
}

bool is_valid_move( int i, int j, char c )
{
    int x, y;
    take_pos(i, j, &x, &y, c);

    if( x >= 0 && x < 5 && y >= 0 && y < 5 )
    {
        puzzle [ i ][ j ] = puzzle [ x ][ y ];
        puzzle [ x ][ y ] = ' ';
        space_i = x;
        space_j = y;
        return true;
    }
    return false;
}

void print_puzzle()
{
    int i, j;
    for( i = 0; i < 5; i ++ )
    {
        for( j = 0; j < 5; j ++ )
        {
            if( j ) printf( " " );
            printf("%c", puzzle [ i ][ j ]);
        }
        printf("\n");
    }
    return;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int i, kase = 1;
    bool final;

    while( gets( str ) )
    {
        if( strcmp(str, "Z") == 0 ) break;
        sscanf(str, "%c %c %c %c %c", &puzzle [ 0 ][ 0 ], &puzzle [ 0 ] [ 1 ], &puzzle [ 0 ] [ 2 ],&puzzle [ 0 ] [ 3 ],&puzzle [ 0 ] [ 4 ]);
        for( i = 1; i < 5; i ++)
        {
            gets( str );
            puzzle [ i ][ 0 ] = str [ 0 ];
            puzzle [ i ] [ 1 ] = str [ 1 ];
            puzzle [ i ] [ 2 ] = str [ 2 ];
            puzzle [ i ] [ 3 ] = str [ 3 ];
            puzzle [ i ] [ 4 ] = str [ 4 ];
        }


        final = true;
        find_space( );


        while( gets( str ) )
        {
            for( i = 0; str [ i ] != '\0' && str [ i ] != '0'; i ++)
            {
                if( final )
                {
                    if( !(str [ i ] == 'A' || str [ i ] == 'B' || str [ i ] == 'R' || str [ i ] == 'L')) final = false;
                    if( final && !is_valid_move(space_i, space_j , str [ i ]) ) final = false;
                }
            }
            if( str [ i ] == '0' ) break;
        }
        if( kase > 1 )printf("\n");
        printf("Puzzle #%d:\n", kase ++);
        if( !final )
        {
            printf("This puzzle has no final configuration.\n");
        }
        else
        {
            print_puzzle();
        }
    }
    return 0;
}
Please help me.
thanks in advance.

Jehad Uddin
Learning poster
Posts: 74
Joined: Fri May 08, 2009 5:16 pm

Re: 227 puzzle

Post by Jehad Uddin » Sat Jul 25, 2009 10:13 am

I got 10 WAs and then got acc, the input is so critical here,change ur input technique and use string class in C++,

Post Reply

Return to “Volume 2 (200-299)”