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

ditrix
New poster
Posts: 33
Joined: Sat Mar 01, 2003 12:38 am
Location: Paris

Post by ditrix » Tue Mar 18, 2003 2:02 am

A lot of my programs in C++, that received a Runtime Error "Invalid memory reference" were accepted after I have rewrited them without an operator "delete". (nothing other changed).

Can anybody tell me what is the problem?
@+!
DitriX

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post by Adil » Tue Mar 18, 2003 7:15 am

hello.

unsing delete will cause runtime-error, if you are "delete"ing something that doesn't exist. i had to learn it the hard-way. it might be easier to point the mistake out from the code.

not using delete can cause the program to run out of memory. so it's a good idea to know when and how to use "delete".

Celax
New poster
Posts: 1
Joined: Fri Mar 21, 2003 12:25 pm

101 RTE again and always...

Post by Celax » Fri Mar 21, 2003 12:39 pm

Please i need help!
I read all the topics about RTE in 101 and i tried the examples but it always runs.
It doesn't run if the syntax is wrong but it's written "There will be no syntactically incorrect commands. ".
So, here is my program:

C :
#include<stdio.h>

int tab[80][80];
int k;
int a;
int rech(int tab[][80],int m)
{
int i=0;
for(k=0;k<a;k++)
{
for(i=0;i<a;i++)
{
if(tab[k]==m)
break;
}
if(tab[k]==m)
break;
}
return(i);
}

int main()
{
int b,i,j,d,sec,n,chiffre,h;
scanf("%d",&a);
b=getchar();
for(i=0;i<a;i++)
{
tab[0]=i;
}
for(i=0;i<a;i++)
{
for(j=1;j<=a;j++)
{
tab[j]=-1;
}
}
while((b=getchar())!=113)
{
if(b==109)
{
for(i=0;i<4;i++)
{
b=getchar();
}
scanf("%d",&b);

for(i=0;i<3;i++)
{
d=getchar();
}
for(i=0;i<3;i++)
{
sec=getchar();
}
if(d==110)
{/*move a onto b*/
scanf("%d",&d);
if(b>=a || d>=a)
goto base;
chiffre=b;
d=rech(tab,d);
h=k;
b=rech(tab,b);
if(b==d)
goto base;
for(j=k+1;tab[j]!=-1;j++)
{
n=tab[j];
tab[j]=-1;
tab[n][0]=n;
}
for(j=h+1;tab[d][j]!=-1;j++)
{
n=tab[d][j];
tab[d][j]=-1;
tab[n][0]=n;
}
tab[d][h+1]=chiffre;
tab[k]=-1;

}
else
{/*move a over b*/
scanf("%d",&d);
if(b>=a || d>=a)
goto base;
chiffre=b;
d=rech(tab,d);
h=k;
b=rech(tab,b);
if (b==d)
goto base;
for(j=h;tab[d][j]!=-1;j++)
{
}
tab[d][j]=chiffre;
for(j=k+1;tab[j]!=-1;j++)
{
n=tab[j];
tab[j]=-1;
tab[n][0]=n;
}
tab[k]=-1;
}

}
else
{
for(i=0;i<4;i++)
{
b=getchar();
}
scanf("%d",&b);
for(i=0;i<3;i++)
{
d=getchar();
}
for(i=0;i<3;i++)
{
sec=getchar();
}
if(d==118)
{/*pile a over b*/
scanf("%d",&d);
if(b>=a || d>=a)
goto base;
d=rech(tab,d);
h=k;
b=rech(tab,b);
if(b==d)
goto base;
for(j=h;tab[d][j]!=-1;j++)
{
}
for(i=k;tab!=-1;i++)
{
tab[d][i-k+j]=tab;
}
for(i=k;tab[b]!=-1;i++)
{
tab[b]=-1;
}
}
else
{ /*pile a onto b*/
scanf("%d",&d);
if(b>=a || d>=a)
goto base;
chiffre=b;
d=rech(tab,d);
h=k;
b=rech(tab,b);
if(b==d)
goto base;
for(i=h+1;tab[d]!=-1;i++)
{
n=tab[d];
tab[d][i]=-1;
tab[n][0]=n;
}
for(j=k;tab[b][j]!=-1;j++)
{
tab[d][j-k+h+1]=tab[b][j];
tab[b][j]=-1;
}
for(j=k;tab[b][j]!=-1;j++)
{
tab[b][j]=-1;
}
}

}
base:
i=getchar();
}
for(i=0;i<a;i++)
{
printf("%d:",i);
for(j=0;tab[i][j]!=-1;j++)
{
printf(" %d",tab[i][j]);
}
printf("\n");
}
return 0;
}



Thanks

Celax

PS: Sorry about the crumy program, i only started programming recently.

Incognegro
New poster
Posts: 4
Joined: Tue Mar 25, 2003 9:52 pm

100 101 WA in Java

Post by Incognegro » Tue Mar 25, 2003 10:00 pm

I am at a loss as to why my programs that I submitted give WA. I hope its nothing to do with the wierd Java restrictions. Shame that the system cannot tell me what the wrong and right input is !!!!

Here is the code for 100:
[java]
import java.io.IOException;

class Main {

static int countDigits(String s) {
char[] ca = s.toCharArray();
int n = 0;

for(int i = 0; i < ca.length; i++)
if(Character.isDigit(ca)) {
n++;
while(i < ca.length && Character.isDigit(ca[i++]));
i--;
}

return n;
}

static int[] integers(String s) {
char[] ca = s.toCharArray();
int [] ia = new int[countDigits(s)];
char[] buf = new char[ca.length];

for(int i = 0, j = 0, k = 0;; ) {
while(i < ca.length && Character.isDigit(ca))
buf[j++] = ca[i++];

String ss = new String(buf, 0, j); j = 0;
ia[k++] = Integer.decode(ss).intValue();

while(i < ca.length && !Character.isDigit(ca)) i++;
if(i == ca.length) break;
}

return ia;
}

static void parse(String s) {
int[]ia = integers(s);

System.out.print(ia[0]); System.out.print(' ');
System.out.print(ia[1]); System.out.print(' ');

if(ia[0] < ia[1])
cycle(ia[0], ia[1]);
else
cycle(ia[1], ia[0]);
}

static void cycle(int m, int n) {
long max = 0;

for(int i = m; i <= n; i++) {

long j = i;
long len = 1;

while(j != 1) {
j = (j % 2) == 0 ? j/2 :3*j + 1;
len++;
}

if(len > max)
max = len;
}

System.out.println(max);
}

static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}

public static void main(String[] args) {
String s;
while((s = ReadLn(255)) != null) parse(s);
}
}
[/java]

and for 101:

[java]
import java.io.IOException;

// Guido Sohne <guido@sohne.net>

class Main {
private int id;
private int pile;
private int next;

/** static array of blocks. the block world. */
private static Main[] blockWorld;

private Main(int a, int b, int c) {
id = a; pile = b; next = c;
}

private static void createBlockWorld(int numBlocks)
{
blockWorld = new Main[numBlocks];
for(int i = 0; i < numBlocks; i++)
blockWorld = new Main(i, i, -1);
}

private static void printBlockWorld()
{
for(int i = 0; i < blockWorld.length; i++) {
System.out.print(i + ":"); printChain(block(i)); System.out.println();
}
}

private static void printChain(Main b)
{
int i = b.id;

if(b.pile == i) do {
System.out.print(" " + b.id);
b = block(b.next);
} while(b != null && b.pile == i);

}

private static boolean isIllegalMove(Main a, Main b) {
return (a.id == b.id || a.pile == b.pile) ? true : false;
}

private static void moveOnto(Main a, Main b) {
if(isIllegalMove(a, b)) return;

revertAfter(a);
revertAfter(b);

cap(a, b);
}

private static void moveOver(Main a, Main b) {
if(isIllegalMove(a, b)) return;

revertAfter(a);

while(b.next != -1)
b = block(b.next);

cap(a, b);
}

private static void cap(Main a, Main b) {
b.next = a.id; a.next = -1; a.pile = b.pile;
}

private static void breakLinkTo(Main a) {
for(int i = 0; i < blockWorld.length; i++)
if(block(i).next == a.id) {
block(i).next = -1;
break;
}
}

private static void movePile(Main a, int pile) {
do {
a.pile = pile;
a = block(a.next);
} while(a.next != -1);

a.pile = pile;
}

private static void pileOnto(Main a, Main b) {
if(isIllegalMove(a, b)) return;

breakLinkTo(a);
revertAfter(b);

b.next = a.id;
movePile(a, b.pile);
}

private static void pileOver(Main a, Main b) {
if(isIllegalMove(a, b)) return;

breakLinkTo(a);

while(b.next != -1)
b = block(b.next);

b.next = a.id;
movePile(a, b.pile);
}

private static void revertAfter(Main b) {
do {
Main bb = b;
b = block(b.next);
bb.next = -1;
bb.pile = bb.id;
} while(b != null);
}

private static Main block(int id) {
return id == -1 ? null : blockWorld[id];
}

private static boolean parseLine(String s) {
boolean move = false;
boolean onto = false;
int dst = s.indexOf("o", 4) - 1;
int src = -1;

if(s.equals("quit"))
return false;

move = s.charAt(0) == 'm' ? true : false;
onto = s.charAt(dst + 2) == 'n' ? true : false;
src = Integer.decode(s.substring(5, dst)).intValue();
dst = Integer.decode(s.substring(6 + dst)).intValue();

return operation(move, src, onto, dst);
}

private static boolean operation(boolean move, int src, boolean onto, int dst) {
Main bs = block(src);
Main ds = block(dst);

if(move) {
if(onto) {
moveOnto(bs, ds);
}
else {
moveOver(bs, ds);
}
}
else {
if(onto) {
pileOnto(bs, ds);
} else {
pileOver(bs, ds);
}
}

return true;
}


static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}

public static void main(String[] args) {
String s = ReadLn(255);
if(s == null) return;
createBlockWorld(Integer.decode(s).intValue());

while((s = ReadLn(255)) != null)
if(!parseLine(s)) break;

printBlockWorld();
}
}

[/java][/java]

Incognegro
New poster
Posts: 4
Joined: Tue Mar 25, 2003 9:52 pm

additonal information

Post by Incognegro » Tue Mar 25, 2003 10:04 pm

This is really my first ever post and my first submissions to the
contest, so I would appreciate some pointers.

The rules on Java are really wierd, I hate having to change my
code and fold in the wierd subroutines I am importing from a util
class I keep around.

It makes me really think about doing stuff in C from now on. If anyone can help by telling me why in *@&$*@&$ I am getting WA I would be very very much less frustrated!
:evil:

Incognegro
New poster
Posts: 4
Joined: Tue Mar 25, 2003 9:52 pm

rewrote them in C. worked for me!

Post by Incognegro » Sat Mar 29, 2003 11:58 pm

I just ended up rewriting my solutions in C instead of Java. Makes
life much easier in a lot of ways given the wieird Java restrictions.

Seems my solutions were correct and there was something wrong
with Java. All is well except for the damnably high number of rejected
submissions.

There goes my perfect record .... :roll:

Farid Ahmadov
Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan

101. Somebody help. I did'n understand the problem.

Post by Farid Ahmadov » Thu Apr 17, 2003 3:45 pm

move a onto b

where a and b are block numbers, puts block a onto block b after returning any blocks that are stacked on top of blocks a and b to their initial positions

pile a onto b

where a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto block b. All blocks on top of block b are moved to their initial positions prior to the pile taking place. The blocks stacked above block a retain their order when moved.

What does mean the initial position in this problem?
_____________
NO sigNature

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric » Thu Apr 17, 2003 4:17 pm

Suppose there are FIVE blocks.
0---0
1---1
2---2
3---3
4---4
So, the initial position is its number. For instance, the initial position of block 2 is 2.[/list]

User avatar
TangentZ
New poster
Posts: 2
Joined: Thu Apr 17, 2003 6:33 pm
Contact:

Post by TangentZ » Thu Apr 17, 2003 8:32 pm

I also have trouble understanding this problem.

Let's say your world looks like this:

0: 0 4 1 2 3
1:
2:
3:
4:

What should happen when you try "pile 0 onto 1"?

My guess is this:

0:
1: 1 0 4
2: 2
3: 3
4:

But, I'm not sure. :o
Kami no Itte ga ore ni zettai naru!

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok » Thu Apr 17, 2003 10:11 pm

Hello TangentZ, in your case, "pile 0 onto 1" must be ignored. It's explicitly mentioned in the problem-statement.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).

User avatar
TangentZ
New poster
Posts: 2
Joined: Thu Apr 17, 2003 6:33 pm
Contact:

Post by TangentZ » Fri Apr 18, 2003 1:31 am

Yeah, I went back and reread the problem and found that rather
"obscure" line that says if a = b or they are on the same pile, then
ignore the command. :x

Apparently, I also have to handle invalid inputs. Argh! :evil:

I finally got it accepted. :wink:

Woohoo! My first UVA accepted submission! :D
Kami no Itte ga ore ni zettai naru!

Farid Ahmadov
Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan

I am getting crazy of 101

Post by Farid Ahmadov » Sun Apr 20, 2003 5:42 pm

People, programmers, somebody please help me to do this problem. :oops:

I always get WA. Why I don't know.

Maybe you can help me to solve this problem.
Please help as you can.

I am tired of this problem.
It will be better if you give me some tests.

Thank you for reading it. I will wait for your replies. :wink:
_____________
NO sigNature

radzaw
New poster
Posts: 1
Joined: Sun Apr 20, 2003 1:23 pm
Location: Szczecin, PL
Contact:

Post by radzaw » Sun Apr 20, 2003 7:23 pm

on this forum you have lots of examples to this problem (http://acm.uva.es/board/viewtopic.php?t=2315 etc..)

you can take a look at my solution, but judge replies on it : runtime error :)

Farid Ahmadov
Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan

Post by Farid Ahmadov » Sun Apr 20, 2003 10:09 pm

Thank you very much for looking at this topic.
But I have already got AC.
_____________
NO sigNature

JusmanX
New poster
Posts: 1
Joined: Fri May 09, 2003 8:13 am
Contact:

Problem 101-Help~!!

Post by JusmanX » Fri May 09, 2003 8:18 am

I'm wondering if anybody would be kind enough to take a look of my program.

I believe that the program works....at least it compiles successfully and works for all the tests cases I can think of when I use Visual C++.

However, when I submit my program, the online judge gives me an "invalid memory reference", which I don't really know what they mean by that.

The following is my source code, written in C++:

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <assert.h>
#include <string>

#define NOTHING -1
#define GROUND -2
using namespace std;

class Block
{
public:
Block();
Block(int label);
void move_onto(Block& b);
void move_over(Block& b);
void pile_onto(Block& b);
void pile_over(Block& b);
void return_above();
Block* find_topmost();
void print_blockAbove();
void print_status(); //debugging purposes


int blockLabel; //how do i get those variables to be protected??
int blockBelow;
int blockAbove;
Block *nextBlock;
protected:
private:

};

/******************************************************************
class: Block
function name: Block();
input: void
return type: none
purpose: constructor
*******************************************************************/
Block::Block()
{
blockLabel = 0; //set blockLabel = 0(default)
blockBelow = GROUND; //initially sitting on the ground
blockAbove = NOTHING; //and nothing is above it
nextBlock = NULL; //and there's no next Block
}
/******************************************************************
class: Block
function name: Block(int);
input: int label, which indicates the block number
return type: none
purpose: constructor
*******************************************************************/
Block::Block(int label)
{
blockBelow = GROUND; //initially sitting on the ground
nextBlock = NULL; //and the nextBlock is pointing to NULL;
blockAbove = NOTHING; //and nothing is above it
blockLabel = label; //except the block number
}

/******************************************************************
class: Block
function name: print_status()
input: void
return type: void
purpose: a function for debugging purposes only.
prints out the status of a particular block,
including:
1. the current block label
2. the block below it
3. the block above it
*******************************************************************/
void Block::print_status()
{
/* STAGE ONE: PRINT OUT THE CURRENT BLOCK */

cout << "the current block is " << blockLabel << endl; //the current block is:

/* STAGE TWO: PRINT OUT THE PREVIOUS BLOCK */
cout << "and it's sitting on block "; //and is sitting on:

if(blockBelow == GROUND) //if it's sittin on the ground
{
cout << "GROUND" << endl; //say so
}else{ //else
cout << blockBelow << endl; //print out the block number of the one below it
}

/* STAGE THREE: PRINT OUT THE ABOVE BLOCK */

cout << "and the one above it is "; //and the one above it is:
if(blockAbove == NOTHING) //if nothing
{
assert(nextBlock == NULL); //check that the nextBlock is indeed NULL
cout << "NOTHING" << endl; //then say nothin is above it
}else{ //else
assert(nextBlock->blockLabel == blockAbove); //an assertion for debugging only
cout << blockAbove << endl; //print out the blockAbove
}

/* STAGE FOUR: PRINT OUT the nextBlock pointer */

cout << "and the one nextBlock pointer is pointing to: "; //just some cout command
if(nextBlock == NULL) //if nextBlock is pointing to nothin
cout << "NOTHING" << endl; //say so
else //else
cout << nextBlock->blockLabel << endl; //print out the nextBlock's label
}

/******************************************************************
class: Block
function name: print_blockAbove()
input: void
return type: void
purpose: print out the labels of all the blocks above, excluding
the current block
*******************************************************************/

void Block::print_blockAbove()
{
if(nextBlock == NULL) //if nothing is above it
{
assert(blockAbove == NOTHING); //an assertion for debugging purposes; just make sure all the variables are up to date
return; //nothing to print, just return; also the base case
}else{
cout << nextBlock->blockLabel << ' '; //else, print out the nextBlock's label
nextBlock->print_blockAbove(); //and do so recursively until hit the base case
}
}


/******************************************************************
class: Block
function name: return_above();
input: void
return type: void
purpose: return all the above blocks back to ground, and
reset all the variables accordingly
*******************************************************************/
void Block::return_above()
{
if(nextBlock == NULL) //if no nextBlock, nothing to return, do nothing
{
assert(blockAbove == NOTHING); //make sure that all the variables are up to date
return; //so I do nothing, also this is the base case
}else{ //else
blockAbove = NOTHING; //set blockAbove to NOTHING
blockBelow = GROUND; //and set blockBelow to GROUND
nextBlock ->return_above(); //and do so recursively to the nextBlock, until hit the base case
nextBlock = NULL; //and after the above blocks are done recursively, set nextBlock to NULL;
}
return; //done, return
}

/******************************************************************
class: Block
function name: find_topmost();
input: void
return type: Block*
purpose: return a pointer to the topmost of a stack of blocks
*******************************************************************/

Block* Block::find_topmost()
{
if(nextBlock == NULL) //if nothing is above it
{
assert(blockAbove == NOTHING); //making sure all the variables are up to date
return this; //found it; return this block
}else{ //if not the top yet
return nextBlock -> find_topmost(); //keep going recursively
}
}

/******************************************************************
class: Block
function name: move_onto(Block& b);
input: b, type Block, passed in by reference
return type: void
purpose: 1. clear all the blocks above a and b
2. then move a on top of b
*******************************************************************/
void Block::move_onto(Block& b)
{
return_above(); //return all the blocks above this block
b.return_above(); //and also return all the blocks above b
b.blockAbove = blockLabel; //then update b's blockAbove to be the current label
blockBelow = b.blockLabel; //and of course update the blockBelow of hte current to the label of b
b.nextBlock= this; //and update the nextBlock's pointer to point to the current block
}


/******************************************************************
class: Block
function name: move_over(Block& b);
input: b, type Block, passed in by reference
return type: void
purpose: 1. clear all the blocks above a
2. move a on top of b.
*******************************************************************/
void Block::move_over(Block& b)
{
return_above(); //clear all the blocks above a
Block *topmost = b.find_topmost(); //let a pointer to point to the topmost element of b
if(topmost == &b) //if the topmost is already b
return; //done; do nothing and return
else //else
move_onto(*topmost); //move a onto the topmost element
}

/******************************************************************
class: Block
function name: pile_onto(Block& b);
input: b, type Block, passed in by reference
return type: void
purpose: 1. clear all the blocks above b
2. move the whole pile of a onto b
*******************************************************************/
void Block::pile_onto(Block& b)
{
b.return_above(); //clear all the blocks above b
b.blockAbove = blockLabel; //and let b's blockAbove to equal to a's label
blockBelow = b.blockLabel; //and let's a's blockBelow equal to b's label
b.nextBlock= this; //and let b's nextBlock to point to a
}

/******************************************************************
class: Block
function name: pile_over(Block& b);
input: b, type Block, passed in by reference
return type: void
purpose: 1. move the whole pile of a onto top of b
*******************************************************************/
void Block::pile_over(Block & b)
{
Block *topmost = b.find_topmost(); //find the pointer to the topmost block
pile_onto(*topmost); //and move the whole pile onto it
}


class Ground: public Block
{
public:
Ground(int num_blocks);
~Ground();
void print_Ground();
void move_onto(int a, int b);
void move_over(int a, int b);
void pile_onto(int a, int b);
void pile_over(int a, int b);

protected:

private:
Block* ground;
int num_blocks;
};

/*********IMPLEMENTATION OF GROUND CLASS *******/

/******************************************************************
class: Ground
function name: Ground(int);
input: number of blocks, type int
return type: none
purpose: constructor
*******************************************************************/
Ground::Ground(int blocks)
{
ground = new Block[blocks]; //dyanmically allocate the number of blocks

for(int i= 0; i < blocks; i++) //for every element on the ground
{
ground.blockLabel = i; //initialize the block labels
}

num_blocks = blocks; //and also set teh num_blocks to the input
}

/******************************************************************
class: Ground
function name: ~Ground();
input: void
return type: none
purpose: destructor
*******************************************************************/
Ground::~Ground()
{
delete [] ground; //delete the dynamically allocated array of blocks
}


/******************************************************************
class: Ground
function name: print_Ground();
input: void
return type: void
purpose: to print the status of the current ground
*******************************************************************/

void Ground::print_Ground()
{
for(int i=0; i < num_blocks; i++) //for every element on the ground
{
cout << i << ": "; //first of all, print out the element label
if(ground.blockBelow==GROUND) //for all the elements sitting on the ground
{
cout << i << ' '; //prints itself
ground.print_blockAbove(); //and print all the blocks that are above it
}
cout << endl; //this is just to make it look nicer
}
}

/******************************************************************
class: Ground
function name: move_onto(int a, int b);
input: int a, int b; indicating the two block labels
return type: void
purpose: move block a onto block b
*******************************************************************/

void Ground::move_onto(int a, int b)
{
int previousBlock = ground[a].blockBelow; //first of all, keep track of a's previousBlock

ground[a].move_onto(ground); //move a onto b

ground[previousBlock].nextBlock= NULL; //and set a's original previousBlock's nextBlock pointer to NULL

ground[previousBlock].blockAbove = NOTHING; //and remember to update the blockAbove variable too
return;
}

/******************************************************************
class: Ground
function name: move_onto(int a, int b);
input: int a, int b; indicating the two block labels
return type: void
purpose: move block a over block b
*******************************************************************/

void Ground::move_over(int a, int b)
{
int previousBlock = ground[a].blockBelow; //keep track of a's previous block

ground[a].move_over(ground); //move a over b

ground[previousBlock].nextBlock= NULL; //set a's original previousBlock's nextBlock pointer to NULL

ground[previousBlock].blockAbove = NOTHING; //and remember to update the blockAbove variable too
return;
}

/******************************************************************
class: Ground
function name: pile_onto(int a, int b);
input: int a, int b; indicating the two block labels
return type: void
purpose: pile block a onto block b
*******************************************************************/

void Ground::pile_onto(int a, int b)
{
int previousBlock = ground[a].blockBelow; //keep track of a's previousBlock

ground[previousBlock].nextBlock= NULL; //and set a's original previousBlock's nextBlock pointer to NULL

ground[previousBlock].blockAbove = NOTHING; //and remember to update the blockAbove variable too

ground[a].pile_onto(ground); //pile a onto b

return;
}

/******************************************************************
class: Ground
function name: pile_over(int a, int b);
input: int a, int b; indicating the two block labels
return type: void
purpose: pile block a over block b
*******************************************************************/

void Ground::pile_over(int a, int b)
{
int previousBlock = ground[a].blockBelow; //keep track of the previousBlock

ground[previousBlock].nextBlock= NULL; //and set a's original previousBlock's nextBlock pointer to NULL

ground[previousBlock].blockAbove = NOTHING; //and remember to update the blockAbove variable too

ground[a].pile_over(ground); //pile a over b

return;
}




/******************************************************************
class: none
function name: ignore();
input: void
return type: void
purpose: an empty fcn to simply increase readability
*******************************************************************/
void ignore()
{
return;
}

/******************************************************************
class: none
function name: main();
input: void
return type: void
purpose: an empty fcn to simply increase readability
*******************************************************************/
int main()
{
int num_blocks;

cin >> num_blocks; //takes in number of blocks

Ground table(num_blocks); //and initialize a table of that many blocks

string command; //initialize whole bunch of variables that i'll need
string action;
int blockA;
int blockB;

START_OF_WHILE_LOOP:
while(cin >> command) //read in until EOF
{
if(command == "quit") //if command is quit
{
table.print_Ground(); //print out the situation
return 0; //and exit
}else if(command != "move" && command != "pile") //else if invalid command
{
ignore(); //ignore it
goto START_OF_WHILE_LOOP; //restart the while loop
}


cin >> blockA; //reads in the blocks you'd like to move
cin >> action; //and action too
cin >> blockB;


if(command == "move") //if command is move
{
if(action == "onto") //and action is onto
table.move_onto(blockA, blockB); //do this
else if(action == "over") //if action is over
table.move_over(blockA, blockB); //do this
else //any other stuff
ignore(); //just ignore

}else if(command == "pile") //if command is pile
{
if(action == "onto") //and action is onto
table.pile_onto(blockA, blockB); //do this
else if(action == "over") //or if action is over
table.pile_over(blockA, blockB); //do this
else //anything else
ignore(); //just ignore
}else{ //and any other command
ignore(); //simply ignore
}
}
return 0;
}

Post Reply

Return to “Volume 1 (100-199)”