## 227 - Puzzle

Moderator: Board moderators

Axiom
New poster
Posts: 3
Joined: Wed Dec 05, 2001 2:00 am
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
Kept wrong answer......following is my code,I don't know what's wrong with it

#include <stdio.h>

void main()
{
char puz;
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);
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:
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 == 'Z' && input == '\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
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
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. Ivor

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

### 227 - Puzzle

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,c,buffer,move;
for(count=1;;count++)
{
fflush(stdin);
scanf("%c",&c);
if(c=='Z')
break;
puzzle=c;
for(x=0;(c=getchar())!='\n';x++)
buffer[x]=c;
buffer[x]='\0';
for(x=1;x<5;x++)
puzzle[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=' ',buffer='\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

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

### It also happened to me :-?

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 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
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

### scanf() vs gets()

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:
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 ?
Not AC yet AC at last djemili
New poster
Posts: 1
Joined: Sat Apr 05, 2003 1:17 pm
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:
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    "Everything should be made simple, but not always simpler"

ayaw
New poster
Posts: 18
Joined: Fri May 23, 2003 3:52 pm
Contact:
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.
hello Olivier, remember your old mate epsilon zero? 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 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 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:
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   "Everything should be made simple, but not always simpler"