## 445 - Marvelous Mazes

Moderator: Board moderators

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

### 445 WA

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;
T:=0;
end;
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;
T:=0;
end;
end;
end.[/pascal]

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

### 445help

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;
end;
while len<>0 do begin
slv;
len:=0;
while not eoln do begin
len:=len+1;
end;
end;
writeln;
end;
end.

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

### help me??

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

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:
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

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

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

void main()
{
short repeat, i;

while(cin.good())
{
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;
}
}
[/cpp]

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

### Problem 445

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

Learning poster
Posts: 73
Joined: Mon Oct 14, 2002 7:15 am
Location: United States
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
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]

Or is there anything wrong?

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

### Thanks

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

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?

Anyone able to offer any tips?

GCS

Betty
New poster
Posts: 19
Joined: Sun Aug 17, 2003 2:10 pm
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

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??????

#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]