Page 1 of 4

Posted: Wed Jan 02, 2002 9:25 pm
by Axiom
Hi!
does any one noes the catch with problem 227? the input seems to be VERY tricky!.

any help is greatly appreciated!

Posted: Thu Jan 03, 2002 4:30 pm
by FlyDeath
Kept wrong answer......following is my code,I don't know what's wrong with it


#include <stdio.h>

void main()
{
char puz[5][5];
int i,j;
int sx,sy;
char temp;
int flag;
int cases=0;
int flag2=0;
while(1)
{
cases++;
flag=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
temp=getchar();
if(temp=='Z')
{
flag2=1;
break;
}
if(temp==' ')
{
sy=i;
sx=j;
}
puz[j]=temp;
}
while(1)
{
temp=getchar();
if(temp=='n')
break;
}
if(flag2==1)
break;
}
if(flag2==1)
break;
while(1)
{
temp=getchar();
if(temp=='0')
break;
if(flag==1)
continue;
if(temp=='A')
{
if(sy==0)
{
flag=1;
continue;
}
puz[sy][sx]=puz[sy-1][sx];
puz[sy-1][sx]=' ';
sy--;
}
else if(temp=='B')
{
if(sy==5)
{
flag=1;
continue;
}
puz[sy][sx]=puz[sy+1][sx];
puz[sy+1][sx]=' ';
sy++;
}
else if(temp=='L')
{
if(sx==0)
{
flag=1;
continue;
}
puz[sy][sx]=puz[sy][sx-1];
puz[sy][sx-1]=' ';
sx--;
}
else if(temp=='R')
{
if(sx==5)
{
flag=1;
continue;
}
puz[sy][sx]=puz[sy][sx+1];
puz[sy][sx+1]=' ';
sx++;
}
}
printf("Puzzle #%d:n",cases);
if(flag==1)
printf("This puzzle has no final configuration.n");
else
{
for(i=0;i<5;i++)
{
printf("%c",puz[0]);
for(j=1;j<5;j++)
printf(" %c",puz[j]);
printf("n");
}
}
printf("n");
while(1)
{
temp=getchar();
if(temp=='n')
break;
}
}
}

Posted: Thu May 09, 2002 5:40 pm
by le
I am getting WA too. It must have something to do with the output, where spaces are allowed and where not. Anyone have some sample input and output around, that works and that I can use to test my program?

I think it is not very clear where spaces may be set and where not (if the empty field is the last in the line, should it be printed at all?)
[c]
#include <stdio.h>

#define BUFSIZE 1024
#define MAXFIELDS 25

char puzzle[MAXFIELDS];

int move(char *, int *);
void output(void);

int main()
{
char input[BUFSIZE];
int i,j,space,no,rc;

no = 0;

while ((fgets(input, sizeof(input), stdin)) != NULL)
{
if (input[0] == 'Z' && input[1] == '\n')
break;

if (no > 0)
printf("\n");

no++;
i = space = 0;

do
{
for (j = 0; j < 5; j++)
{
puzzle[i*5+j] = input[j];
if (input[j] == ' ')
space = i*5+j;
}
i++;
} while (i < 5 && fgets(input, sizeof(input), stdin) != NULL);

do
{
fgets(input, sizeof(input), stdin);
rc = move(input, &space);
} while (rc == 0);

printf("Puzzle #%d:\n", no);
if (rc == -1)
{
printf("This puzzle has no final configuration.\n");
}
else
{
output();
}
}

exit(0);
}

int move(char *input, int *space)
{
char h,ch;

for (;*input != '\0'; input++)
{
ch = *input;
switch (ch)
{
case 'A':
if (*space < 5)
return -1;
else
{
h = puzzle[*space-5];
puzzle[*space-5] = ' ';
puzzle[*space] = h;
*space -= 5;
}
break;
case 'B':
if (*space >= 20)
return -1;
else
{
h = puzzle[*space+5];
puzzle[*space+5] = ' ';
puzzle[*space] = h;
*space += 5;
}
break;
case 'L':
if (*space == 0 || *space % 5 == 0)
return -1;
else
{
h = puzzle[*space-1];
puzzle[*space-1] = ' ';
puzzle[*space] = h;
*space -= 1;
}
break;
case 'R':
if (*space % 5 == 4)
return -1;
else
{
h = puzzle[*space+1];
puzzle[*space+1] = ' ';
puzzle[*space] = h;
*space += 1;
}

break;
case '0':
return 1;
break;
case '\n':
return 0;
break;
case '?':
default:
return -1;
}
}

return 0;
}

void output(void)
{
int i;
for (i = 0; i < 25; i++)
{
switch (puzzle[i])
{
case ' ':
printf("%c ", puzzle[i]);
break;
default:
printf("%c", puzzle[i]);
if (i % 5 == 4)
printf("\n");
else if (i % 5 == 3 && puzzle[i+1] == ' ')
{
printf("\n");
i++;
}
else
printf(" ");
break;
}
}
}[/c]

Posted: Wed May 29, 2002 4:29 am
by javipeg
I also had a lot of WA with this. The thingy about this problem is with the input. The input file is in DOS mode!!! (no wonder why) and instead of '\n', the linefeeds are '\r\n'; so you have to mind this when scanning the input.

The output linefeeds must be in UNIX mode.

Posted: Wed May 29, 2002 9:17 am
by Ivor
Can't understand where did you get that "clever" idea.

There's nothing like '\r\n' in the input, only '\n'. You better revise your algorithm before telling this kind of a things. :roll:

Ivor

227 - Puzzle

Posted: Thu Jul 04, 2002 5:31 pm
by htl
Why does this code get Runtime Error?
[c]
#include<stdio.h>
#include<string.h>
#define SWAP(x,y,t) (t=x,x=y,y=t)
#define YES 1
#define NO 0
void main(void)
{
int x,y,count,error,px,py,found;
char puzzle[5][6],c,buffer[5],move[10000];
for(count=1;;count++)
{
fflush(stdin);
scanf("%c",&c);
if(c=='Z')
break;
puzzle[0][0]=c;
for(x=0;(c=getchar())!='\n';x++)
buffer[x]=c;
buffer[x]='\0';
for(x=1;x<5;x++)
puzzle[0][x]=buffer[x-1];
for(x=1;x<5;x++)
{
fflush(stdin);
for(y=0;(c=getchar())!='\n';y++)
buffer[y]=c;
buffer[y]='\0';
if(y==4)
buffer[4]=' ',buffer[5]='\0';
for(y=0;y<5;y++)
puzzle[x][y]=buffer[y];
puzzle[x][y]='\0';
}
fflush(stdin);
for(x=0;(c=getchar())!='0';x++)
move[x]=c;
move[x]='\0';
found=NO;
for(px=0;px<5;px++)
{
for(py=0;py<5;py++)
if(puzzle[px][py]==' ')
{
found=YES;
break;
}
if(found==YES)
break;
}
for(x=0;move[x]!='\0';x++)
{
error=NO;
if(move[x]=='A')
if(px==0)
error=YES;
else
{
SWAP(puzzle[px][py],puzzle[px-1][py],c);
px--;
}
if(move[x]=='B')
if(px==4)
error=YES;
else
{
SWAP(puzzle[px][py],puzzle[px+1][py],c);
px++;
}
if(move[x]=='L')
if(py==0)
error=YES;
else
{
SWAP(puzzle[px][py],puzzle[px][py-1],c);
py--;
}
if(move[x]=='R')
if(py==4)
error=YES;
else
{
SWAP(puzzle[px][py],puzzle[px][py+1],c);
py++;
}
if(error==YES)
break;
}
if(error==YES)
{
printf("Puzzle #%d:\n",count);
printf("This puzzle has no final configuration.\n");
}
else
{
printf("Puzzle #%d:\n",count);
for(x=0;x<5;x++)
for(y=0;y<5;y++)
if(y==4)
printf("%c\n",puzzle[x][y]);
else
printf("%c ",puzzle[x][y]);
}
}
}
[/c]

227 puzzle

Posted: Sun Feb 16, 2003 8:53 pm
by bery olivier
Hi folks,

I wrote a source to solve "the puzzle" and I got WA :(. However I tested it with a lot of differents inputs and a real puzzle and my program always gave me the good answer. I don't have a single clue of what doesn't work.
Is there something I should take care ?
Please let me know what could be wrong.

It also happened to me :-?

Posted: Thu Mar 13, 2003 9:36 pm
by cris_vice
It also happened to me :-? and I'm very disappointed !!
This is my first try and... first fault. I've programmed it using Java and it works for all the examples I've tried, but... when I submitted it to the judge on-line, it said that, after 0,016 seconds trying IT DOESN'T WORK :cry: snif snif... and... I DON'T KNOW WHAT'S WRONG WITH IT... well, I'll go on trying !! If I find an example that makes my code fail I'll let you know just in case it also can help you find your error, ok?
Bye.

scanf() vs gets()

Posted: Fri Mar 14, 2003 3:24 pm
by shahriar_manzoor
When I solved it I used C/C++. The most common problem with this is taking input. With respect to C/C++ my suggestion is don't use gets() and scanf() together. Use gets() and sscanf() pair instead. For example your input taking might struggle when the first character of the puzzle is space
as below:

BXXXX
XXXXX
XXXXX
XXXXX

/*Here B is space*/

Posted: Fri Mar 14, 2003 3:44 pm
by bery olivier
I used C as well. I didn't use gets() but getchar() or scanf("%c"). So I didn't get any trouble with spaces.
Is it really more safety with sscanf ??? What is the difference between (scanf() or getchar()) and sscanf() when we got spaces in the input ?
Thanks for your help.

Posted: Sat Apr 05, 2003 1:24 pm
by djemili
I have the same problem!!

I discovered that when i use getchar() function the program waits for an ENTER, and sometimes after the Z the user doesnt press ENTER. Then, I tried to use the getc() function but the program fails with a Segmentation fault (ups!!). Now i'm using the read function read(0, &c, 1); and it seems to run ok but i'm having the same result, WA.

Posted: Sun Apr 06, 2003 7:40 am
by anupam
i handled the input by another process..
not just take it with scanf..
because when i used scanf i got wa..
but when i take it specially it got ac...
i don't know why..
---
anupam :oops: :oops: :oops: :oops:

Posted: Mon May 26, 2003 9:28 am
by ayaw
i got the same result...
some of my friends try to solve this problem also got WA

anyone knows how to get the input

just for info, i use:

Code: Select all

for(i=0;i<5;i++) {
  for(j=0;j<5;j++) PUZZLE[i][j]=getchar();
  scanf("\n");
}
to gather the puzzle

and...

Code: Select all

do {
  move = getchar();
  if(move!='0'&&flag==1) flag = puzzle(move);
} while(move!='0');
scanf("\n");
to gather the move

anyone can help me???

Posted: Mon May 26, 2003 3:48 pm
by epsilon0
hello Olivier, remember your old mate epsilon zero? :P

i just solved this 227 puzzle.

i had no problem with input, i used fgets to fetch each line.

since i had AC, i can tell you that you can assume the following:

the "command lines" have no space in them, ie:

AAAA BB0 <-- you will never have something like this.

i used an array char puzzle[5][7] to store the puzzle 7 leaves room for 2 extra bytes: '\n' and '\0'.

then fetch it with fgets(puzzle,7,stdin);

for the command lines i used a buffer char buffer[1024] and fgets(buffer, 1024, stdin);

it worked well, so you can assume no line is longer than 1022 characters.

have fun :)

Posted: Tue May 27, 2003 6:22 am
by anupam
i used getchar() to take the input and ignores the whitespace very carefully. because there is a whitespace needed in the input.
check with special fn to take input and i think no more complexity to solve the problem..
--
anupam :oops: :oops: :oops: