101 - The Blocks Problem

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

Moderator: Board moderators

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

Don't print unnecessary space and please remove your code..

ilms
New poster
Posts: 2
Joined: Fri Dec 15, 2006 6:20 pm

Re: q101 where is my wrong

Post by ilms »

I have know where is my wrong
this is my new code

Code: Select all

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class bb{
	private:
		int box[25][25];
		int box_in[25];
	public:
		int use_box;
		void re_set();
		void re_put(int index);
		void put(int fb,int tb);
		void put_all(int fb,int tb);
		void show();
		bool check(int fb,int tb);
};
bb sbox;
int main(){
	string da,db;
	int ia,ib;
	while(cin>>ia){
		sbox.use_box=ia;
		sbox.re_set();
		re_load:
		cin>>da;
		if(da=="quit"){
			sbox.show();
			//system("pause");
			//return 0;
		}else{
			cin>>ia>>db>>ib;
			if(ia!=ib&&sbox.check(ia,ib)){
				if(da=="move"){
					if(db=="onto"){
						sbox.re_put(ia);
						sbox.re_put(ib);
						sbox.put(ia,ib);
					}
					if(db=="over"){
						sbox.re_put(ia);
						sbox.put(ia,ib);
					}
				}
				if(da=="pile"){
					if(db=="onto"){
						sbox.re_put(ib);
						sbox.put_all(ia,ib);
					}
					if(db=="over"){
						sbox.put_all(ia,ib);
					}				
				}
			}
			goto re_load;
		}
	}
	return 0;
}
void bb::re_set(){
	int ax,ay;
	for(ax=0;ax<25;ax++)
		for(ay=0;ay<25;ay++){
			box[ax][ay]=ax;
			box_in[ax]=1;
		}
}
void bb::re_put(int index){
	int ax,ay,ac,ad,ae;
	for(ax=0;ax<use_box;ax++)
		for(ay=0;ay<box_in[ax];ay++)
			if(box[ax][ay]==index){
				ad=box_in[ax];
				for(ac=ay+1;ac<ad;ac++){
					box_in[ax]--;
					ae=box[ax][ac];
					box[ae][box_in[ae]]=ae;
					box_in[ae]++;
				}
				return;
			}
}
void bb::put(int fb,int tb){
	int ax,ay,ac,ad;
	for(ax=0;ax<use_box;ax++)
		for(ay=0;ay<box_in[ax];ay++)
			if(box[ax][ay]==fb){
				box_in[ax]--;
				for(ac=ay;ac<use_box;ac++){
					box[ax][ac]=box[ax][ac+1];
				}
				for(ax=0;ax<use_box;ax++){
					ad=box_in[ax];
					for(ay=0;ay<ad;ay++)
						if(box[ax][ay]==tb){
							box[ax][box_in[ax]]=fb;
							box_in[ax]++;
						}
				}
				return;
			}
}
void bb::put_all(int fb,int tb){
	int ax,ay,ac,ad,ae;
	for(ax=0;ax<use_box;ax++)
		for(ay=0;ay<box_in[ax];ay++)
			if(box[ax][ay]==fb)
				for(ac=0;ac<use_box;ac++)
					for(ad=0;ad<box_in[ac];ad++)
						if(box[ac][ad]==tb){
							ae=box_in[ax];
							for(ad=ay;ad<ae;ad++){
								box[ac][box_in[ac]]=box[ax][ad];
								box_in[ax]--;
								box_in[ac]++;
							}
							return;
						}
}
void bb::show(){
	int ax,ay;
	for(ax=0;ax<use_box;ax++){
		cout<<ax<<":";
		for(ay=0;ay<box_in[ax];ay++){
			cout<<" "<<box[ax][ay];
		}
		cout<<endl;
	}
}
bool bb::check(int fb,int tb){
	int ax,ay;
	for(ax=0;ax<use_box;ax++)
		for(ay=0;ay<box_in[ax];ay++)
			if(box[ax][ay]==fb){
				for(ay=0;ay<box_in[ax];ay++)
					if(box[ax][ay]==tb)
						return false;
				return true;
			}
}


Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

the output should be

Code: Select all

If there is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear stacked in that position with each block number separated from other block numbers by a space. Don't put any trailing spaces on a line.
your program prints extra spaces in every line

i hope u will be able to get AC now
Last edited by Debashis Maitra on Mon Dec 18, 2006 10:02 pm, edited 1 time in total.
Akash chhoyar swopno
Dream to touch the sky

Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

try this input

Code: Select all

15
move 10 onto 1
move 8 over 1
move 2 over 9
move 6 over 10
pile 8 over 6
pile 8 onto 5
pile 1 onto 5
quit
move 12 over 1
move 14 onto 9
quit
output should be

Code: Select all

0: 0
1:<==no space here
2:
3: 3<==no space after last number
4: 4
5: 5 1 10
6: 6
7: 7
8: 8
9: 9 2
10:
11: 11
12: 12
13: 13
14: 14
<===cursor is here>
Akash chhoyar swopno
Dream to touch the sky

dddolphin
New poster
Posts: 3
Joined: Fri Dec 15, 2006 10:16 am

ACC

Post by dddolphin »

Hi Debashis Maitra,

Thanks for the reply.

Yes, there's no space after the last or before the first char of the line.
And there's always a '\n' at the end of the line, even the last line!

Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

Ok

now post your output part of your code

or

send me the output text file to me

debashis_du@yahoo.com
Akash chhoyar swopno
Dream to touch the sky

joy
New poster
Posts: 48
Joined: Wed Oct 18, 2006 1:00 pm
Location: Dhaka, Bangladesh
Contact:

Re: Solved--Thanks

Post by joy »

dddolphin wrote:the above processing is correct.

but still encounter presentation error
here is my out put part:
hope it helps..

Code: Select all

		for(i=0; i<n; i++)
		{
			printf("%d:", i);
			for(j=0; j<top[i]; j++)
				printf(" %d", bl[i][j]);
			puts("");
		}
form kisui na ... class tai asol....
iF U hv d class u get the form....

blodstone
New poster
Posts: 6
Joined: Sun Nov 19, 2006 5:47 am

Post by blodstone »

I have solved the problems, it seems that I've make a few fatal mistake. Thx for the help.

mlwmlw
New poster
Posts: 1
Joined: Fri Dec 29, 2006 11:07 pm

101 RTE

Post by mlwmlw »

Runtime Error
Invalid memory reference


why?

my answer is ok?!

why memory invalid?

because my malloc mode?

because my funtion?


please help me....thanks.....

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
void initial(void);
char **init;
long high;
int i;
void popline(int);
void pushline(int);
void moveonto(int,int);
void moveover(int,int);
void pileonto(int,int);
void pileover(int,int); 
void display(void);
char popli[25];
void pop(int);
void push(int);
char poptemp;
int now[25];
main()
{

      initial();

      while(1)
      {
        int input1,input2;
        char in[5],in2[5];  
        scanf("%s",in);
        if(!strcmp(in,"quit"))
          break;
        scanf(" %d",&input1);
        scanf(" %s",in2);
        scanf(" %d",&input2);
        if(input1>high-1||input2>high-1)continue;
        if(!strcmp(in,"move"))
          if(!strcmp(in2,"onto"))
            moveonto(input1,input2);
          else if(!strcmp(in2,"over"))
            moveover(input1,input2);
          else continue;
        else if(!strcmp(in,"pile")) 
          if(!strcmp(in2,"onto"))
            pileonto(input1,input2);
          else if(!strcmp(in2,"over"))
            pileover(input1,input2);
          else continue;
        else continue;
      }
      display();
      for(i=0;i<=high-1;i++)
        free(init[i]);
      free(init);
      high=0;
} 
void initial(void)
{
    char *tpr;
    while(high>24 || high<1)
      scanf("%d",&high);
              
    init=(char**)malloc(sizeof(char**)*high);
    tpr=(char*)malloc(sizeof(char*)*high*high+50);
     for(i=0;i<=high-1;i++)
    { 
      init[i]=tpr+high*i;
      //init[i]=(char*)malloc(sizeof(char*)*high);
    }
    
    for(i=0;i<=high-1;i++)
    { 
      init[i][0]=i+'0';
      init[i][1]='\0';
      now[i]=i;
    }

}
void pop(int x)
{
 poptemp=init[x][(strlen(init[x])-1)];
 init[x][strlen(init[x])-1]='\0';
}
void popline(int x)
{
 int ser=0,put=0;
 while(init[now[x]][ser]!=x+'0')
   {
   ser++;
   }
   strcpy(popli,&init[now[x]][ser]);
   init[now[x]][ser]='\0';
 /*while(popli[put]=init[now[x]][ser])
   {
   put++;ser++;
   init[now[x]][ser-1]='\0';
   if(popli[put-1]=='\0')
   break;
   }*/
 
}
void pushline(int x)
{
 int len=strlen(popli);
 strcat(init[now[x]],popli);
 for(int put=0;put<len;put++)
 now[popli[put]-'0']=x;
}
void push(int x)
{
 if(!poptemp)return;
 int len=strlen(init[x]);
 init[x][len+1]='\0';
 init[x][len]=poptemp;
 now[poptemp-'0']=x;
}
void moveonto(int x,int y)
{
 int len;
 if(x==y)return ;
 if(now[x]==now[y])return;
 len=strlen(init[now[y]])-1;
 while(y!=init[now[y]][len]-'0')
 {
   pop(now[y]);
   len--;
   push(poptemp-'0');
 }
 pop(now[x]);

 while(x!=poptemp-'0')
 {
   push(poptemp-'0');
   if(now[x]==(poptemp-'0'))return;
   pop(now[x]);
 }
 
 push(now[y]);
}
void moveover(int x,int y)
{
 if(x==y)return ;
 if(now[x]==now[y])return;
 pop(now[x]);

 while(x!=poptemp-'0')
 {
   push(poptemp-'0');
   if(now[x]==(poptemp-'0'))return;
   pop(now[x]);
 }
 
 push(now[y]);
}
void pileonto(int x,int y)
{
  int len;
  if(x==y)return;
  if(now[x]==now[y])return;
  len=strlen(init[now[y]])-1;
  while(y!=init[now[y]][len]-'0')
  {
    pop(now[y]);
    len--;
    push(poptemp-'0');
  }      
  popline(x);
  pushline(now[y]); 
}
void pileover(int x,int y)
{
     if(now[x]==now[y])return;
     if(x==y)return;
     popline(x);
     pushline(now[y]);
}
 
 
 
void display(void)
{
     char k=0;
     for(i=0;i<=high-1;i++)
          {            
             printf("%d: ",i);
             k=0;
             while(init[i][k]!='\0')
               {
               printf("%d ",init[i][k]-'0');
               k++;
               }
               
             printf("\n"); 
          }
}


   
}

ipa2
New poster
Posts: 1
Joined: Mon Jan 01, 2007 9:27 am

101 WA

Post by ipa2 »

Anyone, please give me some test-cases for problem 101-The Blocks Problem to test my program because i keep receiving WA for this problem.

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

Post by stcheung »

Thank you StepLg for your previous comment. To clarify, (1) output an extra line at the end despite what the problem statement says and (2) DO NOT add a space in front of the row number like the sample output does, even when the row number is a single digit. How horrible that the problem statement is incorrect like this when now a solution won't count as "solved" if it's a PE.

zaman
New poster
Posts: 8
Joined: Sun Jan 07, 2007 5:40 am
Location: dhaka

WA(101)

Post by zaman »

I've got wrong answer for this problem.Anybody please give some sample inputs where it doesn't pass. Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void command_handle_move();
void command_handle_pile();
/*void over_handle( int pos_a, int pos_b, int pos_a1, int pos_b1);
void onto_handle( int pos_a, int pos_b, int pos_a1, int pos_b1); */

int a, b, pos_a, pos_b, pos_a1, pos_b1, blok[25][25], command_call;

void main(){

int n, i, j ;
char seps[] =" ", ch[25], *token;
//freopen("G:\\input.txt","r",stdin);

while( scanf("%d",&n)!=EOF ){
j = 0;

for( i = 0; i < 25; i++ ){

for( j = 0; j < 25; j++ ){

if( j == 0 )
blok[j] = i;

else
blok[j] = -1;
}
}

gets(ch);
while( strcmp(gets(ch),"quit") !=0 ){
token = strtok( ch,seps );
pos_a = pos_b = pos_a1 = pos_b1 = -1;

if( strcmp(token,"move") == 0 ){
//token = strtok( NULL,seps);
token = strtok( NULL,seps);
a = token[0] - '0';
token = strtok( NULL,seps);

if( strcmp(token,"onto") == 0 )
command_call = 1;

else
command_call = 2;

token = strtok( NULL,seps);
//token = strtok( NULL,seps);
b=token[0] - '0';
command_handle_move();

}

else{
token = strtok( NULL,seps);
//token = strtok( NULL,seps);
a = token[0] - '0';
token = strtok( NULL,seps);

if( strcmp(token,"onto") == 0 )
command_call = 1;

else
command_call = 2;

//token = strtok( NULL,seps);
token = strtok( NULL,seps);
b=token[0] - '0';
//pos_a = pos_b = pos_a1 = pos_b1 = -1;

command_handle_pile();

}

//gets(ch);

}

for( i = 0; i < n; i++){
printf("%d: ",i);

for( j = 0; j < 25; j++){

if( blok[j] == -1)
break;

else
printf("%d ",blok[j]);

}
printf("\n");

}



}

}


void command_handle_move(void){

int i, j;

for( i = 0; i < 25; i++ ){

if( pos_a != -1 && pos_b != -1)
break;

for( j =0; j < 25; j++){
if( blok[j] == -1)
break;

if( blok[j] == a){
pos_a = i;
pos_a1 = j;
}

if( blok[j] == b){
pos_b = i;
pos_b1 = j;
}
}
}

if( pos_a == pos_b)
return ;

if( blok[pos_a][pos_a1+1] != -1){
blok[blok[pos_a][pos_a1+1]][0] = blok[pos_a][pos_a1+1];
blok[pos_a][pos_a1+1] = -1;
pos_a1+= 2;

while(blok[pos_a][pos_a1] != -1){
blok[blok[pos_a][pos_a1]][0] = blok[pos_a][pos_a1+1];
blok[pos_a][pos_a1+1] = -1;
pos_a1++;
}

}

if(command_call == 1){
if( blok[pos_b][pos_b1+1] != -1){
blok[blok[pos_b][pos_b1+1]][0] = blok[pos_b][pos_b1+1];
blok[pos_b][pos_b1+1] = -1;
pos_b1+= 2;

while(blok[pos_b][pos_b1] != -1){
blok[blok[pos_b][pos_b1]][0] = blok[pos_b][pos_b1+1];
blok[pos_b][pos_b1+1] = -1;
pos_b1++;

}
blok[pos_b][pos_b1+1] = blok[pos_a][pos_a1];
blok[pos_a][pos_a1] = -1;

}

else{
blok[pos_b][pos_b1+1] = blok[pos_a][pos_a1];
blok[pos_a][pos_a1] = -1;

}
}

else{

pos_b1++;

while( blok[pos_b][pos_b1] !=-1)
pos_b1++;

blok[pos_b][pos_b1] = blok[pos_a][pos_a1];
blok[pos_a][pos_a1] = -1;
}



}

void command_handle_pile(){
int i, j;

for( i = 0; i < 25; i++ ){

if( pos_a != -1 && pos_b != -1)
break;

for( j =0; j < 25; j++){
if( blok[j] == -1)
break;

if( blok[j] == a){
pos_a = i;
pos_a1 = j;
}

if( blok[j] == b){
pos_b = i;
pos_b1 = j;
}
}
}

if( pos_a == pos_b)
return ;

if(command_call == 1){
if( blok[pos_b][pos_b1+1] != -1){
blok[blok[pos_b][pos_b1+1]][0] = blok[pos_b][pos_b1+1];
blok[pos_b][pos_b1+1] = -1;
pos_b1+= 2;

while(blok[pos_b][pos_b1] != -1){
blok[blok[pos_b][pos_b1]][0] = blok[pos_b][pos_b1+1];
blok[pos_b][pos_b1+1] = -1;
pos_b1++;

}
blok[pos_b][pos_b1+1] = blok[pos_a][pos_a1];
blok[pos_a][pos_a1] = -1;

}

while( blok[pos_a][pos_a1] != -1){
blok[pos_b][pos_b1++] = blok[pos_a][pos_a1];
blok[pos_a][pos_a1] = -1;
pos_a1++;

}

}

else{
pos_b1++;

while( blok[pos_b][pos_b1] != -1)
pos_b1++;

while( blok[pos_a][pos_a1] != -1 ){
blok[pos_b][pos_b1++] = blok[pos_a][pos_a1];
blok[pos_a][pos_a1] = -1;
pos_a1++;

}

}


}

thanks in advance
Keep posting!!!!!!

Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

try this input

Code: Select all

15
move 10 onto 1
move 8 over 1
move 7 over 9
move 6 over 10
pile 14 over 6
pile 8 onto 5
pile 1 onto 6
move 12 over 1
move 14 onto 9
quit
output should be

Code: Select all

0: 0
1:
2: 2
3: 3
4: 4
5: 5 8 6 1 10 12
6:
7: 7
8:
9: 9 14
10:
11: 11
12:
13: 13
14:

try to print your output in a file

And please dont open a new thread if there is one already

i hope this may help u

Best of luck
Akash chhoyar swopno
Dream to touch the sky

Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

read previous posts

there is a lot of thread in this topic
see

http://online-judge.uva.es/board/viewtopic.php?t=13782
http://online-judge.uva.es/board/viewtopic.php?t=13232
http://online-judge.uva.es/board/viewtopic.php?t=10497
http://online-judge.uva.es/board/viewtopic.php?t=10406

etc etc etc...........

and i hope you will not open a new thread if there is one already

best of luck
Akash chhoyar swopno
Dream to touch the sky

Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

read previous posts

there is a lot of thread in this topic
see

http://online-judge.uva.es/board/viewtopic.php?t=13782
http://online-judge.uva.es/board/viewtopic.php?t=13232
http://online-judge.uva.es/board/viewtopic.php?t=10497
http://online-judge.uva.es/board/viewtopic.php?t=10406

etc etc etc...........

and i hope you will not open a new thread if there is one already

best of luck
Akash chhoyar swopno
Dream to touch the sky

Post Reply

Return to “Volume 1 (100-199)”