227 - Puzzle
Moderator: Board moderators
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");
}
}
haha after screening my code after posting it, I realize I need the "\n" here:
Thanks again for your help.
Code: Select all
cout << "This puzzle has no final configuration.\n";
-
- New poster
- Posts: 27
- Joined: Mon Nov 27, 2006 4:44 am
- Location: Indonesia
Why WA?? Someone please help me...
Is there any trick here? Let me know...
Thx...
Is there any trick here? Let me know...

Code: Select all
/* Removed after AC */
Last edited by razor_blue on Thu May 10, 2007 5:13 am, edited 1 time in total.
-
- New poster
- Posts: 27
- Joined: Mon Nov 27, 2006 4:44 am
- Location: Indonesia
Last edited by chetan on Tue Jul 17, 2007 11:54 am, edited 1 time in total.
WA------------> Plz help
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;
}
-
- New poster
- Posts: 21
- Joined: Wed Oct 08, 2008 7:04 am
Re: 227 puzzle
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.
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.
Re: 227 puzzle
PLZ give me some critical Input-output
-
- Learning poster
- Posts: 76
- Joined: Mon Jul 21, 2008 8:50 am
- Location: SUST,SYLHET,BANGLADESH.
- Contact:
Re: 227 puzzle
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. 

Heal The World
-
- New poster
- Posts: 37
- Joined: Wed Oct 03, 2007 10:42 am
Re: 227 puzzle
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.
Please help me.
thanks in advance.
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;
}
thanks in advance.
-
- Learning poster
- Posts: 74
- Joined: Fri May 08, 2009 5:16 pm
Re: 227 puzzle
I got 10 WAs and then got acc, the input is so critical here,change ur input technique and use string class in C++,