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

Rav
New poster
Posts: 27
Joined: Sat Jun 14, 2003 1:00 pm
Location: Polska Wrocław

WA

Post by Rav » Fri Jun 20, 2003 3:37 pm

Could somebode tells me what i did wrong ?
My program pass all samples. I carefully take all characters using getchar and scanf for first five characters but i get WA. Help. Here my source code.
//@BEGIN_OF_SOURCE_CODE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
using namespace std;
/* @JUDGE_ID: 32286EW 227 C++ */

int main()
{
#ifndef ONLINE_JUDGE
close (0); open ("myprog.in", O_RDONLY);
close (1); open ("myprog.out", O_WRONLY | O_CREAT, 0600);
#endif

char t[5][7],c,tmp;
int wsk_pion,wsk_poz,pusty_poz,pusty_pion,numer=1;

while ((scanf("%c%c%c%c%c",&t[0][0],&t[0][1],&t[0][2],&t[0][3],&t[0][4]))==5)
{
getchar();
cout << "Puzzle #" << numer << ":\n";
++numer;
for (wsk_poz=1;wsk_poz<5;++wsk_poz)
{
for (wsk_pion=0;wsk_pion<5;++wsk_pion)
t[wsk_poz][wsk_pion]=getchar();
getchar();
}
for (wsk_poz=0;wsk_poz<5;++wsk_poz)
for (wsk_pion=0;wsk_pion<5;++wsk_pion)
if (t[wsk_poz][wsk_pion]==' ')
{
pusty_poz=wsk_poz;
pusty_pion=wsk_pion;
}
while ((c=getchar())!='0')
{
if ((c=='A') && (pusty_poz!=0))
{
tmp=t[pusty_poz-1][pusty_pion];
t[pusty_poz-1][pusty_pion]=' ';
t[pusty_poz][pusty_pion]=tmp;
--pusty_poz;
}
else if ((c=='B') && (pusty_poz!=4))
{
tmp=t[pusty_poz+1][pusty_pion];
t[pusty_poz+1][pusty_pion]=' ';
t[pusty_poz][pusty_pion]=tmp;
++pusty_poz;
}
else if ((c=='R') && (pusty_pion!=4))
{
tmp=t[pusty_poz][pusty_pion+1];
t[pusty_poz][pusty_pion+1]=' ';
t[pusty_poz][pusty_pion]=tmp;
++pusty_pion;
}
else if ((c=='L') && (pusty_pion!=0))
{
tmp=t[pusty_poz][pusty_pion-1];
t[pusty_poz][pusty_pion-1]=' ';
t[pusty_poz][pusty_pion]=tmp;
--pusty_pion;
}
else if ((c=='A') || (c=='B') || (c=='R') || (c=='L'))
{
cout << "This puzzle has no final configuration.\n";
goto kon;
}

}

for (wsk_poz=0;wsk_poz<5;++wsk_poz)
{
for (wsk_pion=0;wsk_pion<5;++wsk_pion)
cout << t[wsk_poz][wsk_pion]<< ' ';
cout << '\n';
}
kon:;
while ((c=getchar())!='\n');
cout << '\n';
}
}
//@END_OF_SOURCE_CODE

jaywinyeah
New poster
Posts: 19
Joined: Sun Aug 17, 2003 10:40 pm

Solution

Post by jaywinyeah » Sun Aug 17, 2003 10:45 pm

I believe that if you come across a move command that is not one of 'A', 'B', 'L', 'R', that the message "This puzzle has no final configuration." should be printed.
LL Cool Jay
The Formula Wizard
Jason Winokur

User avatar
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

All and all.

Post by _.B._ » Mon May 17, 2004 6:24 am

Greetings!.
Used pretty much all tips presented here (epsilon0,jaywinyeah), and added the case for 0 as the only command, and got AC :D
Keep posting!.
_.

Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

227.This is killin' me!!

Post by Heartattack! » Wed Jul 28, 2004 5:46 pm

I know some o' you don't want scanf, but I tried this with some other input functions. None work. It's ok to me, not to the judge. I think the character input is ok, are there any special input cases? My program runs for about 0.5seconds. It can handle spaces at the beginning and 'Z's in the middle. What else do I do? Help me, please! I got my exams in a week and this has got my head! :evil: The code:

Code: Select all

[cpp]
[size=9][/size]
#include "stdlib.h"
#include "stdio.h"
#include "iostream"


using namespace std;
void main(void)
{

	register int i,j;
	int count=0;
	char arr[5][5],input;
	register char ch;
	bool flag;

	while(1)
	{
		///////////////////////
		//Input goes here.[Done.]
		for(i=0;i<5;i++)
			for(j=0;j<5;j++)
			{
				scanf("%c",&ch);
				
				while(ch=='\n')
					scanf("%c",&ch);
				if(!i&&!j)
					if(ch=='Z')
						exit(1);
				arr[i][j]=ch;

				
			}
		count++;

		if(count>1)
			cout<<endl<<endl;
		flag=true;
		//Find the position of the blank
			for(i=0;i<5;i++)
			{
				for(j=0;j<5;j++)
					if(arr[i][j]==' ')
						break;
				if(arr[i][j]==' ')
					break;
			}
		/////////////////////////
		//Main processing goes here

		while(scanf("%c",&input))
		{
		
			while(input=='\n')
				scanf("%c",&input);
			if(input=='0')
				break;
			if(input!='A' && input!='B' && input!='L' && input!='R')
			{
				flag=false;
				break;
			}
			

			//Process the input
			switch(input)
			{
			case 'A':
				if(i==0)
					flag=false;
				else
				{
					arr[i][j]=arr[i-1][j];
					i--;
					arr[i][j]=' ';
				}
				break;

			case 'B':
				if(i==4)
					flag=false;
				else
				{
					arr[i][j]=arr[i+1][j];
					i++;
					arr[i][j]=' ';
				}
				break;

			case 'L':
				if(j==0)
					flag=false;
				else
				{
					arr[i][j]=arr[i][j-1];
					j--;
					arr[i][j]=' ';
				}
				break;


			case 'R':
				if(j==4)
					flag=false;
				else
				{
					arr[i][j]=arr[i][j+1];
					j++;
					arr[i][j]=' ';
				}
				break;



			}
			if(!flag)
				break;
		}


		//Output goes here.[Done.]
		cout<<"Puzzle #:"<<count;
		if(!flag)
		{
			cout<<endl<<"This puzzle has no final configuration.";
			while(input!='\n')
				scanf("%c",&input);
		}
		else
		{
			for(i=0;i<5;i++)
			{
				cout<<endl;
				for(j=0;j<5;j++)
				{
					cout<<arr[i][j];
					if(j!=4)
						cout<<' ';
				}
			}
		}
	}

}


[/cpp]
We will, We will BREAK LOOP!!!!

User avatar
jaracz
Learning poster
Posts: 79
Joined: Sun Sep 05, 2004 3:54 pm
Location: Poland

Post by jaracz » Wed Feb 23, 2005 3:51 pm

yes that's right:D

I had same problem (with a blank character in first line)
cause i used scanf and after gets.
I changed scanf to gets and now it works:D

thank you shahriar_manzoor for your post!!
thank you a lot!!

ACed now:)
keep it real!

ElbowMJC
New poster
Posts: 2
Joined: Tue Jun 14, 2005 4:47 pm
Location: Latvia

Post by ElbowMJC » Tue Jun 14, 2005 4:49 pm

Please help somebody, don't know what it gives me WA?
I'm writting on a PASCAL
program Project2;

{$APPTYPE CONSOLE}

uses
SysUtils;

var MasM:array[1..100,1..5,1..5] of string;
MasS:array[1..100] of string;
MasB:array[1..100] of integer;
S,Tmp,TmpS:string;
i,j,k,m,Empi,Empj,Count,Flag:integer;
begin
k:=1;
ReadLn(S);
if Length(S)=4 then S:=S+' ';
while S<>'Z' do
begin
for i:=1 to 5 do
begin
if Length(S)=4 then S:=S+' ';
for j:=1 to 5 do
MasM[k,i,j]:=S[j];
ReadLn(S);
end;
if S[Length(S)]<>'0' then
begin
Tmp:=S;
while S[Length(S)]<>'0' do
begin
ReadLn(S);
Tmp:=Tmp+S
end;
end
else Tmp:=S;
MasS[K]:=Tmp;
Inc(K);
ReadLn(S)

end;
WriteLn;
for m:=1 to K-1 do
begin
Count:=0;
for i:=1 to 5 do //searching the empty square
begin
for j:=1 to 5 do
if MasM[m,i,j]=' ' then
begin
Inc(Count);
Empi:=i;
Empj:=j
end;
end; //end of searching
if Count=1 then
begin
repeat
S:=MasS[m];
Flag:=1;
for i:=1 to Length(S) do
begin
if S='A' then
begin
if Empi<>1 then
begin
Tmp:=MasM[m,Empi-1,Empj];
MasM[m,Empi-1,Empj]:=' ';
MasM[m,Empi,Empj]:=Tmp;
Empi:=Empi-1;
end
else Flag:=0;
end;

if S='B' then
begin
if Empi<>5 then
begin
Tmp:=MasM[m,Empi+1,Empj];
MasM[m,Empi+1,Empj]:=' ';
MasM[m,Empi,Empj]:=Tmp;
Empi:=Empi+1;
end
else Flag:=0;
end;

if S='L' then
begin
if Empj<>1 then
begin
Tmp:=MasM[m,Empi,Empj-1];
MasM[m,Empi,Empj-1]:=' ';
MasM[m,Empi,Empj]:=Tmp;
Empj:=Empj-1;
end
else Flag:=0;
end;

if S='R' then
begin
if Empj<>5 then
begin
Tmp:=MasM[m,Empi,Empj+1];
MasM[m,Empi,Empj+1]:=' ';
MasM[m,Empi,Empj]:=Tmp;
Empj:=Empj+1;
end
else Flag:=0;
end;

end;
until S<>'0';
if Flag=1 then MasB[m]:=1
else MasB[m]:=0;
end
else MasB[m]:=0;
end;





for m:=1 to K-1 do
begin
if MasB[m]=1 then
begin
WriteLn('Puzzle #',m,':');
for i:=1 to 5 do
begin
for j:=1 to 5 do
begin
Write(MasM[m,i,j]);
if j<>5 then Write(' ')
end;
Writeln
end;
if M<>(K-1) then WriteLn;
end
else
begin
WriteLn('Puzzle #',m,':');
WriteLn('This puzzle has no final configuration.');
if m<>(K-1) then WriteLn
end;

end;
end.

sk
New poster
Posts: 2
Joined: Fri Jun 17, 2005 3:07 pm

Post by sk » Fri Jun 17, 2005 3:45 pm

jaracz wrote:yes that's right:D

I had same problem (with a blank character in first line)
cause i used scanf and after gets.
I changed scanf to gets and now it works:D
Each display line contains exactly 5 characters, beginning with the character on the leftmost square (or a blank if the leftmost square is actually the empty frame position).
I think the input will be valid

It is possible that there is an illegal move, even when it is represented by one of the 4 move characters.
what does it means??
Is it possible that there will be ANOTHER character???

is my input valid :

TRGSJ
XDOKI
M VLN
WPABE
UQHCF
AaRRBBL0
Z

Thx

ElbowMJC
New poster
Posts: 2
Joined: Tue Jun 14, 2005 4:47 pm
Location: Latvia

Post by ElbowMJC » Sat Jun 18, 2005 12:31 am

As I have understood, it means, that even if there one of any 4 move characters is displayed and if this move character gives an illegal move, an illegal move occurs, a I mean for example you have
TRGSJ
UHCJK
-MVLN
ZPOJK
AQWV
L0
Z

then you'll get an illegal move.

leo20
New poster
Posts: 3
Joined: Mon Oct 31, 2005 9:55 pm
Location: Bolivia
Contact:

Why Compile Error

Post by leo20 » Mon May 22, 2006 5:25 pm

:evil:
Any help me?????
The Judge return Compile Error, why???
:evil:
this is my code
[/code]
import java.io.IOException;
import java.util.StringTokenizer;
class Main
{
static String leer ()
{
String lin = "";
char c;
try
{
while (true)
{
c = (char) System.in.read ();
if (c == '\n')
{
break;
}
lin = lin + c;
}
}
catch (IOException e)
{
return null;
}
if (lin.length () == 0)
{
return null;
}
return lin;
}


void mostrar (char p [] [])
{
for (int i = 0 ; i < 5 ; i++)
{
for (int j = 0 ; j < 4 ; j++)
{
System.out.print (p [j] + " ");
}
System.out.println (p [4]);
}
}


void Begin ()
{
String a;
int cont = 1;
while (true)
{
int spf = 0;
int spc = 0;
char puzz [] [] = new char [5] [5];
boolean sw = false;
for (int i = 0 ; i < 5 ; i++)
{
if ((a = leer ()).equals ("Z"))
{
sw = true;
break;
}
for (int j = 0 ; j < 5 ; j++)
{
puzz [j] = a.charAt (j);
if (puzz [j] == ' ')
{
spf = i;
spc = j;
}
}
}
if (sw)
break;
String res;
a = "";
while (true)
{
res = leer ();
int ln = res.length () - 1;
a += res;
if (res.charAt (ln) == '0')
break;
}
boolean sw1 = true;
for (int i = 0 ; a.charAt (i) != '0' ; i++)
{
try
{
if (a.charAt (i) == 'A')
{
puzz [spf] [spc] = puzz [spf - 1] [spc];
puzz [spf - 1] [spc] = ' ';
spf--;
}
else
{
if (a.charAt (i) == 'B')
{
puzz [spf] [spc] = puzz [spf + 1] [spc];
puzz [spf + 1] [spc] = ' ';
spf++;
}
else
{
if (a.charAt (i) == 'L')
{
puzz [spf] [spc] = puzz [spf] [spc - 1];
puzz [spf] [spc - 1] = ' ';
spc--;
}
else
{
puzz [spf] [spc] = puzz [spf] [spc + 1];
puzz [spf] [spc + 1] = ' ';
spc++;
}
}
}
}
catch (java.lang.ArrayIndexOutOfBoundsException e)
{
sw1 = false;
}
}
System.out.println ("Puzzle #" + cont + ":");
cont++;
if (sw1)
mostrar (puzz);
else
System.out.println ("This puzzle has no final configuration.");
System.out.println ();
}
}


public static void main (String args [])
{
Main rafael = new Main ();
rafael.Begin ();
}
}

mosaick2
New poster
Posts: 21
Joined: Wed Mar 08, 2006 4:05 am

227(Puzzle), W.A. Who know about this problem?

Post by mosaick2 » Wed Aug 16, 2006 2:02 pm

Who know this problem?
I searched articles related with this problem.
And I applied some rule that I couldn't be realized from the articles.
But, I can't get A.C yet.
Who CAN explain what's wrong in my program or logic?
Thankx, friends~

Code: Select all

#include <iostream>
//#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
#include <cstring>
const int NONZERO = -2;
const int BLSIZE = 6;
const int CSIZE = 100;
const int RSIZE = 100;
const int A = -1;
const int B = 1;
const int L = -1;
const int R = 1;

class Puzzle
{
private:
	typedef struct {
		int col; int row;
	} EmptyPos;
	EmptyPos ep;
	string aline;
	string move;
	char board[CSIZE][RSIZE];

public:
	Puzzle()
	{
		ep.col = 0; ep.row = 0;
		memset(board, 0, sizeof(char)*CSIZE*RSIZE);
	}
	int SetInput()
	{
		memset(board, 0, sizeof(char)*CSIZE*RSIZE);
		for (int c = 1; c <= 5; c++)
		{
			aline = "";
			getline(cin, aline);
			if (aline == "Z")
				return false;
			int pos;
			if ((pos = aline.find(' ')) != string::npos)	
			{
				ep.col = c;
				ep.row = pos+1;
			}
			aline.copy(board[c]+1, BLSIZE-1, 0);
		}
		getline(cin, move);  
		
		int last = move.size()-1;
		string movesum(move);
		while (movesum[last] != '0')
		{
			getline(cin, move);
			movesum += move;			
			last = movesum.size()-1;
		}
		move = movesum;
		last = move.size()-1;
		move.erase(last,1);

		//cout << "move:" << move << endl;		

		return true;
	}

	bool MovePuzzle()
	{
		for (int i = 0; i < move.size(); i++)
		{
			bool inBound = (ep.col != 0 && ep.row != 0 && ep.col != BLSIZE && ep.row != BLSIZE);
			if ( inBound )
			{
				switch (move[i])
				{
				case 'A':
					board[ep.col][ep.row] = board[ep.col+A][ep.row];
					board[ep.col+A][ep.row] = ' ';
					ep.col += A;
					break;
				case 'B':
					board[ep.col][ep.row] = board[ep.col+B][ep.row];
					board[ep.col+B][ep.row] = ' ';
					ep.col += B;
					break;
				case 'R':
					board[ep.col][ep.row] = board[ep.col][ep.row+R];
					board[ep.col][ep.row+R] = ' ';
					ep.row += R;
					break;
				case 'L':
					board[ep.col][ep.row] =  board[ep.col][ep.row+L];
					board[ep.col][ep.row+L] = ' ';
					ep.row += L;
					break;
				default:
					return false;
				}
			}
			else
				return false;
		}
		return true;
	}
	void PrintPuzzle()
	{
		for(int c = 1 ; c <= BLSIZE; c++)
		{
			for (int r = 1; r <= BLSIZE; r++)
			{
				cout << board[c][r] << " ";
			}
			cout << endl;
		}

	}

};
int main()
{
//	ifstream fin("input.txt");
	Puzzle pz;
	int cnt = 0;
	for(;;)
	{
		int ret = 0;
		if (ret = pz.SetInput())
		{
			if(cin.eof())
				return 0;
			cnt++;
			cout << "Puzzle #" << cnt << ":\n";
			if (pz.MovePuzzle() && ret != NONZERO)
				pz.PrintPuzzle();
			else
				cout << "This puzzle has no final configuration.\n";
		}
		else
			break;
	}
	return 0;
}

Backbone
New poster
Posts: 5
Joined: Sun May 21, 2006 8:40 am

227 Puzzle Help please!

Post by Backbone » Wed Sep 20, 2006 7:33 pm

Hi!!! I need some I/O for 227, because OJ tell me WA!!!, If you found some problems in my code plz tell what to do! :D

Code: Select all

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>

using namespace std;
void mover (vector <char>,int);
main()
{
 int i,cont=0,npuzzles=1,flag=1;
 char car;
 vector <char> puzzle;
 
 scanf("%c",&car);
 while(car!='Z')
 {
   while(cont<25)
   {
    if(isalpha(car) || car==' ')
    {
      puzzle.push_back(car);
      cont++;
    }
    scanf("%c",&car);
   }
  if(flag)
  {
  mover(puzzle,npuzzles);
  puzzle.clear();
  npuzzles++;
  cont=0;
  }
  else break;
 scanf("%c",&car);
  if(!isalpha(car) && car!=' ')
    scanf("%c",&car);
 }
}

void mover(vector <char> puzzle,int npuzzles)
{
int j=0,fila,col,filamov,colmov,correcto=0;
char aux,car;
scanf("%c",&car);
while(car!='0')
{
  for(j=0;j<puzzle.size();j++)
     if(puzzle[j]==' ')
      { col=j%5; break; }

  if(j<=4)       fila=0;
  else if(j<=9)  fila=1;
  else if(j<=14) fila=2;
  else if(j<=19) fila=3;
  else           fila=4;
  
  if(isalpha(car))
  {
     if(car=='A')
     {
      filamov=fila-1;
      colmov=col;         
     }
     else if(car=='R')
     {
      filamov=fila;
      colmov=col+1;
     }
     else if(car=='L')
     {
      filamov=fila;
      colmov=col-1;
     }
     else
     {
      filamov=fila+1;
      colmov=col;     
     }
  
     if((filamov>4 || filamov<0) || (colmov>4 || colmov<0))
        correcto=1;
     else
     {
      aux=puzzle[j]; 
      puzzle[j]=puzzle[5*filamov+colmov]; 
      puzzle[5*filamov+colmov]=aux; 
     }
  }
scanf("%c",&car);
}
     printf("Puzzle #%d:",npuzzles);
     
     if(correcto==0)
     {               
         for(j=0;j<puzzle.size();j++)
        {
         if(j%5==0) printf("\n");
        printf("%c ",puzzle[j]);
        } 
     }
     else
     printf("\nThis puzzle has no final configuration.");
printf("\n\n");
}

mukeshtiwari
Learning poster
Posts: 63
Joined: Tue Mar 07, 2006 6:51 pm
Location: india

Post by mukeshtiwari » Fri Dec 01, 2006 3:14 pm

i m not much proficient in c++ as a vector class .and there r also lot of post abt thsi topic in this forum .so u can take input from those posts .here i m giving my accepted code so that u can cross check with ur program....

[DELETED BY MODERATOR - NO ACCEPTED CODES PLEASE]

stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

227 Puzzle - Presentation Error

Post by stcheung » Sun Jan 21, 2007 5:38 am

Any trick to not get Presentation Error on #227 Puzzle? I tried almost 10 times with all kinds of spacing variations but still no luck. Anything I need to watch out for? Normally I consider PE just as good as AC but after they recompile the user stats a PE isn't considered as solving the problem.

tgoulart
New poster
Posts: 42
Joined: Sat Oct 21, 2006 8:37 am
Location: Alegrete, Brazil

Post by tgoulart » Sun Jan 21, 2007 7:02 am

Just print the characters using something like this:

Code: Select all

if (case++ > 0)
	printf("\n");
printf("Puzzle #%d:\n",case);
for (i=0; i < 5; i++) {
	printf("%c",board[i][0]);
	for (j=1; j < 5; j++)
		printf(" %c",board[i][j]);
	printf("\n");
}
Thiago Sonego Goulart - UFMG/Brazil

stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

Post by stcheung » Sun Jan 21, 2007 7:18 am

I copied almost exactly what you have but I am still getting PE. In fact, I might have even tried your variation before. I also tried not showing the empty space if it's the last cell in any row. Grr...

Post Reply

Return to “Volume 2 (200-299)”