445 - Marvelous Mazes

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia

445 WA

Post by Revenger » Sun Jul 07, 2002 6:26 pm

Why I get WA?

[pascal]Program p445;

Var Ch : Char;
T,i : Integer;

begin
T:=0;
While Not Eof(InPut) Do begin
While Eoln(InPut) do begin
Writeln;
Readln;
T:=0;
end;
Read(Ch);
if Ch='!' then begin
Writeln;
T:=0;
end else
if Ch in ['0'..'9'] then T:=T+Ord(Ch)-Ord('0') else begin
for i:=1 to T do Write(Ch);
T:=0;
end;
While Eoln(InPut) do begin
Writeln;
Readln;
T:=0;
end;
end;
end.[/pascal]

lu shukai
New poster
Posts: 7
Joined: Tue Aug 06, 2002 9:26 am

445help

Post by lu shukai » Mon Aug 19, 2002 1:57 pm

//i got wrong answer please me.
program P445;
{$APPTYPE CONSOLE}
uses
SysUtils;

var
str:array[1..1000000] of char;
len:integer;
procedure slv;
var
time,t,j:integer;
begin
t:=1;
while t<=len do begin
if str[t]='!' then begin
writeln;
t:=t+1;
end;
time:=0;
while (str[t]>='0')and(str[t]<='9') do begin
time:=time+ord(str[t])-48;
t:=t+1;
end;
if str[t]='b' then
for j:=1 to time do
write(' ')
else for j:= 1 to time do
write(str[t]);
t:=t+1;
end;
writeln;
end;



begin
while not eof do begin
len:=0;
while not eoln do begin
len:=len+1;
read(str[len]);
end;
readln;
while len<>0 do begin
slv;
len:=0;
while not eoln do begin
len:=len+1;
read(str[len]);
end;
readln;
end;
writeln;
end;
end.

Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

help me??

Post by Shahid » Sat Aug 31, 2002 12:04 am

can anyone tell me what should be the appropriate input routine for this problem in C?

i find two difficulties: 1) description for a row of a maze can be terminated either by ! sign or newline
2) so a newline may not always the indication of the description of another maze, it could be the description of the next row of the current maze

so what way the input routine formed and the out shoul be processsed?
at first when i solved it i get P.E....but when i was going to correct the error, i got straight WA..so help me

amd-RS
New poster
Posts: 27
Joined: Thu Sep 05, 2002 7:37 am

445 WA

Post by amd-RS » Fri Jan 17, 2003 12:11 am

I need some input for this problem. It works for the sample input at ACM.

Thanks, Aur

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Fri Jan 17, 2003 9:11 am

Mazes are separated one from other by EMPTY LINE. So if iput contains line, which contians only new line character, it's end of description of current maze :)

Best luck
Dominik

Sneeze
New poster
Posts: 13
Joined: Thu Jan 30, 2003 4:04 am

About q445

Post by Sneeze » Wed May 07, 2003 1:02 pm

The following is my code, could anyone figure out why I always got WA?

[cpp]
#include <iostream>
#include <cctype>
using namespace std;

void main()
{
char read_in[3000], *ptr;
short repeat, i;

cin.getline(read_in, 3000);
while(cin.good())
{
ptr=read_in;
repeat=0;
while(*ptr!='\0')
{
if(isdigit(*ptr))
repeat+=short(*ptr-'0');
else if(*ptr=='b')
{
for(i=0; i<repeat; i++)
cout << ' ';
repeat=0;
}
else if(*ptr=='!')
{
repeat=0;
cout << endl;
}
else
{
for(i=0; i<repeat; i++)
cout << *ptr;
repeat=0;
}
ptr++;
}
cout << endl;
cin.getline(read_in, 3000);
}
}
[/cpp]
:cry:

ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Problem 445

Post by ravingavin » Fri Oct 17, 2003 9:32 pm

Hey, What's up fellas? Well, I've been attempting probelm 445. I think I've just about got the problem solved (I have the logic). When I submit the program, online judge gives me TLE.

I think my input is messing up the judge. I'm new to this and haven't done a problem when you have to use EOF. So, some tips would be helpful.

[cpp]
#include <iostream>
#include <cctype>

using namespace std;

//445

int main(){


char *input;
input = new char[133];

while(!cin.eof()){ //This part!!! I tried using cin.get() != EOF as well

cin.getline(input, 132);

int length = strlen(input);

int sum=0;
int numstatus=0;

for(int count = 0; count < length; count++){

if(isdigit(input[count]) && numstatus==0){
sum = (static_cast<int>(input[count])-48);
numstatus = 1;
}

else if(isdigit(input[count]) && numstatus==1){
sum += (static_cast<int>(input[count])-48);
}

else if(isalpha(input[count]) && isupper(input[count])){

for(int counter=0; counter < sum; counter++)
cout << input[count];

sum=0;
numstatus=0;
}

else if(input[count] == '*'){

for(int twocounter=0; twocounter < sum; twocounter++)
cout << input[count];

sum=0;
numstatus=0;

}

else if(input[count] == '!'){

cout << endl;

sum=0;
numstatus=0;
}

else if(input[count] == 'b'){

for(int threecounter=0; threecounter < sum; threecounter++)
cout << " ";

sum=0;
numstatus=0;

}


}

cout << endl;



delete [] input;
input = new char[133];


}

return 0;

}
[/cpp]


GCS

rjhadley
Learning poster
Posts: 73
Joined: Mon Oct 14, 2002 7:15 am
Location: United States

Post by rjhadley » Sat Oct 18, 2003 6:02 pm

Try checking for EOF after reading a line. i.e. replace
[cpp]
while(!cin.eof()){
cin.getline(input, 132);
// etc.
}
[/cpp]
with
[cpp]
while( true ) {
cin.getline( input, 132 );
if( cin.fail() || cin.eof() ) {
break;
}
// etc.
}
[/cpp]

Also, note that it greatly simplifies things if repeated statements like
[cpp] sum=0;
numstatus=0;
[/cpp]
are factored out of all the if statements. (Similarly, your loops can be factored out). You'll end up with a solution that is half the length or smaller.

Sneeze
New poster
Posts: 13
Joined: Thu Jan 30, 2003 4:04 am

Post by Sneeze » Sun Oct 19, 2003 4:52 am

This is my WA code.
[code] int number=0;
char printchar;
string line;

getline(cin, line);
while(cin.good()){
int len = line.length();
for(int i=0; i<len; i++){
if(isdigit(line[i]))
number += int(line[i]-'0');
else{
printchar = line[i];
if(printchar == 'b')
printchar = ' ';
else if(printchar == '!'){
number=0;
cout << endl;
continue;
}
while(number){
cout << printchar;
number--;
}
}
}
cout << endl;
getline(cin, line);
}[/code]

Do I have the same problem about reading EOF?
Or is there anything wrong? :roll:

ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Thanks

Post by ravingavin » Tue Oct 21, 2003 2:05 am

Hey rjhadley,

Thanks for the tips. Got the problem solved out of a bit more work. Your tips were very helpful!

Thanks a million!!!,

GCS

ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

445 WA

Post by ravingavin » Fri Jan 16, 2004 7:39 pm

Hello All,

If someone could give me some tips, I would appreciate that; I'm getting WA, and I have no idea what could be wrong. I think I'm pretty darn close. It might just be a formatting error that I am unaware of:

[cpp]
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

int main(){

char *array;
array = new char[134];

while( cin.getline(array, 133) ) {

int sum=0;

for(int i=0; i <strlen(array); i++){


if(isdigit(array)){
sum += (array-48);
}

else if(array == 'b'){
for(int k=0; k<sum; k++){
cout << " ";
}
sum=0;
}

else if(array == '!')
cout << "\n";

else{
for(int j=0; j <sum; j++){

cout << array;
}

sum=0;
}



}
cout << endl;

delete [] array;
array = new char[134];

}


return 0;
}

[/cpp]

Thanks,

GCS

ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Tips?

Post by ravingavin » Tue Jan 20, 2004 3:01 am

Anyone able to offer any tips?

GCS

Betty
New poster
Posts: 19
Joined: Sun Aug 17, 2003 2:10 pm

Post by Betty » Fri Jan 23, 2004 5:23 am

i have a feeling that the 132 size limit was on the width of the maze not the input.

ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Thanks

Post by ravingavin » Sat Jan 24, 2004 12:52 am

Hey Betty,


Thank you so much for your response! I made one of the silliest mistakes by not reading the problem correctly!

Thanks again

GCS

rlatif119
New poster
Posts: 16
Joined: Mon Mar 01, 2004 4:00 pm
Location: Dhaka

445-WA.....WHY??????

Post by rlatif119 » Mon Mar 01, 2004 4:44 pm

#include<stdio.h>
void maze_create(int n);

char str[5000],ch;

main()
{
int i,flag;
flag = 0;
while(1)
{
i=0;
while(1)
{
ch = getc(stdin);
if(feof(stdin))
{
flag = 1;
break;
}
if(ch=='\n' && str[i-1] == '\n')
{
str[i++] = ch;
break;
}
str[i++] = ch;
}
if(flag == 1)
break;
else
maze_create(i);
}
}

void maze_create(int n)
{
int i,sum,j;
sum=0;
for(i=0;i<n;++i)
{
if(str[i]>=48 && str[i]<=57)
sum = sum + (str[i]-48);
else if(str[i] == 'b')
{
for(j=1;j<=sum;++j)
printf(" ");
sum = 0;
}
else if(str[i] == '!' || str[i] == '\n')
{
printf("\n");
sum = 0;
}
else
{
for(j=1;j<=sum;++j)
printf("%c",str[i]);
sum = 0;
}

}



}[c][cpp][/cpp][/c][code][c][cpp][color=blue][/color][/cpp][/c][/code]

Post Reply

Return to “Volume 4 (400-499)”