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

hbobenicio
New poster
Posts: 1
Joined: Sat Apr 26, 2008 12:14 am

Re: C compiler used by UVA judge

Post by hbobenicio »

I recommend that who is getting CE to try to compile the code with the -Wall parameter. It shows some hiding warnings that can easyly pass through anyones vigilance. :D

Another usefull abstraction that could be usefull is when you have to use fflush(stdin) (windows) or __fpurge(stdin) (linux)

As I don't know in which SO Uva will run my code, I like to use that abstraction code:

Code: Select all


#ifdef WIN32
    #define CLEAR_BUFFER fflush(stdin)
#else
    #include <stdio_ext.h>
    #define CLEAR_BUFFER __fpurge(stdin)
#endif

So, when I call CLEAR_BUFFER; that you do the job! :D

I Hope that could be usefull!

:D
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: C compiler used by UVA judge

Post by mf »

hbobenicio wrote:Another usefull abstraction that could be usefull is when you have to use fflush(stdin) (windows) or __fpurge(stdin) (linux)
Why would you ever have to use such a code?

fflush is supposed to flush stream's output buffer. There's nothing it can do with an input stream, such as stdin.
So fflush(stdin) is effectively a no-op. Wasted cpu cycles.

And as for __fpurge, its man page says it all:
fpurge(3) man page wrote:Notes:
Usually it is a mistake to want to discard input buffers.
Keep in mind that UVa feeds the input to your program from a file, not keyboard.
hbobenicio wrote:As I don't know in which SO Uva will run my code,
If you meant OS by that, then the answer - it's always been Linux.
abhinavswaroop
New poster
Posts: 3
Joined: Tue May 27, 2008 3:10 pm

plz plz help for problem no. 101 RTE error

Post by abhinavswaroop »

plz see the code its running well on my comp but at every submission its giving a RTE.......


#include<iostream>
using namespace std;

struct node
{
int val;
struct node *next;
};

int main()
{ struct node *temp=NULL,*t1=NULL,*t2=NULL,*t3=NULL,*q1=NULL,*q2=NULL;
struct node *a[25];
string s1,s2;
int n,c,d,k,l;
cin>>n;
for(int i=0;i<n;i++)
{
temp=new node;
temp->val=i;
temp->next=NULL;
a=temp;

}
cin>>s1>>c>>s2>>d;
while(s1!="quit")
{
for(int i=0;i<n;i++)
{
temp= new node;
temp=a;
while(temp!=NULL)
{
if(temp->val==c)
k=i;

if(temp->val==d)
l=i;
temp=temp->next;
}
}
q1= new node;
q2= new node;
q1=a[k];
q2=a[l];
while(q1->val!=c)
q1=q1->next;
while(q2->val!=d)
q2=q2->next;

if(k!=l)
{
t3= new node;
t3->val=q1->val;

if(s1=="move")
{
t3->next=NULL;
if(s2=="onto")
{
t1= new node;
t1=q1->next;
if(t1!=NULL)
a[t1->val]=t1;


t2= new node;
t2=q2->next;
if(t2!=NULL)
a[t2->val]=t2;

q2->next=t3;
}

if(s2=="over")
{

t1= new node;
t1=q1->next;
if(t1!=NULL)
a[t1->val]=t1;

t2= new node;
t2=q2;
while(t2->next!=NULL)
t2=t2->next;


t2->next=t3;

}
}


if(s1=="pile")
{
t3->next=q1->next;
if(s2=="onto")
{
t2= new node;
t2=q2->next;
if(t2!=NULL)
a[t2->val]=t2;
q2->next=t3;
}

if(s2=="over")
{
t2= new node;
t2=q2;
while(t2->next!=NULL)
t2=t2->next;


t2->next=t3;
}

}
q1->next=NULL;
q1->val=26;
}
cin>>s1;
if(s1!="quit")
cin>>c>>s2>>d;
}
cout<<endl;
for(int i=0;i<n;i++)
{
cout<<i<<": " ;
temp= new node;
temp=a;
while(temp!=NULL)
{
if(temp->val!=26)
cout<<temp->val<<" ";
temp=temp->next;
}
cout<<endl;
}
return 0;
system("pause");
}

plz do reply and
if u find error on some input plz tell me the input i will see to it......
plz plz
abhinavswaroop
New poster
Posts: 3
Joined: Tue May 27, 2008 3:10 pm

Re: plz plz help for problem no. 101 RTE error

Post by abhinavswaroop »

here is the error i am getting:

Your submission with number 6449663 for the problem 101 - The Blocks Problem has failed with verdict Runtime error.

This means that the execution of your program didn't finish properly. Remember to always terminate your code with the exit code 0.
dc.
New poster
Posts: 1
Joined: Sun Jun 08, 2008 5:05 pm

Re: Help on Problem 101

Post by dc. »

Hi, I keep getting a runtime error with my java code for problem 101. (despite, that the program logic still has a few bugs)
I wanted to ask you, to look over my code, so that I can get to work on "Wrong Answer" ;)

Code: Select all

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

class Main {

	private ArrayList<LinkedList<Integer>> blocks;
	private static String newline = System.getProperty("line.separator");

	/**
	 * constructor
	 * 
	 * @param n
	 *            number of blocks in the world
	 */
	public Main(int n) {

		blocks = new ArrayList<LinkedList<Integer>>();

		for (int i = 0; i < n; i++) {
			blocks.add(i, new LinkedList<Integer>());

			// initially every block named i is in position i on the table
			blocks.get(i).add(i);
		}

	} // end of constructor

	public void pileOnto(int a, int b) {
		int posA = findBlock(a);
		int posB = findBlock(b);

		// don't do anything if block a already is on block b
		if (posA == posB)
			return;

		// move all blocks on top of block b to their initial positions
		moveAwayUntil(posA, a);

		pileOver(a, b);

	} // end of pileOnto

	public void pileOver(int a, int b) {
		int posA = findBlock(a);
		int posB = findBlock(b);

		// don't do anything if block a already is on block b
		if (posA == posB || posA < 0 || posB < 0)
			return;

		// System.out.println("piling" + blocks.get(posA) + " over to " +
		// blocks.get(posB));

		blocks.get(posB).addAll(blocks.get(posA));
		blocks.get(posA).clear();

	}

	public void moveOver(int a, int b) {
		int posA = findBlock(a);
		int posB = findBlock(b);

		// return all blocks stacked on top of block a to their initial
		// positions
		moveAwayUntil(posA, a);

		// move a onto the stack containing block b
		moveBlock(posA, posB);

	}

	public void moveOnto(int a, int b) {
		int posA = findBlock(a);
		int posB = findBlock(b);

		// don't do anything if block a already is on block b
		if (posA == posB || posA < 0 || posB < 0)
			return;

		// first return any blocks that are stacked on top of blocks a and b to
		// their
		// initial positions
		moveAwayUntil(posA, a);
		moveAwayUntil(posB, b);

		// now move block a onto block b
		moveBlock(posA, posB);

	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		int i = 0;
		for (LinkedList<Integer> l : blocks) {
			sb.append(i).append(":").append(printList(l)).append(newline);
			i++;
		}

		return sb.toString();
	}

	private String printList(LinkedList<Integer> l) {
		StringBuilder sb = new StringBuilder();

		if (l.isEmpty())
			return "";

		sb.append(" ");
		for (Integer i : l)
			sb.append(i).append(" ");

		return sb.toString();

	} // end of printList

	public void quit() {
		System.out.println(this);

	}

	private void moveBlock(int pos, int newPos) {
		int tmpBlock = blocks.get(pos).removeLast();
		blocks.get(newPos).addLast(tmpBlock);
	}

	/**
	 * returns all blocks on "top" of searchBlock to their initial positions, so
	 * that searchBlock then has no block on top
	 * 
	 * @param pos -
	 *            list in blocks array
	 * @param searchBlock -
	 *            block to move free
	 */
	private void moveAwayUntil(int pos, int searchBlock) {
		int tmpBlock = -1;

		tmpBlock = blocks.get(pos).getLast();

		while (tmpBlock != searchBlock) {
			tmpBlock = blocks.get(pos).removeLast();

			// move tmpBlock to its original stack
			if (tmpBlock != searchBlock) {
				blocks.get(tmpBlock).addLast(tmpBlock);
			}
		} // end of while

	} // end of moveAwayUntil

	/**
	 * @param searchBlock
	 * @return the position of the list in the blocks array, in which the
	 *         searchBlock is contained.
	 */
	private int findBlock(int searchBlock) {
		int i = 0;

		for (i = 0; i < blocks.size(); i++) {
			if (blocks.get(i).contains(searchBlock))
				return i;
		}

		return -1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		int numberOfBlocks = in.nextInt();
		in.nextLine();

		Main myWorld = new Main(numberOfBlocks);

		while (in.hasNext()) {

			String cmd1 = in.next();
			if (cmd1.equals("quit")) {
				myWorld.quit();
				System.exit(0);
			}

			int blockA = in.nextInt();
			String cmd2 = in.next();
			int blockB = in.nextInt();
			in.nextLine();

			if (!(blockA == blockB)) {

				if (cmd1.equals("move")) {
					if (cmd2.equals("onto"))
						myWorld.moveOnto(blockA, blockB);

					if (cmd2.equals("over"))
						myWorld.moveOver(blockA, blockB);

				}
				if (cmd1.equals("pile")) {
					if (cmd2.equals("onto"))
						myWorld.pileOnto(blockA, blockB);
					if (cmd2.equals("over"))
						myWorld.pileOver(blockA, blockB);
				}
			}

		} // end of while

		myWorld.quit();
		System.exit(0);
	} // end of main
} // end of class BlocksWorld
sachin1984
New poster
Posts: 1
Joined: Mon Jul 14, 2008 10:02 am

WA in Problem 101

Post by sachin1984 »

My code gives correct output for all the sample test cases given, but still i m getting WA, I will be very grateful if somebody helps me out with this: My code follows

#include <stdio.h>
#include <string.h>
static int row;
static int col;

int getAction()
{
int result;
int num;
int m, i = 0;
char action[25];
char character;
memset(action, 0x0, 25);
while(1)
{
scanf("%c", &character);
if(character == '\n')
break;
action[i++] = character;
}
action = '\0';
row = 0;
col = 0;
if(action[0] == 'q')
return 4;
if(action[0] == 'm')
result = 0;
else
result = 1;
m = 0;
while(m < 25 && action[m++] != ' ');

while(m < 25 && action[m] != ' ')
{
num = action[m] - 48;
row = 10 * row + num;
m++;
}
m++;
if(action[m + 1] == 'v')
{
if(result == 1)
result += 2;
else
result += 1;
}
else
{
if(result == 1)
result += 1;
}

while(m < 25 && action[m++] != ' ');

while(m < 25 && action[m] != '\0')
{
num = action[m] - 48;
col = 10 * col + num;
m++;
}
return result;

}

int main()
{
unsigned i, j, k, k1, cp, cp1;
int array[35][35];
unsigned num_of_blocks;
int action;
char temp;
int rowc = -1;
int colc = -1;
int prow;
int flag = 1;

scanf("%u", &num_of_blocks);

for(i = 0; i < 35; ++i)
{
for(j = 0; j < 35; ++j)
array[j] = -26;
array[0] = i;
}
scanf("%c", &temp);

while(flag == 1)
{
rowc = -1;
colc = -1;
action = getAction();

/* storing original start block in a temp varaible */
prow = row;

/* Finding the destination block */
k = 1;
if(array[col][0] < 0)
{
colc = -1 * (array[col][0]);
if (colc > 25) colc = 0;
while(k < num_of_blocks && array[colc][k++] != col);
col = colc;
}

/* Finding the starting block */
j = 1;
if(array[row][0] < 0)
{
rowc = -1 * (array[row][0]);
if (rowc > 25) rowc = 0;
while(j < num_of_blocks && array[rowc][j++] != row );
row = rowc;

}

/* if start and destination are in the same stack, ignore the action */
if(col == row && action != 4)
continue;

switch(action)
{
case 0: /* MOVE -- ONTO */
cp = k;
/* Putting blocks over destination to their initial position */
while(k < num_of_blocks && array[col][k] > -26)
{
array[array[col][k]][0] = array[col][k];
array[col][k] = -26;
k++;
}

cp1 = j - 1;
/* Putting blocks over source to their initial position */
while(j < num_of_blocks && array[row][j] > -26)
{
array[array[row][j]][0] = array[row][j];
array[row][j] = -26;
j++;
}

array[col][cp] = prow;
if(cp1 != 0)
array[row][cp1] = -26;

if (col == 0) col = 50;
array[prow][0] = -1 * (col);
break;


case 1: /* MOVE -- OVER */
/* Getting to the top of destination block stack */
while(k < num_of_blocks && array[col][k++] > -26);
k--;

cp1 = j - 1;
/* Putting blocks over source to their initial position */
while(j < num_of_blocks && array[row][j] > -26)
{
array[array[row][j]][0] = array[row][j];
array[row][j] = -26;
j++;
}

array[col][k] = prow;
if(cp1 != 0)
array[row][cp1] = -26;

if (col == 0) col = 50;
array[prow][0] = -1 * (col) ;
break;


case 2: /* PILE -- ONTO */
k1 = k;
/* Putting blocks over destination to their initial position */
while(k1 < num_of_blocks && array[col][k1] > -26)
{
array[array[col][k1]][0] = array[col][k1];
array[col][k1] = -26;
k1++;
}

j--;
/* Putting source blocks and blocks over it on top of destination block */
while(j < num_of_blocks && array[row][j] > -26)
{
array[col][k++] = array[row][j];
if (col == 0) col = 50;
array[array[row][j]][0] = -1 * (col);
if(j != 0)
array[row][j] = -26;
j++;
}

break;

case 3: /* PILE -- OVER */

/* Getting to the top of destination block stack */
while(k < num_of_blocks && array[col][k++] > -26);
k--;

j--;
/* Putting source blocks and blocks over it on top of destination block */
while(j < num_of_blocks && array[row][j] > -26)
{
array[col][k++] = array[row][j];
if (col == 0) col = 50;
array[array[row][j]][0] = -1 * col;
if(j != 0)
array[row][j] = -26;
j++;
}

break;

case 4:
/*Displaying the blocks final positions */
for(i = 0; i < num_of_blocks; ++i)
{
j = 0;
printf("%d:", i);
while(array[j] >= 0)
printf(" %d",array[j++]);
printf("\n");
}
flag = 0;
break;
}
}
return 0;
}
silverlining
New poster
Posts: 4
Joined: Wed Apr 15, 2009 9:41 am

101 - RE (java)

Post by silverlining »

My code works perfectly fine on my computer, but seems to cause runtime errors on this website. I already tried searching other topics but none were helpful.

Code: Select all

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringTokenizer data;

        String input = in.nextLine();
        int aSize = Integer.parseInt(input);
        Vector piles[] = new Vector[aSize];

        for (int i = 0; i < aSize; i++) {
            piles[i] = new Vector();
            piles[i].addElement(i);
        }

        int a = 0, b = 0;
        String op1 = "", op2 = "";
        input = in.nextLine();

        while (!input.equals("quit")) {

            data = new StringTokenizer(input);
            op1 = data.nextToken();
            a = Integer.parseInt(data.nextToken());
            op2 = data.nextToken();
            b = Integer.parseInt(data.nextToken());

            if (op1.equals("move")) {
                if (op2.equals("onto")) {
                    for (int i = 0; i < aSize; i++) {
                        if (piles[i].contains(a)) {
                            for (int j = 0; j < aSize; j++) {
                                if (piles[j].contains(b)) {
                                    if (a == b) {
                                        break;
                                    }
                                    piles[i].removeElement(a);
                                    piles[j].insertElementAt(a, piles[j].indexOf(b) + 1);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                } else if (op2.equals("over")) {
                    for (int i = 0; i < aSize; i++) {
                        if (piles[i].contains(a)) {
                            for (int j = 0; j < aSize; j++) {
                                if (piles[j].contains(b)) {
                                    piles[j].addElement(a);
                                    piles[i].removeElement(a);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            } else if (op2.equals("onto")) {
                for (int i = 0; i < aSize; i++) {
                    if (piles[i].contains(a)) {
                        for (int j = 0; j < aSize; j++) {
                            if (piles[j].contains(b)) {
                                int shift = 1;
                                for (int k = piles[i].indexOf(a); k != piles[i].size();) {
                                    piles[j].insertElementAt(piles[i].get(k), piles[j].indexOf(b));
                                    piles[i].removeElementAt(k);
                                    shift++;
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            } else if (op2.equals("over")) {
                for (int i = 0; i < aSize; i++) {
                    if (piles[i].contains(a)) {
                        for (int j = 0; j < aSize; j++) {
                            if (piles[j].contains(b)) {
                                if (i == j) {
                                    break;
                                }
                                for (int k = piles[i].indexOf(a); k != piles[i].size();) {
                                    piles[j].addElement(piles[i].get(k));
                                    piles[i].removeElementAt(k);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }

            input = in.nextLine();
        }

        in.close();

        for (int i = 0; i < aSize; i++) {
            System.out.print(i + ":");
            for (int j = 0; j < piles[i].size(); j++) {
                System.out.print(" " + piles[i].get(j));
            }
            System.out.print("\n");
        }


    }
}
silverlining
New poster
Posts: 4
Joined: Wed Apr 15, 2009 9:41 am

Re: 101 - RE (java)

Post by silverlining »

Changed the code around a bit. Still can't get past the runtime error result on my submission. What could possibly be wrong? Can RTE mean exceptions were thrown?

Code: Select all

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringTokenizer data;

        String input = in.nextLine();
        int aSize = Integer.parseInt(input);
        LinkedList piles[] = new LinkedList[aSize];

        for (int i = 0; i < aSize; i++) {
            piles[i] = new LinkedList();
            piles[i].add(i);
        }

        int a = 0, b = 0;
        String op1 = "", op2 = "";
        input = in.nextLine();

        while (!input.equals("quit")) {

            data = new StringTokenizer(input);
            op1 = data.nextToken();
            a = Integer.parseInt(data.nextToken());
            op2 = data.nextToken();
            b = Integer.parseInt(data.nextToken());

            if (op1.equals("move")) {
                if (op2.equals("onto")) {
                    for (int i = 0; i < aSize; i++) {
                        if (piles[i].contains(a)) {
                            for (int j = 0; j < aSize; j++) {
                                if (piles[j].contains(b)) {
                                    if (a == b) {
                                        break;
                                    }
                                    piles[i].remove(piles[i].indexOf(a));
                                    piles[j].add(piles[j].indexOf(b) + 1, a);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                } else if (op2.equals("over")) {
                    for (int i = 0; i < aSize; i++) {
                        if (piles[i].contains(a)) {
                            for (int j = 0; j < aSize; j++) {
                                if (piles[j].contains(b)) {
                                    piles[j].add(a);
                                    piles[i].remove(piles[i].indexOf(a));
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            } else if (op2.equals("onto")) {
                for (int i = 0; i < aSize; i++) {
                    if (piles[i].contains(a)) {
                        for (int j = 0; j < aSize; j++) {
                            if (piles[j].contains(b)) {
                                int aIndex = piles[i].indexOf(a);
                                for (int blocks = piles[i].size() - piles[i].indexOf(a); blocks > 0; blocks--) {
                                    piles[j].add(piles[j].indexOf(b), piles[i].get(aIndex));
                                    piles[i].remove(aIndex);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            } else if (op2.equals("over")) {
                for (int i = 0; i < aSize; i++) {
                    if (piles[i].contains(a)) {
                        for (int j = 0; j < aSize; j++) {
                            if (piles[j].contains(b)) {
                                if (i == j) {
                                    break;
                                }
                                for (int k = piles[i].indexOf(a); k != piles[i].size();) {
                                    piles[j].add(piles[i].get(k));
                                    piles[i].remove(k);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }

            input = in.nextLine();
        }

        in.close();

        System.out.print(0 + ":");
        for (int i = 1; i < aSize; i++) {
            for (int j = 0; j < piles[i].size(); j++) {
                System.out.print(" " + piles[i].get(j).toString().trim());
            }
            System.out.print("\n" + i + ":");
        }
    }
}
silverlining
New poster
Posts: 4
Joined: Wed Apr 15, 2009 9:41 am

101 RTE

Post by silverlining »

My code works perfectly fine on my computer, but seems to cause runtime errors for problem set 101. I've spent hours tearing my hair out over this, please help. >_<

Code: Select all

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringTokenizer data;

        String input = in.nextLine();
        int aSize = Integer.parseInt(input);
        LinkedList piles[] = new LinkedList[aSize];

        for (int i = 0; i < aSize; i++) {
            piles[i] = new LinkedList();
            piles[i].add(i);
        }

        int a = 0, b = 0;
        String op1 = "", op2 = "";
        input = in.nextLine();

        while (!input.equals("quit")) {

            data = new StringTokenizer(input);
            op1 = data.nextToken();
            a = Integer.parseInt(data.nextToken());
            op2 = data.nextToken();
            b = Integer.parseInt(data.nextToken());

            if (op1.equals("move")) {
                if (op2.equals("onto")) {
                    for (int i = 0; i < aSize; i++) {
                        if (piles[i].contains(a)) {
                            for (int j = 0; j < aSize; j++) {
                                if (piles[j].contains(b)) {
                                    if (a == b) {
                                        break;
                                    }
                                    piles[i].remove(piles[i].indexOf(a));
                                    piles[j].add(piles[j].indexOf(b) + 1, a);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                } else if (op2.equals("over")) {
                    for (int i = 0; i < aSize; i++) {
                        if (piles[i].contains(a)) {
                            for (int j = 0; j < aSize; j++) {
                                if (piles[j].contains(b)) {
                                    piles[j].add(a);
                                    piles[i].remove(piles[i].indexOf(a));
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            } else if (op2.equals("onto")) {
                for (int i = 0; i < aSize; i++) {
                    if (piles[i].contains(a)) {
                        for (int j = 0; j < aSize; j++) {
                            if (piles[j].contains(b)) {
                                int aIndex = piles[i].indexOf(a);
                                for (int blocks = piles[i].size() - piles[i].indexOf(a); blocks > 0; blocks--) {
                                    piles[j].add(piles[j].indexOf(b), piles[i].get(aIndex));
                                    piles[i].remove(aIndex);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            } else if (op2.equals("over")) {
                for (int i = 0; i < aSize; i++) {
                    if (piles[i].contains(a)) {
                        for (int j = 0; j < aSize; j++) {
                            if (piles[j].contains(b)) {
                                if (i == j) {
                                    break;
                                }
                                int aIndex = piles[i].indexOf(a);
                                for (int blocks = piles[i].size() - piles[i].indexOf(a); blocks > 0; blocks--) {
                                    piles[j].add(piles[i].get(aIndex));
                                    piles[i].remove(aIndex);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }

            input = in.nextLine();
        }

        in.close();

        for (int i = 0; i < aSize; i++) {
            System.out.print(i + ":");
            for (int j = 0; j < piles[i].size(); j++) {
                System.out.print(" " + piles[i].get(j));
            }
            System.out.print("\n");
        }
    }
}
rsala004
New poster
Posts: 1
Joined: Thu Nov 26, 2009 7:58 am

RE 101 blocks, need help getting started submitting

Post by rsala004 »

What is wrong with how I am taking input ( the output should be correct though ), i receive runtime error...

just focus on my input loop, ignore other code

Code: Select all

int main()
{
vector<string> *instr = new vector<string>;
	string input="";
	while(input.compare("quit"))
	{
		getline(cin,input);
		instr->push_back(input);
	}


        // possibly irrelevant code proceeding 
        starter->number=0;
	for(int i=1;i< atoi((instr->at(0)).c_str()) ;++i)
		addNodeRight(i);

	for(int i=1; i < instr->size()-1;++i)
	{
		callMethod(instr->at(i));
	}

	printBlocks();
	cout<<endl;

	return 0;
}
THANK YOU!
sysabod
New poster
Posts: 2
Joined: Mon Jun 07, 2010 6:04 pm

101 get a TLE, seeking help

Post by sysabod »

dear all:
here is my code. i still have no idea why i got TLE error,do i really need to change my data structures for speed's sake

Code: Select all

#include <vector>
#include <utility>
#ifndef ONLINE_JUDGE
#include <fstream>
std::ifstream cin("data.in");
std::ofstream cout("data.in.out")
#else
#include <iostream>

#endif

using namespace std;

typedef vector<vector<int> > TWO_DIM_Arr;
TWO_DIM_Arr blocks;
vector<pair<int,int> > position; //first for nStack,second for nOffset

void returnOnTopOf(int block){
	int nStack = position[block].first;
	int nOffSet = position[block].second;

	for(int i= nOffSet+1; i < blocks[nStack].size(); i++){
		int data = blocks[nStack][i];
		blocks[data].push_back(data);
		position[data] = make_pair(data,0);
	}
	blocks[nStack].resize(nOffSet+1);
}
void merge_stack(int source, int target)
{
   int nSourceStack = position[source].first;
   int nSourceOffset = position[source].second;
   int nTargetStack = position[target].first;
   for(int i = nSourceOffset; i < blocks[nSourceStack].size();i++){
	   int data = blocks[nSourceStack][i];

	   position[data] = make_pair(nTargetStack,
				   blocks[nTargetStack].size());
	   blocks[nTargetStack].push_back(data);
   }
   blocks[nSourceStack].resize(nSourceOffset);
}

int main(int argc,char** argv)
{
	int nBlock = 0,source =0,target = 0;
	string command,type;

	cin>>nBlock;
	blocks.resize(nBlock);
	position.resize(nBlock);

	for(int i = 0;i < nBlock;++i){
		blocks[i].push_back(i);
		position.push_back(make_pair(i,0));
	}

	while(true){
		cin>>command>>source>>type>>target;
		if(source == target ||
			position[source].first == position[target].first){
			continue;
		}
		if(command[0] == 'q') break;

		switch(type[1]){
		case 'n':
			returnOnTopOf(target);
			break;
		case 'v':
			break;
		default:
			break;
		}
		switch(command[0]){
		case 'm':
			returnOnTopOf(source);
			break;
		case 'p':
			break;
		default:
			break;
		}
		merge_stack(source,target);
	}
	for(int i = 0 ; i<blocks.size();i++){
		cout<<i<<":";
		if(blocks[i].size()!=0){
			for(int j = 0; j < blocks[i].size();j++)
				cout<<" "<<blocks[i][j];
		}
		cout<<endl;
	}
	return 0;
}
any help or pointers is well appreciated !
nermeen
New poster
Posts: 1
Joined: Fri Jul 23, 2010 12:07 am

WA in 101 help please

Post by nermeen »

This my code
i got wrong answer I donno what's the problem with it if any one can help with a wrong test case It would be good

Code: Select all

#include<iostream>
#include<vector>
#include<string>
#include<sstream>

using namespace std;

int main()
{
	int size;
    cin>>size;
    vector< vector<int> > vec(size);
    string s1,s3, s2,s4;
    int m,n,p,q,a,b;
    
    
    
    for(int i = 0;i <size;i++ )
    {
		   vec[i].push_back(i);
    }
    string h = "";
    getline(cin,h);
    while(h != "quit")
     {
        getline(cin,h);
           if(h == "quit")
        {
           break;
        }
        istringstream str(h);
        str>>s1>>s2>>s3>>s4;
        cout<<" s1 = "<<s1<<"  s3 ="<<s3<<endl;
        stringstream ss;
        ss<<s2;
        ss>>a;
        ss.clear();
        ss<<s4;
        ss>>b;
        if(s1 == "move" && s3 == "onto")//case1
        {
			  cout<<"      be5       "<<endl;
              for(int i = 0;i<vec.size();i++)
              {
                for(int j = 0;j<vec[i].size();j++)
                {
                        if(vec[i][j] == a)
                        {
                                     m = i;
                                     n = j;
                        }
                        if(vec[i][j] == b)
                        {
                                     p=i;
                                     q=j;

                        }
				}
			 }
                        if(p != m)
                        {
                             int o = n;
                             int h =q;
                             for(int k = vec[m].size()- 1; k > o ;k --)
                             {
                                             vec[vec[m][k]].push_back(vec[m][k]);
                                             vec[m].pop_back();
                             }
                              for(int l = vec[p].size() - 1; l > h + 1;l--)
                              {
                                             vec[vec[p][l]].push_back(vec[p][l]);
                                              vec[p].pop_back();
                              }
                              vec[m].pop_back();
                              vec[p].push_back(a);//a fo2 b
                        }
                        else
                              break;
        } //end of case1
        else if(s1 == "move" && s3 == "over") //case2
        {
            for(int i = 0;i < size;i++)
              {
                for(int j = 0;j < vec[i].size();j++)
                {
                        if(vec[i][j] == a)
                        {
                                     m= i;
                                     n=j;

                        }
                        if(vec[i][j] == b)
                        {
							p = i;
							q = j;
						}
				}
              }
								     if(p != m)
                                     {
                                         for(int k = vec[m].size()-1; k > n;k --)
                                         {
                                             vec[vec[m][k]].push_back(vec[m][k]);
                                             vec[m].pop_back();
                                         }
                                         vec[m].pop_back();
                                         vec[p].push_back(a); 
                                     }
                                     else
                                         break;
                        
              
        }//end of case2
       else  if(s1 == "pile" && s3 == "onto")//case3
        {
               for(int i = 0;i<size;i++)
               {
                for(int j = 0;j<vec[i].size();j++)
                {

                        if(vec[i][j] == b)//b
                        {
                                     p=i;
                                     q=j;
                        }
                         if(vec[i][j] == a)
                        {
                                     m= i;
                                     n=j;
						}
                }
              }
                                     if(p != m)
                                     {
                                        for(int l =vec[p].size()-1 ; l > q;l--)
                                        {
                                             vec[vec[p][l]].push_back( vec[p][l]);
                                             vec[p].pop_back();
                                        }
                                        for(int l = n;l < vec[m].size();l++)
                                        {
                                          vec[p].push_back(vec[m][l]);
                                        }
                                        for(int o= vec[m].size()-1;o>=n;o--)
                                        {
                                           vec[m].pop_back();
                                        }
                                      }
                                      else
                                         break;
                        
              
        }//endof case3
        else if(s1 == "pile" && s3 == "over")//case4
        {
             for(int i = 0;i<size;i++)
              {
                for(int j = 0;j<vec[i].size();j++)
                {
                        if(vec[i][j] == a)
                        {
                                     m= i;
                                     n=j;
                        }
                        if(vec[i][j] == b)
                        {
                                     p=i;
                                     q=j;
						}
				}
			}
                                     if(p != m)
                                     {
                                        for(int l = n; l <vec[m].size();l++)
                                        {
                                             vec[p].push_back(vec[m][l]);//a fo2 b
                                         }
                                        for(int k = vec[m].size()-1;k >= n;k--)
                                       {
                                        vec[m].pop_back();
                                        }
                                     }
			
        }   //end of case4
        
     }//while end*/
for(int i = 0;i <size;i++ )
{
           cout<<i<<":";
           for(int j = 0;j < vec[i].size();j ++ )
            {
                cout<<" "<<vec[i][j];
            }
            cout<<endl;
}
  //  system("pause");
    return 0;
}
feysal
New poster
Posts: 5
Joined: Sun Aug 15, 2010 3:53 am

[SOLVED]101- Runtime Error

Post by feysal »

Hello,
I got Runtime Error on the problem 101. I read some posts related to the topics on this board and try to make the changes that they suggest and run the inputs i found there, it works fine on my computer but when uploaded on UVa, i got Runtime Error (8 times).
This is the email i receive
Hi,
This is an automated response from UVa Online Judge.
Your submission with number 8180799 for the problem 101 - The Blocks Problem has failed with verdict Runtime error.
This means that the execution of your program didn't finish properly. Remember to always terminate your code with the exit code 0.
Best regards,
The UVa Online Judge team
That's my code

Code: Select all

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

#define N 25
#define NIL -1

typedef struct node
{
    int data;
    struct node *next;
} list;

void init(list **l)
{
    *l = NULL;
    struct node *sentinel = malloc(sizeof(struct node));
    sentinel->data = NIL;
    sentinel->next = *l;
    *l = sentinel;
}

void push(int data, struct node  **n)
{
    struct node *new = malloc(sizeof(struct node));
    new->data = data;
    new->next = *n;
    *n = new;
}

void insert(int data, list **l)
{
    struct node *temp = *l;
    push(data, &(temp->next));
}

void print(list *l)
{
    struct node *temp = l;
  
    while((temp = temp->next) != NULL)
          printf(" %d", temp->data);
  
    printf("\n");
}

void free_list(list **l)
{
    struct node *temp = *l, *next;
    while(temp != NULL)
    {
        next = temp->next;
        free(temp);
        temp = next;
    }
    *l = NULL;
}

/* Returns the index in "array" of the list where "data" is stored and assign "ptr" to the node preceding "data" */
int search(struct node **ptr, int data, list *array[])
{
    int i = 0;
    int found = 0;

    while(i < N && !found)
    {
        struct node *temp = array[i];
        while(temp != NULL)
        {
            if(temp->data == data)
            {
                found = 1;
                break;
            }
            *ptr = temp;
            temp = temp->next;
        }
        i++;
    }

    return i - 1;
}

void move_onto(int a, int b, list *array[])
{
    struct node *pa = NULL, *pb = NULL;
    struct node *tempa = NULL, *tempb = NULL;
    int i = search(&pa, a, array);
    int j = search(&pb, b, array);

    if(i != j) // If i == j then a and b are stored in the same list
    {
        struct node *temp = NULL;
        tempa = pa->next;
        tempb = pb->next;
        temp = tempb->next;
        pa->next = tempa->next;
        tempb->next = tempa;
        tempa->next = temp;
    }
}
    
void move_over(int a, int b, list *array[])
{
    struct node *pa = NULL, *pb = NULL;
    struct node *tempa = NULL;
    int i = search(&pa, a, array);
    int j = search(&pb, b, array);

    if(i != j)
    {
        struct node *temp = array[j];
        tempa = pa->next;
        pa->next = tempa->next;
        while(temp->next != NULL)
            temp = temp->next;
        temp->next = tempa;
        tempa->next = NULL;
    }
}

void pile_onto(int a, int b, list *array[])
{
    struct node *pa = NULL, *pb = NULL;
    struct node *tempa = NULL, *tempb = NULL;
    int i = search(&pa, a, array);
    int j = search(&pb, b, array);

    if(i != j)
    {
        struct node *temp = NULL;
        tempa = pa->next;
        tempb = pb->next;
        temp = tempb->next;
        pa->next = NULL;
        tempb->next = tempa;
    }
}
 
void pile_over(int a, int b, list *array[])
{
    struct node *pa = NULL, *pb = NULL;
    struct node *tempa = NULL;
    int i = search(&pa, a, array);
    int j = search(&pb, b, array);

    if(i != j)
    {
        struct node *temp = array[j];
        tempa = pa->next;
        pa->next = NULL;
        while(temp->next != NULL)
            temp = temp->next;
        temp->next = tempa;
    }
}
   
int main()
{
    list *array[N];
    int i = 0;
    int n = 0;
    char cmd[4], param[4];
    int a = 0, b = 0;

    scanf("%d", &n);

    for(i = 0; i < n; ++i)
    {
        init(&array[i]);
        insert(i, &array[i]);
    }
   
    while(scanf("%s", cmd) && cmd[0] != 'q')
    {
        scanf("%d", &a);
        scanf("%s", param);
        scanf("%d", &b);

        if(a < n && b < n)
        {
            if(cmd[0] == 'm') // move
            {
                if(param[1] == 'n') // onto
                    move_onto(a, b, array);
                else if(param[1] == 'v') // over
                    move_over(a, b, array);
            }
            else if(cmd[0] == 'p') // pile
            {
                if(param[1] == 'n')
                    pile_onto(a, b, array);
                else if(param[1] == 'v')
                    pile_over(a, b, array);
            }
        }
    }


    for(i = 0; i < n; ++i)
    {
        printf("%d:", i);
        print(array[i]);
    }

    for(i = 0; i < n; ++i)
        free_list(&array[i]);

    return 0;
}
Two inputs on which i test my program and their outputs

Code: Select all

 INPUT
24
pile 0 over 23
pile 1 over 23
pile 2 over 23
pile 3 over 23
pile 4 over 23
pile 5 over 23
pile 6 over 23
pile 7 over 23
pile 8 over 23
pile 9 over 23
pile 10 over 23
pile 11 over 23
pile 12 over 23
pile 13 over 23
pile 14 over 23
pile 15 over 23
pile 16 over 23
pile 17 over 23
pile 18 over 23
pile 19 over 23
pile 20 over 23
pile 21 over 23
pile 22 over 23
pile 23 over 23
pile 23 onto 0
quit
OUTPUT
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23: 23 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

INPUT
21
move 2 onto 1
move 3 onto 2
move 4 onto 3
move 5 over 1
pile 1 over 10
move 9 over 8
move 11 over 8
pile 3 over 8
pile 8 over 3
move 20 over 19
pile 19 over 18
pile 18 onto 15
move 15 over 3
pile 20 onto 19
pile 19 onto 18
pile 18 over 17
quit
OUTPUT
0: 0
1:
2:
3:
4:
5:
6: 6
7: 7
8: 8 9 11 3 4 5 15
9:
10: 10 1 2
11:
12: 12
13: 13
14: 14
15:
16: 16
17: 17 18 19 20
18:
19:
20:
Please, help me find the error in my code.

P. S. Sorry if my english is not very well, i'm french
Last edited by feysal on Tue Aug 17, 2010 2:56 am, edited 2 times in total.
fernandohbc
New poster
Posts: 5
Joined: Sat Aug 14, 2010 10:31 pm

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

Post by fernandohbc »

I tested your code and it turns out that the line below contains a blunder.

Code: Select all

char cmd[4], param[4];
Since the command can have up to 4 characters, an array of 4 chars is not enough to handle the whole string and the \0 string terminator. It must have 5 chars (or more) in order to do that.

Try replacing this line with this new one and see what happens.

Code: Select all

char cmd[5], param[5];
feysal
New poster
Posts: 5
Joined: Sun Aug 15, 2010 3:53 am

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

Post by feysal »

Thanks for your reply,
your suggestion gives Runtime Error too even with

Code: Select all

char cmd[10], param[10];
Post Reply

Return to “Volume 1 (100-199)”