445 - Marvelous Mazes
Moderator: Board moderators
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;
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]
[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]
445help
//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.
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.
-
- Learning poster
- Posts: 68
- Joined: Fri Oct 26, 2001 2:00 am
- Location: Dhaka, Bangladesh
- 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
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
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
About q445
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]

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

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

-
- New poster
- Posts: 21
- Joined: Sun Sep 14, 2003 11:44 pm
- Location: USA
- Contact:
Thanks
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
Thanks for the tips. Got the problem solved out of a bit more work. Your tips were very helpful!
Thanks a million!!!,
GCS
-
- 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
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
-
- New poster
- Posts: 21
- Joined: Sun Sep 14, 2003 11:44 pm
- Location: USA
- Contact:
Tips?
Anyone able to offer any tips?
GCS
GCS
-
- 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
Thank you so much for your response! I made one of the silliest mistakes by not reading the problem correctly!
Thanks again
GCS
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]
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]