Page 38 of 43

Re: 101 - Help me understand why i always got Runtime Error

Posted: Tue Aug 17, 2010 2:54 am
by feysal
I finally got AC and the code runs in 0.012 s.
My problem was i misunderstood the statement (my english is not as good as i thought) :
after returning any blocks that are stacked on top of blocks a and b to their initial positions.
This is why i miscoded my functions especially the function "pile_onto" which caused the Runtime Error.
So i coded a function "returns" to do the job and call it at it's right place in the others functions

Code: Select all

void returns(int b, list *array[])
{
    struct node *pb = NULL, *tempb = NULL, *next = NULL;
    search(&pb, b, array);
    tempb = pb->next->next;
    while(tempb != NULL)
    {
        int data = tempb->data;
        next = tempb->next;
        insert(data, &array[data]);
        free(tempb);
        tempb = next;
    }
    pb->next->next = NULL;
}
Sorry for the inconvenience

Re: [SOLVED]101- Runtime Error

Posted: Wed Aug 18, 2010 8:21 pm
by shahin_198
hello........................................
any body hereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

Runtime Error - 101

Posted: Sun Sep 05, 2010 10:06 am
by hardfire
i dont know why i am getting runtime error in 101 .
seems have done all correct and it works for the given input

My Code

Code: Select all

#include<iostream>
#include <string.h>
#include <malloc.h>
using namespace std;

struct node{
	int value;
	node* nxt;
};

void returnToInitial(int val, struct node*** blockAd, struct node*** positionsAd, struct node*** parentAd,int total)
{     
     struct node** positions=*(positionsAd);
     struct node** parent=*(parentAd);
     struct node** block=*(blockAd);
     struct node* top = *(positions + val);
     struct node* temp;
     int i;
     top=top -> nxt;
     while(top != NULL)
     {
               *(block + top->value) = *(positions + top->value);
               *(parent + top->value) = NULL;
               temp=top;
               top = top -> nxt;
               temp -> nxt = NULL;
     }
     top = *(positions + val);
     top -> nxt = NULL ; 
}

void moveOnTop(int t,int b, struct node*** blockAd, struct node*** positionsAd, struct node*** parentAd,int total)
{
    struct node** positions=*(positionsAd);
    struct node** parent=*(parentAd);
    struct node** block=*(blockAd);
    struct node* below = *(positions + b);
    struct node* top = *(positions + t);
    struct node* temp;
    below -> nxt = top;
    if(*(parent + t) == NULL)
    {
        *(block + t) = NULL;    
    }
    else
    {
        (*(parent + t)) -> nxt = NULL;
    }
    *(parent + t) = below;
}

int goToTop(int val,struct node*** positionsAd)
{
     struct node** positions=*(positionsAd);
     struct node* top = *(positions + val);
     while(top -> nxt != NULL)
        top = top -> nxt;
    return top -> value;
}

int checkSameStack(int val1,int val2, struct node*** positionsAd)
{
    struct node** positions=*(positionsAd);
    struct node* top = *(positions + val1);
    int retval=1;
    while(top != NULL)
    {
        if(top -> value == val2)
        {
            retval = 0;
            break;   
        }
        top = top -> nxt;
    }
    if(retval == 1)
    {
        top = *(positions + val2);
         while(top != NULL)
         {
             if(top -> value == val1)
                {
                    retval = 0;
                    break;   
              }
              top = top -> nxt;
          }
    }
    return retval;
}
int main()
{
	int total;
	struct node** block;
	struct node** positions;
	struct node** parent;
	struct node* temp,*nxtt;
	int *x;
	int i;
	char input[5];
	int first,second;
	int s;
//	  malloc linked list for each of the block;
	
	cin >> total; 
    block = (struct node **) malloc(4 * total);
    positions = (struct node **) malloc(4 * total);
    parent = (struct node **) malloc (4 * total);
    
    for(i=0;i<total;i++)
    {
                    block[i]=(struct node *) malloc(sizeof(struct node));                
                    (*(block+i))->value=i;
                    (*(block+i))->nxt=NULL;
                    positions[i]=block[i];
                    parent[i]=NULL;
    }

while ( strcmp(input,"quit") != 0 )
{
    cin >> input;
    if (strcmp(input,"move") == 0)
    {
        cin >> first;
        cin >> input;
        cin >> second;
        if(first == second)
            continue;
        if (checkSameStack(first,second,&positions) == 0)
        {
            continue;
        }
        if(strcmp(input,"onto") == 0){
            returnToInitial(first,&block,&positions,&parent,total);
            returnToInitial(second,&block,&positions,&parent,total);
            moveOnTop(first,second,&block,&positions,&parent,total);
        }
        else if(strcmp(input,"over") == 0){
            returnToInitial(first,&block,&positions,&parent,total);
            s = goToTop(second,&positions);
            moveOnTop(first,s,&block,&positions,&parent,total);
        }
        else{
        }        
    }
    
    else if(strcmp(input,"pile") == 0)
    {
        cin >> first;
        cin >> input;
        cin >> second;
        if(first == second)
            continue;
        if (checkSameStack(first,second,&positions) == 0)
        {
            continue;
        }
        if(strcmp(input,"onto") == 0){
            returnToInitial(second,&block,&positions,&parent,total);
            moveOnTop(first,second,&block,&positions,&parent,total);
        }
        else if(strcmp(input,"over") == 0){
            s = goToTop(second,&positions);
            moveOnTop(first,s,&block,&positions,&parent,total);
        }
        else{
        }        
    }
    else{
    
    }
  
}
for(i=0;i<total;i++)
     {
                    printf("%d:",i);
                    temp = *(block + i);
                    while(temp != NULL)
                    {
                             //  printf("HERE");
                               printf(" %d",temp->value);
                               temp=temp->nxt;           
                    }                
                    printf("\n");
                     
     }

   return 0;  
}

Re: Runtime Error - 101

Posted: Fri Sep 10, 2010 10:57 pm
by arifcsecu
your code is too dificult to guess. I used 2d vector . Its very simple to code

Re: Help on Problem 101

Posted: Wed Sep 22, 2010 12:10 am
by Victor Pangaldus
Hello guys,

I am newbie in uva online judge. And I am stuck on a problem 101. I have tested my program with several test cases and it prints properly. But, I do not know why I still get "wrong answer" for my solution. Does anyone knows what is the problem with my solution? Please help me !!! Thanks a lot....:)

Here is my code for problem no 101:

Code: Select all

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

int inp;							// varriable for storing user input	
int board[25][25];					// the board for the block
char command[15];
int *block;							// array that represent the blocks

struct posBlock
{
	int slotBoard;					// variable that represent the slot number of a board (Y)
	int stackBoard;					// variable that represent the stack number in a stack (X)
};

void display();						
void CommandCheck();				
void moveOnto(int a, int b);		
void moveOver(int a, int b);		
void pileOnto(int a, int b);		
void pileOver(int a, int b);		
// helper function
posBlock findBlock(int blockID);	
void moveBlock(int blockID, int destSlot, posBlock fromBlockPos);

int main ()
{
	scanf("%d %s", &inp, command);

	// Initialize the board primitive member
	for (int i = 0; i < 25; i++ )
	{
		for (int j = 0; j < 25; j++ )
		{
			board[i][j] = 32;
		}
	}
	
	// create array that represent blocks
	block = new int [inp];
	// initialize the array (blocks)
	for (int i = 0; i < inp ; i++)
	{
		block[i] = i;
	}

	// placing block to initial position
	for (int i = 0; i < inp; i++)
	{
		board[i][0] = block[i];
	}

	while(command[0] != 'q')
	{
		CommandCheck();
	}
	
	display();

	// delete the array (blocks)
	delete [] block;

	return 0;
}

void display()
{
	for (int i = 0; i < inp ; i++)
	{
		printf("%d:", i);
		for (int j = 0; j < 25; j++)
		{
			if (board[i][j] != 32)
			{
				printf(" %d", board[i][j]);
			}
		}
		printf("\n");
	}
}

void moveOnto(int a, int b)
{
	posBlock blockAPos;
	posBlock blockBPos;
	posBlock blockTemp;
		
	// Find block B
	blockBPos = findBlock(b);
	
	// Move all blocks stacked on block B to its initial position
	for (int i = (blockBPos.stackBoard+1); i < 25; i++)
	{
		if (board[blockBPos.slotBoard][i] != 32)
		{
			blockTemp.slotBoard = blockBPos.slotBoard;
			blockTemp.stackBoard = i;
			moveBlock(board[blockBPos.slotBoard][i], board[blockBPos.slotBoard][i],blockTemp);
		}
	}

	// Find block A
	blockAPos = findBlock(a);

	// Move all blocks stacked aboved block A to its initial position
	for (int i = (blockAPos.stackBoard+1); i < 25; i++)
	{
		if (board[blockAPos.slotBoard][i] != 32)
		{
			blockTemp.slotBoard = blockAPos.slotBoard;
			blockTemp.stackBoard = i;
			moveBlock(board[blockAPos.slotBoard][i], board[blockAPos.slotBoard][i],blockTemp);
		}
	}

	// Move block A onto block B
	moveBlock(a, blockBPos.slotBoard, blockAPos);
}

void moveOver(int a, int b)
{
	posBlock blockAPos;
	posBlock blockBPos;
	posBlock blockTemp;
	
	// Find block A
	blockAPos = findBlock(a);

	// Find block B
	blockBPos = findBlock(b);
	
	// Move all blocks stacked on block A to its initial position
	for (int i = (blockAPos.stackBoard+1); i < 25; i++)
	{
		if (board[blockAPos.slotBoard][i] != 32)
		{
			blockTemp.slotBoard = blockAPos.slotBoard;
			blockTemp.stackBoard = i;
			moveBlock(board[blockAPos.slotBoard][i], board[blockAPos.slotBoard][i],blockTemp);
		}
	}

	// Move block A to the top of stack that consist of block B
	if (blockAPos.slotBoard != blockBPos.slotBoard)
	{
		moveBlock(a, blockBPos.slotBoard, blockAPos);
	}
}

void pileOnto(int a, int b)
{
	posBlock blockAPos;
	posBlock blockBPos;
	posBlock blockTemp;
	int stackTemp[25];
	int numstack = 0;

	// Find block B
	blockBPos = findBlock(b);
	
	// Move all blocks stacked on block B to its initial position
	for (int i = (blockBPos.stackBoard+1); i < 25; i++)
	{
		if (board[blockBPos.slotBoard][i] != 32)
		{
			blockTemp.slotBoard = blockBPos.slotBoard;
			blockTemp.stackBoard = i;
			moveBlock(board[blockBPos.slotBoard][i], board[blockBPos.slotBoard][i],blockTemp);
		}
	}
	// Find block A
	blockAPos = findBlock(a);

	// Move the pile of blocks that contains block A and any blocks above block A to the block B
	if (blockAPos.slotBoard != blockBPos.slotBoard)
	{
		for (int i = (blockAPos.stackBoard); i < 25; i++)
		{
			if (board[blockAPos.slotBoard][i] != 32)
			{
				blockTemp.slotBoard = blockAPos.slotBoard;
				blockTemp.stackBoard = i;
				moveBlock(board[blockAPos.slotBoard][i], blockBPos.slotBoard,blockTemp);
			}
		}
	}
}

void pileOver(int a, int b)
{
	posBlock blockAPos;
	posBlock blockBPos;
	posBlock blockTemp;
	int stackTemp[25];
	int numstack = 0;
	
	// Find block A
	blockAPos = findBlock(a);

	// Find block B
	blockBPos = findBlock(b);

	if (blockAPos.slotBoard != blockBPos.slotBoard)
	{
		// Move the pile of blocks that contains block A and any blocks above block A to the stack of block B
		for (int i = (blockAPos.stackBoard); i < 25; i++)
		{
			if (board[blockAPos.slotBoard][i] != 32)
			{
				blockTemp.slotBoard = blockAPos.slotBoard;
				blockTemp.stackBoard = i;
				moveBlock(board[blockAPos.slotBoard][i], blockBPos.slotBoard,blockTemp);
			}
		}
	}	
}

void CommandCheck()
{
	int a = 0;
	int b = 0;
	
	if (command[0] == 'm')
	{
		scanf("%d %s %d", &a, command, &b);
		if (command[3] == 'r')
		{
			moveOver(a, b);
		}
		else if (command[3] = 'o')
		{
			moveOnto(a, b);
		}
	}
	else if (command[0] == 'p')
	{
		scanf("%d %s %d", &a, command, &b);
		if (command[3] == 'r')
		{
			pileOver(a, b);
		}
		else if (command[3] == 'o')
		{
			pileOnto(a, b);
		}
	}
	scanf("%s", command);
}

//--------------------------------------------------
// helper function
//--------------------------------------------------
posBlock findBlock(int blockID)
{
	posBlock blockPos;
	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
		{
			if(board[i][j] == blockID)
			{
				blockPos.slotBoard = i;
				blockPos.stackBoard = j;
				return blockPos;
			}
		}
	}
}

void moveBlock(int blockID, int destSlot, posBlock fromBlockPos)
{
	for(int i = 0; i < 25 ; i++)
	{
		if(board[destSlot][i] == 32)
		{
			board[destSlot][i] = blockID;
			i = 25;
		}
	}
	board[fromBlockPos.slotBoard][fromBlockPos.stackBoard] = 32;
}

Re: 101 - Help me understand why i always got Runtime Error

Posted: Thu Oct 14, 2010 4:24 pm
by bjk
what is
All blocks on top of block b are moved to their initial positions prior to the pile taking place.
when i have

Code: Select all

 0: 0
 1: 1 9 2 4
 2:
 3: 3
 4:
 5: 5 8 7 6
 6:
 7:
 8:
 9:
and pile 8 onto 1
and get ??????

Code: Select all

 0: 0
 1: 1 8 7 6
 2: 2
 3: 3
 4: 4
 5: 5
 6:
 7:
 8:
 9: 9
second:
what is
after returning any blocks that are stacked on top of blocks a and b to their initial positions.
when i have

Code: Select all

 0: 0
 1: 1 9 2 4
 2:
 3: 3
 4:
 5: 5 8 7 6
 6:
 7:
 8:
 9:
and move 8 onto 1
and get ??????

Code: Select all

 0: 0
 1: 1 8
 2: 2
 3: 3
 4: 4
 5: 5
 6: 6
 7: 7
 8:
 9: 9

101-Runtime Error, using C

Posted: Sun Jan 30, 2011 3:53 am
by lacrimosa629
I've run this program on Dev C++ and gcc , it seems OK, but when I submit the code , I got RE, is anybody can help me?
By the way, I use 2-dimensioned array to simulate the block.

Code: Select all

#include<stdio.h>

int CheckCommand(int block[][25],int a,int b)
{
    if(a==b)
        return 0;
    else{
        if(block[a][0]==block[b][0])
            return 0;
        else 
            return 1;
    }
}
void MoveOnto(int block[][25], int a, int b)
{
    if(CheckCommand(block, a, b)){
        int i,j;
        int location_ax=block[a][0],location_ay;
        int location_bx=block[b][0],location_by;
        /*find position*/
        for(i=0;i<25;i++){
            if(block[location_ax][i]==a){
                location_ay=i;
                break;
            }
        }
        for(i=0;i<25;i++){
            if(block[location_bx][i]==b){
                location_by=i;
                break;
            }
        }
        /*return the stack*/
        for(i=location_ay+1;i<25,block[location_ax][i]!=-1;i++){
            int init_position=block[location_ax][i];
            
            block[init_position][0]=init_position;
            block[location_ax][i]=-1;
        }
        for(i=location_by+1;i<25,block[location_bx][i]!=-1;i++){
            int init_position=block[location_bx][i];
            
            block[init_position][0]=init_position;
            block[location_bx][i]=-1;
        }
        /*move on*/
        block[location_bx][location_by+1]=a;
        block[location_ax][0]=location_bx;
        if(location_ay!=0)
            block[location_ax][location_ay]=-1;
        
    }
}
void MoveOver(int block[25][25], int a, int b)
{
    if(CheckCommand(block, a, b)){
        int i,j;
        int location_ax=block[a][0],location_ay;
        int location_bx=block[b][0],location_btop;
        /*find position*/
        for(i=0;i<25;i++){
            if(block[location_ax][i]==a){
                location_ay=i;
                break;
            }
        }
        for(i=0;i<25;i++){
            if(block[location_bx][i]==-1){
                location_btop=i;
                break;
            }
        }
        /*return stack*/
        for(i=location_ay+1;i<25,block[location_ax][i]!=-1;i++){
            int init_position=block[location_ax][i];
            
            block[init_position][0]=init_position;
            block[location_ax][i]=-1;
        }
        /*move over*/
        block[location_bx][location_btop]=a;
        block[location_ax][0]=location_bx;
        if(location_ay!=0)
            block[location_ax][location_ay]=-1;

        
    }
}
void PileOnto(int block[][25], int a, int b)
{
    if(CheckCommand(block, a, b)){
        int i,j;
        int location_ax=block[a][0],location_ay;
        int location_bx=block[b][0],location_by;
        /*find position*/
        for(i=0;i<25;i++){
            if(block[location_ax][i]==a){
                location_ay=i;
                break;
            }
        }
        for(i=0;i<25;i++){
            if(block[location_bx][i]==b){
                location_by=i;
                break;
            }
        }
        location_by++;
        /*return stack*/
        for(i=location_by;i<25,block[location_bx][i]!=-1;i++){
            int init_position=block[location_bx][i];
            
            block[init_position][0]=init_position;
            block[location_bx][i]=-1;
        }
        /*pile onto*/
        for(i=location_ay;i<25,block[location_ax][i]!=-1;i++){
            int init_position=block[location_ax][i];
            
            block[location_bx][location_by]=block[location_ax][i];
            block[location_ax][i]=-1;
            block[init_position][0]=location_bx;
            location_by++;
        }
    }
}
void PileOver(int block[][25], int a, int b)
{
    if(CheckCommand(block, a, b)){
        int i,j;
        int location_ax=block[a][0],location_ay;
        int location_bx=block[b][0],location_btop;
        /*find position*/
        for(i=0;i<25;i++){
            if(block[location_ax][i]==a){
                location_ay=i;
                break;
            }
        }
        for(i=0;i<25;i++){
            if(block[location_bx][i]==-1){
                location_btop=i;
                break;
            }
        }
        /*pile over*/
        for(i=location_ay;i<25,block[location_ax][i]!=-1;i++){
            int init_position=block[location_ax][i];
            block[location_bx][location_btop]=block[location_ax][i];
            block[init_position][0]=location_bx;
            block[location_ax][i]=-1;
            location_btop++;
        }
    }
}
void PrintBlock(int block[][25],int num)
{
    int i,j;
    
    for(i=0;i<num;i++){
        printf("%d:",i);
        if(block[i][0]==i)
            printf(" %d",block[i][0]);
        for(j=1;j<25,block[i][j]!=-1;j++){
            printf(" %d",block[i][j]);
        }
        printf("\n");
    }
}
int main(void)
{
    /* use two-dimensioned array to simulate block and its stack*/
    int block[25][25];
    int num;
    int a,b;
    int i,j;
    char command[10]={'\0'};
    char command1[10]={'\0'};
    /*initailize block*/
    for(i=0;i<25;i++){
        for(j=0;j<25;j++){
            block[i][j]=-1;
        }
    }
    for(i=0;i<25;i++){
        block[i][0]=i;
    }
    /*execute the function*/
    while(scanf("%d",&num)!=-1){
        scanf("%s",command);
        while(strcmp(command,"quit")!=0){
            scanf("%d%s%d",&a,command1,&b);
            
            if(strcmp(command,"move")==0){
                if(strcmp(command1,"onto")==0)
                    MoveOnto(block,a,b);
                else
                    MoveOver(block,a,b);
            }
            else{
                if(strcmp(command1,"onto")==0)
                    PileOnto(block,a,b);
                else
                    PileOver(block,a,b);
            }
            scanf("%s",command);
        }
        
        PrintBlock(block,num);
    }

    return 0;
}

Re: 101-Runtime Error, using C

Posted: Sun Jan 30, 2011 1:36 pm
by lacrimosa629
Sorry, but I can't understand your language...

101 Runtime Error C++

Posted: Wed Apr 06, 2011 4:12 pm
by pedrosorio
I have RTE at 0.000s, but it works well for the sample input and all the other inputs I've tried. Does anyone have any hints?

Code: Select all

#include <iostream>
#include <string>
#include <stack>

using namespace std;

stack<int> piles[30];
stack<int> mover;
int loc[30];

void move(int n1) {
	int n;
	while(piles[loc[n1]].top()!=n1) {
		n=piles[loc[n1]].top();
		piles[loc[n1]].pop();
		piles[n].push(n);
		loc[n]=n;
	}
}

void pile(int n1,int n2) {
	int n;
	do {
		n=piles[loc[n1]].top();
		piles[loc[n1]].pop();
		mover.push(n);
	}
	while(n!=n1);
	
	while(!mover.empty()) {
		n=mover.top();
		mover.pop();
		piles[loc[n2]].push(n);
		loc[n]=n2;
	}
}
	

int main() {
	string w1,w2;
	int n1,n2,N;
	cin >> N;
	for(int i=0;i<N;i++) {
		piles[i].push(i);
		loc[i]=i;
	}	
	while(1) {
		cin >> w1 >> n1 >> w2 >> n2;
		if(w1=="quit")
			break;
		if(n1==n2 || loc[n1]==loc[n2])
			continue;
		if(w2=="onto")
			move(n2);
		if(w1=="move") {
			move(n1);
			piles[loc[n1]].pop();
			piles[loc[n2]].push(n1);
			loc[n1]=n2;
		}
		else
			pile(n1,n2);
	}
	for(int i=0;i<N;i++) {
		cout << i << ':';
		while(!piles[i].empty()) {
			cout << ' ' << piles[i].top();
			piles[i].pop();
		}
		cout << endl;
	}
	return 0;
}

Q101: why i got the Runtime error!!

Posted: Fri Jul 15, 2011 7:49 am
by popoyo5168

Code: Select all

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int num[25][25]={-1};
int i,j;
int n;
int back(int x1,int y1)
{
    int temp;
    temp=num[x1][y1];
    num[x1][y1]=-1;
    for(int j=0;j<25;j++)
    {
        if(num[temp][j]==-1)
        {
            num[temp][j]=temp;
            break;
        }
    }
    num[x1][y1]=-1; 
     
}
int find(int a1)
{
    i=0;
    j=0;
    for( j=0;j<n;j++)
    {
        for( i=0;i<n;i++)
        {
            if(num[i][j]==a1) goto first;
        }
    }
    first:
    return i;
    
}
int move_onto(int x,int y)
{
    int temp;
    
          
    find(x);
    temp=j;
    while(num[i][++j]!=-1) back(i,j);
    num[i][temp]=-1;
     
    
    find(y);
    temp=j+1;
    while(num[i][++j]!=-1) back(i,j);
    num[i][temp]=x;
}
   
    
    


int move_over(int x,int y)
{
    int temp;
    
    
    find(x);
    temp=j;
    while(num[i][++j]!=-1) back(i,j);
    num[i][temp]=-1;
    
    find(y);
    while(1) 
    {
        if(num[i][++j]==-1)
        {
            num[i][j]=x;
            break;
        }
    }
    
    
    
}

int pile_onto(int x,int y)
{
    int temp,yx,yy;
    find(y);
    yx=i;
    yy=j;
    while(num[i][++j]!=-1) back(i,j);
    
    find(x);
    while(num[i][j]!=-1)
    {
        num[yx][++yy]=num[i][j];
        num[i][j]=-1;
        j++;
    }
         
}

int pile_over(int x,int y)
{
    int temp,yx,yy;
    find(y);
    while(num[i][++j]==-1)
    {
        yx=i;
        yy=j;
        break;
    }
    
    find(x);
    while(num[i][j]!=-1)
    {
        num[yx][yy++]=num[i][j];
        num[i][j]=-1;
        j++;
    }
    
}
              
main()
{
    int a,b;
    char ch[10];
      
    while(cin>>n)
    {   
        for(int i=0;i<25;i++)
        {
            for(int j=0;j<25;j++)
            {
                num[i][j]=-1;
            }
        }
        for(int i=0;i<25;i++)
        {
            num[i][0]=i;
        }
        
        
        while(scanf(" %c %c %c %c" ,&ch[0],&ch[1],&ch[2],&ch[3]))
        {
              
            if(ch[0]=='q')
            {
                break;
            } 
            scanf(" %c %c %c %c %c %c" ,&ch[4],&ch[5],&ch[6],&ch[7],&ch[8],&ch[9]);
            a=ch[4]-48;
            b=ch[9]-48;
            
            if(ch[4]==ch[9]) continue;
            else if(find(a)==find(b)) continue;
            else if(ch[0]=='m'&&ch[6]=='n') move_onto(a,b);
            else if(ch[0]=='m'&&ch[6]=='v') move_over(a,b);
            else if(ch[0]=='p'&&ch[6]=='n') pile_onto(a,b);
            else if(ch[0]=='p'&&ch[6]=='v') pile_over(a,b);
        }
    
        for(int i=0;i<n;i++)
        {
            cout<<i<<":";
            for(int j=0;num[i][j]!=-1;j++)
            {
                cout<<" "<<num[i][j];
            }
            cout<<endl;
            
        }
        
    }    
        
                           
                           
      


   return 0;
}

PLease give me a help

101 i got wa plz help me

Posted: Mon Aug 15, 2011 4:52 am
by Tusharghosh
//here my code in c

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


int main()
{
long n,x,y,i,j,num1,num2,num3,num4,k=1;
char a[100],b[100];
while(scanf("%ld",&n)==1)
{
long c[50][50];
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
if(j==0)
c[j]=i;
else
c[j]=-1;
while(1)
{
scanf("%s",a);
if(!strcmp(a,"quit"))
break;
scanf("%ld%s%ld",&x,b,&y);


for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
if(c[j]==x)
{
num1=i;num2=j;

}
else if(c[j]==y)
{
num3=i;num4=j;
}


if(x!=y&&num1!=num3&&x<n&&y<n&&x>=0&&y>=0)
{
if((strcmp(a,"move")==0)&&(strcmp(b,"onto")==0))
{
for(i=num2+1;i<n;i++)
if(c[num1]!=-1)
{
c[c[num1]][0]=c[num1];
c[num1]=-1;
}
for(i=num4+1;i<n;i++)
if(c[num3]!=-1)
{
c[c[num3]][0]=c[num3][i];
c[num3][i]=-1;
}


c[num3][num4+1]=c[num1][num2];
c[num1][num2]=-1;
}

else if((strcmp(a,"move")==0)&&(strcmp(b,"over")==0))
{
for(i=num2+1;i<n;i++)
if(c[num1][i]!=-1)
{
c[c[num1][i]][0]=c[num1][i];
c[num1][i]=-1;
}
for(;c[num3][num4+1]!=-1;)
num4++;


c[num3][num4+1]=c[num1][num2];
c[num1][num2]=-1;


}

else if((strcmp(a,"pile")==0)&&(strcmp(b,"onto")==0))
{
for(i=num4+1;i<n;i++)
if(c[num3][i]!=-1)
{
c[c[num3][i]][0]=c[num3][i];
c[num3][i]=-1;
}
for(i=num2;i<n;i++)
{
c[num3][num4+1]=c[num1][num2];
c[num1][num2]=-1;
num4++; num2++;
}

}

else if((strcmp(a,"pile")==0)&&(strcmp(b,"over")==0))
{
for(;c[num3][num4+1]!=-1;)
num4++;

for(i=num4;i<n;i++)
{
c[num3][i+1]=c[num1][num2];
c[num1][num2]=-1;
num2++;

}


}

}
}

if(k>1)
{
printf("\n");
}
k++;

for(i=0;i<n;i++)
{
printf("%ld: ",i);
for(j=0;j<n;j++)
{
if(c[i][j]!=-1)
printf("%ld ",c[i][j]);

else
break;
}
printf("\n");
}

}
return 0;
}

101-The Blocks Problem: Runtime error!!!

Posted: Fri Aug 19, 2011 11:35 am
by sabquat
Help me!!!
where is the problem???? :(

Code: Select all

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

int main()
{
    char cd1[5],cd2[5];
    int **p,n,i,a,b,j,xa,xb,ya,yb;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        p=(int**)malloc(n*sizeof(int*));
        for(i=0;i<n;i++)
        p[i]=(int*)malloc(n*sizeof(int));
        for(i=0;i<n;i++)
        {
            p[i][0]=i;
            for(j=1;j<n;j++)
            p[i][j]=-1;
        }
        while(1)
        {
           scanf("%s",&cd1);
           if(cd1[0]=='q')
           break;
           scanf ("%d",&a);
           scanf("%s",&cd2);
           scanf("%d",&b);
           for(xa=0;xa<n;xa++)
           {
               for(ya=0;ya<n;ya++)
               if(p[xa][ya]==a)
               break;
               if(p[xa][ya]==a)
               break;
           }
           for(xb=0;xb<n;xb++)
           {
               for(yb=0;yb<n;yb++)
               if(p[xb][yb]==b)
               break;
               if(p[xb][yb]==b)
               break;
           }
           if(xa!=xb)
           {
                if(cd1[0]=='m'&&cd2[1]=='n')
                {
                    for(i=yb+1;i<n&&p[xb][i]!=-1;i++)
                    {
                        p[(p[xb][i])][0]=p[xb][i];
                        p[xb][i]=-1;
                    }
                    for(i=ya+1;i<n&&p[xa][i]!=-1;i++)
                    {
                        p[(p[xa][i])][0]=p[xa][i];
                        p[xa][i]=-1;
                    }
                    p[xb][yb+1]=a;
                    p[xa][ya]=-1;
                }
                else if(cd1[0]=='m'&&cd2[1]=='v')
                {
                    for(i=ya+1;i<n&&p[xa][i]!=-1;i++)
                    {
                        p[(p[xa][i])][0]=p[xa][i];
                        p[xa][i]=-1;
                    }
                    for(i=yb+1;i<n&&p[xb][i]!=-1;i++);
                    p[xb][i]=a;
                    p[xa][ya]=-1;
                }
                else if(cd1[0]=='p'&&cd2[1]=='n')
                {
                    for(i=ya+1;i<n&&p[xb][i]!=-1;i++)
                    {
                        p[(p[xb][i])][0]=p[xb][i];
                        p[xb][i]=-1;
                    }
                    for(i=ya;i<n&&p[a][i]!=-1;i++)
                    {
                        p[xb][(i+1)]=p[xa][i];
                        p[xa][i]=-1;
                    }
                }
                else if(cd1[0]=='p'&&cd2[1]=='v')
                {
                    for(i=yb+1;i<n&&p[xb][i]!=-1;i++);
                    for(j=ya;j<n&&p[xa][j]!=-1;j++,i++)
                    {
                        p[xb][i]=p[xa][j];
                        p[xa][j]=-1;
                    }
                }
           }
        }
        for(i=0;i<n;i++)
        {
            printf("%d:",i);
            for(j=0;j<n;j++)
            if (p[i][j]!=-1)
            printf(" %d",p[i][j]);
            printf("\n");
        }
    }
    return 0;
}

[101] Help me for WA

Posted: Tue Oct 25, 2011 5:29 pm
by darkurvivor
I use the following two test inputs to test my code
the output are same as the answer from the following website
http://uvatoolkit.com/problemssolve.php
but UVa Online always give me WA
help me to find out the bug plz!!

input-1

Code: Select all

19 
move 1 onto 0 
move 0 onto 1 
move 0 onto 2 
move 2 onto 1 
move 4 over 5 
move 7 onto 8 
move 9 onto 7 
move 7 over 9 
move 9 over 7 
move 11 over 10 
move 12 over 10 
move 13 over 10 
move 14 over 10 
move 16 over 15 
move 17 over 15 
move 18 over 15 
move 16 onto 14 
pile 17 onto 12 
move 15 over 10 
pile 17 onto 14 
pile 15 over 7 
pile 6 over 5 
pile 3 onto 9 
quit
output-1

Code: Select all

0: 0
1: 1 2
2:
3:
4:
5: 5 4 6
6:
7:
8: 8 7 9 3
9:
10: 10 11 12
11:
12:
13: 13
14: 14 17
15: 15
16: 16
17:
18: 18
input-2

Code: Select all

10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
output-2

Code: Select all

0: 0
 1: 1 9 2 4
 2:
 3: 3
 4:
 5: 5 8 7 6
 6:
 7:
 8:
 9:
here's my code

Code: Select all

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

#define MOVE_ONTO 1
#define MOVE_OVER 2
#define PILE_ONTO 3
#define PILE_OVER 4

int file_input = 0;

typedef struct{
    int x, y, top;
} Position;

Position search(int **block, int size, int target);
Position move_back(int **block, int size, Position dest);
void process(int **block, int size, int src, int dest, int case_type);

int main(){

    char cmdstr[20], cmd1[20],cmd2[20], tmp_f[20];
    char *tmp;
    int i, j, first, second, block_num, quit;
    FILE *pf;

    memset(cmdstr, 0, sizeof(cmdstr));

    if(file_input == 1){
        pf = fopen("input.txt","r");
        fgets(cmdstr, sizeof(cmdstr), pf);
    }
    else
        fgets(cmdstr, sizeof(cmdstr), stdin);

    block_num=atoi(cmdstr);

    if(block_num >= 25 || block_num <= 0)
        return 0;

    int **stack;
    stack = malloc(block_num * sizeof(int*));
    for(i=0 ; i<block_num ; i++)
        stack[i] = malloc(block_num * sizeof(int*));

    for(i=0 ; i<block_num ; i++){
        stack[i][0] = i;
        for(j=1 ; j<block_num ; j++)
            stack[i][j] = -1;
    }

    do{
        memset(cmdstr, 0, sizeof(cmdstr));
        memset(cmd1, 0, sizeof(cmd1));
        memset(cmd2, 0, sizeof(cmd2));
        memset(tmp_f, 0, sizeof(tmp_f));

        if(file_input==1)
            fgets(cmdstr, sizeof(cmdstr), pf);
        else
            fgets(cmdstr, sizeof(cmdstr), stdin);


        tmp = strtok(cmdstr," \n");
        if(tmp != NULL){

            strcpy(cmd1, tmp);
            if( strcmp(cmd1,"quit") != 0){

                tmp = strtok(NULL," ");
                if(tmp != NULL){
                    strcpy(tmp_f, tmp);
                    first = atoi(tmp_f);
                }

                tmp = strtok(NULL," ");
                if(tmp != NULL)
                    strcpy(cmd2, tmp);

                tmp = strtok(NULL," ");
                if(tmp != NULL){
                    strcpy(tmp_f, tmp);
                    second = atoi(tmp_f);
                }

                if(first == second)
                    continue;

                if(strcmp(cmd1, "move") == 0){
                    if(strcmp(cmd2, "onto") == 0)
                        process(stack, block_num, first, second, MOVE_ONTO);
                    if(strcmp(cmd2, "over") == 0)
                        process(stack, block_num, first, second, MOVE_OVER);
                }
                if(strcmp(cmd1, "pile") == 0){
                    if(strcmp(cmd2, "onto") == 0)
                        process(stack, block_num, first, second, PILE_ONTO);
                    if(strcmp(cmd2, "over") == 0)
                        process(stack, block_num, first, second, PILE_OVER);
                }
            }
            else{
                for(i=0 ; i<block_num ; i++){
                    printf("%d:",i);
                    for(j=0 ; j<block_num ; j++){
                        if(stack[i][j]!=-1)
                            printf(" %d", stack[i][j]);
                    }
                    printf("\n");
                }
                quit = 1;
            }
        }
    }while(quit!=1);

    for(i=0 ; i<block_num ; i++)
        free(stack[i]);

    free(stack);

    if(file_input==1)
        fclose(pf);

    return 0;
}

Position search(int **block, int size, int target){
    Position report;
    int i,j,done=0;
    for(i=0 ; i<size ; i++){
        for(j=0 ; j<size ; j++){
            if(block[i][j] == target){
                report.x = i;
                report.y = j;
                done = 1;
            }
            if(block[i][j] == -1){
                report.top = j;
                j = size;

            }
        }
        if(done == 1){
            break;
        }
    }
    return report;
}

Position move_back(int **block, int size, Position dest){
    int i, j, buf;
    i = dest.x;
    j = dest.y+1;
    buf = block[i][j];

    while(buf != -1){
        block[buf][0] = buf;
        block[i][j] = -1;
        j++;
        buf = block[i][j];
    }
    dest.top = dest.y+1;
    return dest;
}


void process(int **block, int size, int src, int dest, int case_type){

    Position p_src, p_dest;
    p_src = search(block, size, src);
    p_dest = search(block, size, dest);

    if(p_src.x != p_dest.x){

        switch(case_type){
            case MOVE_ONTO:{
                p_src = move_back(block, size, p_src);
                p_dest = move_back(block, size, p_dest);
                block[p_dest.x][p_dest.top] = block[p_src.x][p_src.y];
                block[p_src.x][p_src.y] = -1;
                break;
            }
            case MOVE_OVER:{
                p_src = move_back(block, size, p_src);
                block[p_dest.x][p_dest.top] = block[p_src.x][p_src.y];
                block[p_src.x][p_src.y] = -1;
                break;
            }
            case PILE_ONTO:{

                p_dest = move_back(block, size, p_dest);
                int i = p_src.y, j = p_dest.top, quit = 0;
                do{
                    if(block[p_src.x][i]==-1)
                        quit = 1;
                    block[p_dest.x][j] = block[p_src.x][i];
                    block[p_src.x][i] = -1;
                    i++; j++;
                }while(quit != 1);
                break;
            }
            case PILE_OVER:{
                int i = p_src.y, j = p_dest.top, quit = 0;
                do{
                    if(block[p_src.x][i]==-1)
                        quit = 1;
                    block[p_dest.x][j] = block[p_src.x][i];
                    block[p_src.x][i] = -1;
                    i++; j++;
                }while(quit != 1);
                break;
            }

        }
    }

}

[Solved] 101 - The Blocks Problem | WA | C++

Posted: Sat May 12, 2012 5:18 am
by anon_user
Hey guys :)
I just solved problem 100 but now i have a problem. I don't find out why I get the WA.

Code: Select all

#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
using namespace std;

void search(int number, int arr[][25], int num, int pos[2])
{
    int pos_[2] = {0, 0};
    for(int i = 0; i < number; i++)
    {
        for(int j = 0; j < 25; j++)
        {
            if(arr[i][j] == num)
            {
                pos[0] = i;
                pos[1] = j;
            }
        }
    }
}

void ret(int arr[][25], int pos[2])
{
    for(int j = pos[1]+1; j < 25; j++)
    {
        if(arr[pos[0]][j] != -1)
        {
            int int_ret = arr[pos[0]][j];
            arr[pos[0]][j] = -1;
            arr[int_ret][0] = int_ret;
        }
    }
}
int size_(int arr[], int num)
{
    int size = 0;
    for(int i = 0; i < num; i++)
    {
        if(arr[i] != -1)
        {
            size++;
        }
    }
    return size;
}
void fill_(int arr_[], int num)
{
    for(int i = 0; i < num; i++)
    {
        arr_[i] = -1;
    }
}
void transform_(int arr1[], int num, int arr2[])
{
    int counter = 0;
    for(int i = 0; i < num; i++)
    {
        if(arr1[i] != -1)
        {
            arr2[counter] = arr1[i];
            counter++;
        }
    }
}

int output(int number, int arr[][25])
{
    for(int i = 0; i < number; i++)
    {
        string a;
        for(int j = 0; j < 25; j++)
        {
            if(arr[i][j] != -1)
            {
                char b [50];
                sprintf(b," %d", arr[i][j]);
                string c = b;
                a.append(c);
            }
        }
        cout << i << ":" << a << endl;
    }
}


int main()
{
    string str;
    string quit ("quit");
    int number;
    cin >> number;
    int array[number][25];
    for(int i = 0; i < number; i++)
    {
        array[i][0] = i;
        for(int j = 1; j < 25; j++)
        {
            array[i][j] = -1;
        }
    }
    /*array[1][1] = 6;
    array[6][0] = -1;
    array[8][1] = 9;
    array[9][0] = -1;
    array[8][2] = 7;
    array[7][0] = -1;*/
    while(getline(cin, str))
    {
        cout << str << endl;
        if(str.compare(0,4, "move") == 0)
        {
            str.erase (0,5); int num1, num2, op, pos1[2], pos2[2];
            if(str.compare(1,1, " ") == 0) // Zahlen < 10
            {
                num1 = atoi(str.substr(0,1).c_str());
                str.erase (0,2);
            }
            else
            {
                num1 = atoi(str.substr(0,2).c_str());
                str.erase (0,3);
            }
            if(str.compare(0,4, "onto") == 0)
            {
                str.erase (0,5);
                num2 = atoi(str.substr(0,str.size()).c_str());
                op = 1;
            }
            else
            {
                str.erase (0,5);
                num2 = atoi(str.substr(0,str.size()).c_str());
                op = 2;
            }
            search(number, array, num1, pos1);
            search(number, array, num2, pos2);
            if(op == 1)
            {
                ret(array, pos1);
                ret(array, pos2);
                array[pos1[0]][pos1[1]] = -1;
                array[pos2[0]][pos2[1]+1] = num1;
            }
            else
            {
                ret(array, pos1);
                if(num1 == 1 && num2 == 11)
                {
                    //output(number, array);
                    //cout << "--------------------------------" << endl;
                }
                array[pos1[0]][pos1[1]] = -1;
                for(int j = pos2[1]+1; j < 25; j++)
                {
                    if(array[pos2[0]][j] == -1)
                    {
                        array[pos2[0]][j] = num1;
                        break;
                    }
                }
            }
        }
        else if(str.compare(0,4, "pile") == 0)
        {
            str.erase (0,5); int num1, num2, op, pos1[2], pos2[2];
            if(str.compare(1,1, " ") == 0) // Zahlen < 10
            {
                num1 = atoi(str.substr(0,1).c_str());
                str.erase (0,2);
            }
            else
            {
                num1 = atoi(str.substr(0,2).c_str());
                str.erase (0,3);
            }
            if(str.compare(0,4, "onto") == 0)
            {
                str.erase (0,5);
                num2 = atoi(str.substr(0,str.size()).c_str());
                op = 1;
            }
            else
            {
                str.erase (0,5);
                num2 = atoi(str.substr(0,str.size()).c_str());
                op = 2;
            }
            search(number, array, num1, pos1);
            search(number, array, num2, pos2);
            if(op == 1)
            {
                ret(array, pos2);
                int helper[25];
                fill_(helper, 25);
                for(int j = pos1[1]; j < 25; j++)
                {
                    if(array[pos1[0]][j] != -1)
                    {
                        helper[j] = array[pos1[0]][j];
                        array[pos1[0]][j] = -1;
                    }
                }
                int size = size_(helper, 25);
                int helper_[size];
                transform_(helper, 25, helper_);
                int help_counter = 0;
                for(int j = pos2[1]+1; j < 25; j++)
                {
                    if(help_counter != size)
                    {
                        array[pos2[0]][j] = helper_[help_counter];
                        help_counter++;
                    }
                }
            }
            else
            {
                int helper[25];
                fill_(helper, 25);
                for(int j = pos1[1]; j < 25; j++)
                {
                    if(array[pos1[0]][j] != -1)
                    {
                        helper[j] = array[pos1[0]][j];
                        array[pos1[0]][j] = -1;
                    }
                }
                int size = size_(helper, 25);
                int helper_[size];
                transform_(helper, 25, helper_);
                int help_counter = 0;
                for(int j = pos2[1]+1; j < 25; j++)
                {
                    if(array[pos2[0]][j] == -1 && help_counter != size)
                    {
                        
                        array[pos2[0]][j] = helper_[help_counter];
                        help_counter++;
                    }
                }
                /*ret(array, pos2);
                array[pos1[0]][pos1[1]] = -1;
                for(int j = pos2[1]+1; j < 25; j++)
                {
                    if(arr[pos[0]][j] != -1)
                    {
                        array[pos2[0]][j] = num1;
                        break;
                    }
                }*/
            }
        }
        else if(str.compare("quit") == 0)
            output(number, array);
		//cout << str << endl;
		//output(number, array);
        //cout << "---------------" << endl;
    }
    cin.get();
    return 0;
}
Input:

Code: Select all

20
move 7 over 11
move 1 over 12
pile 11 onto 12
move 2 over 12
move 18 onto 19
move 17 over 1
move 16 onto 19
move 16 onto 0
move 4 over 10
move 1 over 10
move 13 onto 14
quit
Output:

Code: Select all

0: 0 16
1:
2:
3: 3
4:
5: 5
6: 6
7:
8: 8
9: 9
10: 10 4 1
11:
12: 12 11 7 2
13:
14: 14 13
15: 15
16:
17: 17
18: 18
19: 19
I tested it with the sample input and some other inputs but everytime I get the right output. I really cannot say where I made a mistake.

Please help me :)

Edit: Found the error. You have to ignore the input if a is in the stack of b :D

101 - The Blocks Problem

Posted: Tue Jun 19, 2012 8:01 am
by @ce
Getting WA...can't understand why...plzz help

Code: Select all

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>

using namespace std;

main()
{
      int n;
      cin>>n;
      vector <int>v[n];
      for(int i = 0;i<n ; i++)
              v[i].push_back(i);
      string str;
      getline(cin,str);
      while(1)
      {
              getline(cin,str);
              if(str.compare("quit") == 0)
                   break;
              int from = 0,to = 0;
              string str1, str2;
              stringstream ss(str);
              ss >> str1;
              ss >> from;
              ss >> str2;
              ss >> to;
              int fx,fy,tx,ty;
              bool ff = 0, ft = 0;
              for(int i = 0;i<n ;i++)
              {
                      for(int j = 0;j<v[i].size();j++)
                      {
                              if(from == v[i][j])
                              {
                                     fx = i;
                                     fy = j;
                                     ff = 1;
                              }
                              if(to == v[i][j])
                              {
                                     tx = i;
                                     ty = j;
                                     ft = 1;
                              }
                              if(ff && ft)
                                    break;
                      }
              }
              if(fx == tx)
                    continue;
              if(str1.compare("move") == 0)
              {
                      if(str2.compare("onto") == 0)
                      {
                              v[tx].insert(v[tx].begin()+ty+1, v[fx][fy]);
                              v[fx].erase(v[fx].begin()+fy);
                      }
                      else
                      {
                             v[tx].insert(v[tx].end(), v[fx][fy]);
                             v[fx].erase(v[fx].begin()+fy);
                      }
              }
              else
              {
                      if(str2.compare("onto") == 0)
                      {
                              v[tx].insert(v[tx].begin()+ty+1, v[fx].begin()+fy,v[fx].end());
                              v[fx].erase(v[fx].begin()+fy,v[fx].end());
                      }
                      else
                      {
                              v[tx].insert(v[tx].end(), v[fx].begin()+fy,v[fx].end());
                              v[fx].erase(v[fx].begin()+fy,v[fx].end());
                      }
              }
      }
      for(int i = 0;i<n;i++)
      {
              printf("%d:", i);
              for(int j = 0;j<v[i].size();j++)
                      printf(" %d", v[i][j]);
              printf("\n");
      }
      //system("pause");
}