227 - Puzzle
Moderator: Board moderators
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;
}
}
}
#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;
}
}
}
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]
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]
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[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]
[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]
-
- 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.
I wrote a source to solve "the puzzle" and I got WA
![:(](./images/smilies/icon_frown.gif)
Is there something I should take care ?
Please let me know what could be wrong.
Not AC yet
AC at last ![Image](http://www.click-smilies.de/sammlung0903/lachen/laughing-smiley-017.gif)
![Image](http://www.click-smilies.de/sammlung0903/sauer/angry-smiley-030.gif)
![Image](http://www.click-smilies.de/sammlung0903/lachen/laughing-smiley-017.gif)
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.
![:-?](./images/smilies/icon_confused.gif)
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:](./images/smilies/icon_cry.gif)
Bye.
-
- System administrator & Problemsetter
- 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*/
as below:
BXXXX
XXXXX
XXXXX
XXXXX
/*Here B is space*/
-
- Learning poster
- Posts: 90
- Joined: Sat Feb 15, 2003 1:39 am
- Location: Paris, France
- Contact:
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.
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.
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:
to gather the puzzle
and...
to gather the move
anyone can help me???
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");
}
and...
Code: Select all
do {
move = getchar();
if(move!='0'&&flag==1) flag = puzzle(move);
} while(move!='0');
scanf("\n");
anyone can help me???
peace...
hello Olivier, remember your old mate epsilon zero? ![:P](./images/smilies/icon_razz.gif)
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![:)](./images/smilies/icon_smile.gif)
![:P](./images/smilies/icon_razz.gif)
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
![:)](./images/smilies/icon_smile.gif)
We never perform a computation ourselves, we just hitch a ride on the great Computation that is going on already. --Tomasso Toffoli