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

Axiom
New poster
Posts: 3
Joined: Wed Dec 05, 2001 2:00 am

Post by Axiom »

Hi!
does any one noes the catch with problem 227? the input seems to be VERY tricky!.

any help is greatly appreciated!
FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan

Post 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;
}
}
}
le
New poster
Posts: 1
Joined: Thu May 09, 2002 5:37 pm
Location: Vienna, Austria
Contact:

Post 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]
javipeg
New poster
Posts: 1
Joined: Wed May 29, 2002 4:20 am

Post 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.
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post 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
htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

227 - Puzzle

Post 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]
bery olivier
Learning poster
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France
Contact:

227 puzzle

Post 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.
Not AC yet Image AC at last Image
cris_vice
New poster
Posts: 1
Joined: Thu Mar 13, 2003 9:30 pm

It also happened to me :-?

Post 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.
shahriar_manzoor
System administrator & Problemsetter
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

scanf() vs gets()

Post 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*/
bery olivier
Learning poster
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France
Contact:

Post 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.
Not AC yet Image AC at last Image
djemili
New poster
Posts: 1
Joined: Sat Apr 05, 2003 1:17 pm

Post 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.
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post 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:
"Everything should be made simple, but not always simpler"
ayaw
New poster
Posts: 18
Joined: Fri May 23, 2003 3:52 pm
Contact:

Post 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???
peace...
epsilon0
Experienced poster
Posts: 112
Joined: Tue Nov 12, 2002 11:15 pm
Location: Paris, France.

Post 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 :)
We never perform a computation ourselves, we just hitch a ride on the great Computation that is going on already. --Tomasso Toffoli
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post 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:
"Everything should be made simple, but not always simpler"
Post Reply

Return to “Volume 2 (200-299)”