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

jk4837
New poster
Posts: 3
Joined: Sat Aug 04, 2007 6:44 am

101 The Blocks Problem Runtime Error

Post by jk4837 » Sat Aug 04, 2007 6:56 am

It is all right to run on pc. The two sample's answer is correct.
but i receive a letter:
[451] Runtime Error: 101
Dear wonda:

Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference

Before crash, it ran during 0.002 seconds.

--


here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int k,a[27][27],b[27];
int moon(int,int);
int moov(int,int);
int pion(int,int);
int piov(int,int);
main()
{ int i,j,x,y;
char d[5]={" "},h[5]={" "},q[]={"quit"},m[]={"move"},p[]={"pile"},n[]={"onto"},v[]={"over"};
while(1)
{ scanf("%d",&k);
for(i=0;i<k;i++)
{ a[0]=i;
b=i;
for(j=1;j<=k;j++)
a[j]=-1;
}
scanf("%s",d);
while(1)
{ if(strcmp(d,q)==0)
break;
scanf("%d %s %d",&x,h,&y);
if(x!=y && b[x]!=b[y])
{ if(strcmp(d,m)==0 && strcmp(h,n)==0)
moon(x,y);
if(strcmp(d,m)==0 && strcmp(h,v)==0)
moov(x,y);
if(strcmp(d,p)==0 && strcmp(h,n)==0)
pion(x,y);
if(strcmp(d,p)==0 && strcmp(h,v)==0)
piov(x,y);
}
scanf("%s",&d);
}

for(i=0;i<k;i++)
{ j=0;
printf("%d:",i);
while(a[j]!=-1)
{ if(a[j]!=-1)
printf(" %d",a[j]);
else
break;
j++;
}
printf("\n");
}
}
}

int moon(int x,int y)
{ int i=0,j;
while(a[b[x]]!=x)
i++;
a[b[x]]=-1;
i++;
while(a[b[x]]!=-1)
{ b[a[b[x]]]=a[b[x]][i];

a[ a[b[x]][i] ][0]=a[b[x]][i];
a[b[x]][i]=-1;
i++;
}
i=0;
while(a[b[y]][i]!=y)
i++;
j=i;
i++;
while(a[b[y]][i]!=-1)
{ b[a[b[y]][i]]=a[b[y]][i];
a[a[b[y]][i]][0]=a[b[y]][i];
a[b[y]][i]=-1;
i++;
}
a[b[y]][j+1]=x;
b[x]=b[y];
return 1;
}

int moov(int x,int y)
{ int i=0;
while(a[b[x]][i]!=x)
i++;
a[b[x]][i]=-1;
i++;
while(a[b[x]][i]!=-1)
{ b[a[b[x]][i]]=a[b[x]][i];
a[a[b[x]][i]][0]=a[b[x]][i];
a[b[x]][i]=-1;
i++;
}
i=0;
while(a[b[y]][i]!=-1)
i++;
a[b[y]][i]=x;
b[x]=b[y];
return 1;
}

int pion(int x,int y)
{ int i=0,j,t,xx;
xx=b[x];
while(a[b[y]][i]!=y)
i++;
i++;
j=i;
while(a[b[y]][i]!=-1)
{ b[a[b[y]][i]]=a[b[y]][i];
a[a[b[y]][i]][0]=a[b[y]][i];
a[b[y]][i]=-1;
i++;
}
i=0;
while(a[b[x]][i]!=x)
i++;
while(a[xx][i]!=-1)
{ a[b[y]][j]=a[xx][i];
t=a[xx][i];
a[xx][i]=-1;
b[t]=b[y];
i++; j++;
}
return 1;
}

int piov(int x,int y)
{ int i=0,j=0,xx,yy;
xx=b[x];
yy=b[y];
while(a[b[x]][i]!=x)
i++;
while(a[b[y]][j]!=y)
j++;
j++;
while(a[xx][i]!=-1)
{ a[yy][j]=a[xx][i];
b[a[xx][i]]=yy;
a[xx][i]=-1;
i++;
j++;
}
return 1;
}


please help me.
thanks you very much. :D [/code]

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Sat Aug 04, 2007 9:06 pm

Dont open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage

HASHinclude
New poster
Posts: 2
Joined: Tue Oct 23, 2007 12:07 pm

101 RTE

Post by HASHinclude » Tue Oct 23, 2007 12:12 pm

hi I've tried with different test cases that are there in this topic, all are working fine with my code but couldn't get why I'm getting RTE. Here's my code

Code: Select all

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

typedef struct node {

        int val, root;
        struct node *next, *end;
}node;

typedef node* nodeptr;

nodeptr *blocks;

int getNumber(char *mesg)
{
        int i = -1;
        int val = 0;
        while(mesg[++i]!='\0')
                val = val*10 + mesg[i]-'0';

        return val;
}

int *extractNums(char *input)
{

        int *num;
        num = (int*)malloc(sizeof(int)*2);

        strtok(input, " ");
        num[0] = getNumber(strtok(NULL, " "));
        strtok(NULL, " ");
        num[1] = getNumber(strtok(NULL, " "));

        return num;
}

int instack(int first, int second)
{
        if(blocks[first]->root == blocks[second]->root)
                return 1;
        return 0;
}

void changend(int num)
{
        nodeptr befend = NULL, temp;
        if(blocks[num]->root==num)
                return;
        else
        {
                temp = blocks[blocks[num]->root];
                while(temp!=NULL && temp->val!=blocks[num]->val)
                {
                        befend = temp;
                        temp = temp->next;
                }
                if(befend==NULL)
                        return;
                else
                {
                        temp = blocks[blocks[num]->root];
                        befend->next = befend->end = NULL;
                        while(temp->next!=NULL && temp->next->val!=blocks[num]->val)
                        {
                                temp->end = befend;
                                temp = temp->next;
                        }
                }
        }
}

void replace(int num)
{
        nodeptr temp = blocks[num]->next, temp2;
        while(temp!=NULL)
        {
                temp2 = temp->next;
                temp->next = temp->end = NULL;
                temp->root = temp->val;
                temp = temp2;
        }
        blocks[num]->next = NULL;
        blocks[num]->end = NULL;
}

void makend(nodeptr first, nodeptr second)
{
        nodeptr temp = first;

        while(temp->next!=NULL)
        {
                temp->end = second;
                temp = temp->next;
        }
}

void makeroot(nodeptr first, int root)
{
        nodeptr temp = first;

        while(temp!=NULL)
        {
                temp->root = root;
                temp = temp->next;
        }
}

void move_onto(int first, int second)
{
        if(instack(first, second)) return;
        changend(first);
        replace(first);
        replace(second);

        blocks[first]->root = blocks[second]->root;

        makend(blocks[blocks[second]->root], blocks[first]);
        blocks[second]->next = blocks[first];
        blocks[second]->end = blocks[first];
}

void move_over(int first, int second)
{
        if(instack(first, second)) return;
        changend(first);
        replace(first);

        blocks[first]->root = blocks[second]->root;

        if(blocks[second]->next == NULL)
                blocks[second]->next = blocks[first];
        else
                blocks[second]->end->next = blocks[first];

        makend(blocks[blocks[second]->root], blocks[first]);

}

void pile_onto(int first, int second)
{
        if(instack(first, second)) return;

        changend(first);
        replace(second);

        makeroot(blocks[first], blocks[second]->root);

        makend(blocks[blocks[second]->root], blocks[first]);

        blocks[second]->next = blocks[first];
        blocks[second]->end = blocks[first]->end;
}

void pile_over(int first, int second)
{
        if(instack(first, second)) return;
        changend(first);

        makeroot(blocks[first], blocks[second]->root);

        if(blocks[second]->next == NULL)
                blocks[second]->next = blocks[first];
        else
                blocks[second]->end->next = blocks[first];

        makend(blocks[blocks[second]->root], blocks[first]->end);
}

void print(nodeptr block, int val)
{
        nodeptr temp = block;
        while(temp!=NULL)
        {
                if(temp->root == val)
                        printf(" %d", temp->val);

                temp = temp->next;
        }
}

main()
{
        int i, n, *num;
        char input[1000];
        const char quit[10] = "quit";
        const char move[10] = "move";
        const char pile[10] = "pile";
        const char onto[10] = "onto";
        const char over[10] = "over";


        scanf("%d",&n);

        blocks = (nodeptr*)malloc(n*sizeof(nodeptr));
        for(i=0; i<n; i++)
        {
                blocks[i] = (nodeptr)malloc(sizeof(node));
                blocks[i]->val = blocks[i]->root = i;
                blocks[i]->next = blocks[i]->end = NULL;
        }

        while(1)
        {
                scanf("\n%[^\n]", &input);

                if(strstr(&input, &quit)!=NULL)
                        break;

                if(strstr(&input, &move)!=NULL)
                {
                        if(strstr(&input, &onto)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                move_onto(num[0], num[1]);
                        }
                        else if(strstr(&input, &over)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                move_over(num[0], num[1]);
                        }
                }
                else if(strstr(&input, &pile)!=NULL)
                {
                        if(strstr(&input, &onto)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                pile_onto(num[0], num[1]);
                        }
                        else if(strstr(&input, &over)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                pile_over(num[0], num[1]);
                        }
                }
        }

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

                if(blocks[i]->root==i)
                        print(blocks[i], i);

                if(i<n-1)
                        printf("\n");
        }
}

johnfn
New poster
Posts: 1
Joined: Sun Oct 28, 2007 7:21 am

101 WA and I've read everything

Post by johnfn » Sun Oct 28, 2007 7:25 am

My code:

Code: Select all

import java.io.*;

class block{
    static int index;
    static block blocks[] = new block[30];
    public int id;
    blockStack original;
    blockStack cur;
    block(blockStack orig){
        this.id=index;
        index++;
        blocks[index]=this;
        this.original = orig;
        this.cur = orig;
    }
    public int getIndex(){
        return this.id;
    }
    /** Return this block to original position. */
    void returnBlock(){
        cur.removeBlock(this);
        original.addBlock(this);
        this.cur=original;
    }
    /** Move this block to stack st. */
    void moveBlock(blockStack st){
        cur.removeBlock(this);
        st.addBlock(this);
        this.cur = st;
    }
}

class blockStack{
    block[] stack;
    static int index;
    int max;
    int numBlocks;
    public void addBlock(block b){
        stack[numBlocks]=b;
        numBlocks++;
    }
    public void removeBlock(block b){
        boolean found=false;
        for (int i=0;i<numBlocks;i++){
            if (stack[i].equals(b)){
                found=true;
            }
            if (found&&(i+1<numBlocks)){
                stack[i] = stack[i+1];
            }
        }
        
        if (found){
            stack[numBlocks]=null;
            numBlocks--;
        }
    }
    public int placeInLine(block b){
        for (int i=0;i<numBlocks;i++){
            if (stack[i].equals(b)){
                return i;
            }
        }
        return -1;
    }
    
    blockStack(int max){
        this.max=max;
        this.index=index;
        index++;
        stack = new block[max];
        stack[0] = new block(this);
        numBlocks=1;
    }
    void returnBlocks(){
        // i dont think i need this.
        for (int i=0;i<numBlocks;i++){
            if (!stack[i].original.equals(this)){
                stack[i].returnBlock();
            }
        }
    }
    block[] getBlocksAboveIncluding(block start){
        block[] ret = new block[max];
        int arrIndex=0;
        boolean begin=false;
        for (int i=0;i<numBlocks;i++){
            if (stack[i].equals(start)){
                begin=true;
            }
            if (begin){
                ret[arrIndex]= stack[i];
                arrIndex++;
            }
        }
        ret[arrIndex]=null; //end val
        return ret;
    }
    
    void returnBlocksAbove(block start){
        block[] bls = getBlocksAboveIncluding(start);
        for (int i=1;i<bls.length;i++){
            if (bls[i] != null)
                bls[i].returnBlock();
        }
    }
    
    public String toString(){
        String ret=" ";
        for (int i=0;i<numBlocks;i++){
            ret += Integer.toString(stack[i].getIndex()) + " ";
        }
        return ret.substring(0, ret.length()-1);
    }
}


class blockRow{
    static blockStack[] row;
    int max;
    blockRow(int max){
        this.max=max;
        row = new blockStack[max];
        for (int i=0;i<max;i++){
            row[i] = new blockStack(max);
        }
    }
    static blockStack getStack(int index){
        return row[index];
    }
    public void print(){
        System.out.println("");
        for (int i=0;i<max;i++){
            System.out.println(Integer.toString(i)+":" + row[i].toString());
        }
        System.out.println("");
    }
}

public class Main {
    
    public Main() {
    }

    public static void main(String[] args) {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int num=0;
        try{
            num = Integer.parseInt(input.readLine());
        } catch (java.io.IOException Ex){
            System.out.println("Ut oh");
        }
        blockRow br= new blockRow(num);
        String command="";
        String[] words;
        block a, b;
        try{
            
            
            while(true){
                command=input.readLine();
                if (command.substring(0,4).equals("quit")){
                    break;
                }
                
                words= command.split(" ");
                a = block.blocks[Integer.parseInt(words[1])+1];
                b = block.blocks[Integer.parseInt(words[3])+1];
                if (!a.equals(b) && !a.cur.equals(b.cur)){
                    if (words[0].equals("return")){
                        a.returnBlock();
                    }
                    
                    if (words[0].equals("move") && words[2].equals("onto")){
                        a.cur.returnBlocksAbove(a);
                        b.cur.returnBlocksAbove(b);
                        a.moveBlock(b.cur);
                    }
                    if (words[0].equals("move") && words[2].equals("over")){
                        a.cur.returnBlocksAbove(a);
                        a.moveBlock(b.cur);
                    }
                    if (words[0].equals("pile") && words[2].equals("onto")){
                        block[] above;
                        
                        b.cur.returnBlocksAbove(b);
                        above = a.cur.getBlocksAboveIncluding(a);
                        for (int j=0;j<above.length;j++){
                            if (above[j]==null){
                                break;
                            }
                            above[j].moveBlock(b.cur);
                        }
                    }
                    if (words[0].equals("pile") && words[2].equals("over")){
                        block[] above;
                        
                        above = a.cur.getBlocksAboveIncluding(a);
                        for (int j=0;j<above.length;j++){
                            if (above[j]==null){
                                break;
                            }
                            above[j].moveBlock(b.cur);
                        }
                    }
                    //br.print();
                }
            }
            
            
            
        } catch (java.io.IOException Ex){
            System.out.println("Ut oh");
        }
        
        
        
        
        
        br.print();
    }
    
}
I continuously get WA despite running the code through every sample entry I've seen on the whole board with correct output. Whats going wrong? Halp :(

Thanks!

duleb
New poster
Posts: 4
Joined: Mon Jun 11, 2007 8:06 am

101 Plz Help

Post by duleb » Mon Oct 29, 2007 12:16 pm

Im starting a new thread since previous threads are not getting response

Here is my code

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


int fin[30],number[30],ques[50][50],n;

int show()
{
int i,j;
for(i=0;i<n;i++)
{
printf("%d:",i);
for(j=0;j<number;j++)
printf(" %d",ques[j]);
printf("\n");
}
return 1;
}

int movestack(int a , int b)
{
int i,pos1,pos2,j=-1,x,y,arr[25],k=0,max,flag=0;
pos1 = fin[a];
pos2 = fin;
if(pos1==pos2)
flag =1;
max = number[fin[a]];
for(x = 0;x< max;x++)
if(ques[pos1][x] == a)
break;
max = number[fin];
for(y = 0;y< max;y++)
if(ques[pos2][y] == b)
break;

for(i = x;i<number[pos1];i++)
arr[++j] = ques[pos1];
number[pos1] = x;
for(k=0;k<=j;k++)
{
ques[pos2][number[pos2]] = arr[k];
fin[arr[k]] = pos2;
number[pos2] = number[pos2] + 1;
}

}

int moveinitial(int pos , int el)
{
int i,j,max,x;
max = number[ pos];
for(i = 0;i< max;i++)
if(ques[pos] == el)
break;
for(j=max-1;j>=i+1;j--)
{
x = ques[pos][j];
fin[x] = x;
number[x] = number[x] + 1;
number[pos] = number[pos] - 1;
ques[x][number[x]] = x;
}
return 1;
}






int move(int a ,int b,int mode)
{
int pos1,pos2;
pos1 = fin[a];
pos2 = fin;
if(pos1==pos2 || a==b)
return 1;
moveinitial(pos1,a);
if(mode == 1) /* 1 for onto */
moveinitial(pos2,b);

fin[a] = pos2;
ques[pos2][number[pos2]] = a;
number[pos2] = number[pos2] + 1;
number[pos1] = number[pos1] - 1;

return 1;
}

int pile(int a,int b,int mode)
{

int pos1,pos2;
pos1 = fin[a];
pos2 = fin;

if(pos1==pos2 || a==b)
return 1;
if(mode == 1)
{
moveinitial(pos2,b);
movestack(a,b);
}
else if(mode == 2)
movestack(a,b);

return 1;
}



int main()
{
char s[10],s1[10];
int a,b,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
ques[0] = i;
number = 1;
fin = i;
}

while(1)
{
scanf("%s",s);
if(!strcmp(s,"quit"))
{
show();
break;
}
if(!strcmp(s,"move"))
{
scanf("%d",&a);
scanf("%s",s);
scanf("%d",&b);
if(!strcmp(s,"onto"))
move(a,b,1);
else
move(a,b,2); /*Over */

}
else if(!strcmp(s,"pile"))
{
scanf("%d",&a);
scanf("%s",s);
scanf("%d",&b);
if(!strcmp(s,"onto"))
pile(a,b,1);
else
pile(a,b,2); /*Over */

}
}
return 1;
}


I have checked it on a wide variety of test case on unix and the outputs are all fine
But on Submission i get RUNTIME ERROR
So please help me out

AND about the code
ARRAY number[] is used to store the number of elements in each block stack
find[] keeps track that to which block stack does an element belong

HASHinclude
New poster
Posts: 2
Joined: Tue Oct 23, 2007 12:07 pm

101 RTE : tried 3 times and also with example inputs here..

Post by HASHinclude » Mon Oct 29, 2007 1:35 pm

Hi,
I've tried three times to upload my 101 problem but got RTE all the times. I tested with whatever input I found in topics related to 101 all the outputs I'm getting are matching with the ones mentioned in the posts. Can someone see my code and help me!!

Code: Select all

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

typedef struct node {

        int val, root;
        struct node *next, *end;
}node;

typedef node* nodeptr;

nodeptr *blocks;

int getNumber(char *mesg)
{
        int i = -1;
        int val = 0;
        while(mesg[++i]!='\0')
                val = val*10 + mesg[i]-'0';

        return val;
}

int *extractNums(char *input)
{

        int *num;
        num = (int*)malloc(sizeof(int)*2);

        strtok(input, " ");
        num[0] = getNumber(strtok(NULL, " "));
        strtok(NULL, " ");
        num[1] = getNumber(strtok(NULL, " "));

        return num;
}

int instack(int first, int second)
{
        if(blocks[first]->root == blocks[second]->root)
                return 1;
        return 0;
}

void changend(int num)
{
        nodeptr befend = NULL, temp;
        if(blocks[num]->root==num)
                return;
        else
        {
                temp = blocks[blocks[num]->root];
                while(temp!=NULL && temp->val!=blocks[num]->val)
                {
                        befend = temp;
                        temp = temp->next;
                }
                if(befend==NULL)
                        return;
                else
                {
                        temp = blocks[blocks[num]->root];
                        befend->next = befend->end = NULL;
                        while(temp->next!=NULL && temp->next->val!=blocks[num]->val)
                        {
                                temp->end = befend;
                                temp = temp->next;
                        }
                }
        }
}

void replace(int num)
{
        nodeptr temp = blocks[num]->next, temp2;
        while(temp!=NULL)
        {
                temp2 = temp->next;
                temp->next = temp->end = NULL;
                temp->root = temp->val;
                temp = temp2;
        }
        blocks[num]->next = NULL;
        blocks[num]->end = NULL;
}

void makend(nodeptr first, nodeptr second)
{
        nodeptr temp = first;

        while(temp->next!=NULL)
        {
                temp->end = second;
                temp = temp->next;
        }
}

void makeroot(nodeptr first, int root)
{
        nodeptr temp = first;

        while(temp!=NULL)
        {
                temp->root = root;
                temp = temp->next;
        }
}

void move_onto(int first, int second)
{
        if(instack(first, second)) return;
        changend(first);
        replace(first);
        replace(second);

        blocks[first]->root = blocks[second]->root;

        makend(blocks[blocks[second]->root], blocks[first]);
        blocks[second]->next = blocks[first];
        blocks[second]->end = blocks[first];
}

void move_over(int first, int second)
{
        if(instack(first, second)) return;
        changend(first);
        replace(first);

        blocks[first]->root = blocks[second]->root;

        if(blocks[second]->next == NULL)
                blocks[second]->next = blocks[first];
        else
                blocks[second]->end->next = blocks[first];

        makend(blocks[blocks[second]->root], blocks[first]);

}

void pile_onto(int first, int second)
{
        if(instack(first, second)) return;

        changend(first);
        replace(second);

        makeroot(blocks[first], blocks[second]->root);

        makend(blocks[blocks[second]->root], blocks[first]);

        blocks[second]->next = blocks[first];
        blocks[second]->end = blocks[first]->end;
}

void pile_over(int first, int second)
{
        if(instack(first, second)) return;
        changend(first);

        makeroot(blocks[first], blocks[second]->root);

        if(blocks[second]->next == NULL)
                blocks[second]->next = blocks[first];
        else
                blocks[second]->end->next = blocks[first];

        makend(blocks[blocks[second]->root], blocks[first]->end);
}

void print(nodeptr block, int val)
{
        nodeptr temp = block;
        while(temp!=NULL)
        {
                if(temp->root == val)
                        printf(" %d", temp->val);

                temp = temp->next;
        }
}

main()
{
        int i, n, *num;
        char input[1000];
        const char quit[10] = "quit";
        const char move[10] = "move";
        const char pile[10] = "pile";
        const char onto[10] = "onto";
        const char over[10] = "over";


        scanf("%d",&n);

        blocks = (nodeptr*)malloc(n*sizeof(nodeptr));
        for(i=0; i<n; i++)
        {
                blocks[i] = (nodeptr)malloc(sizeof(node));
                blocks[i]->val = blocks[i]->root = i;
                blocks[i]->next = blocks[i]->end = NULL;
        }

        while(1)
        {
                scanf("\n%[^\n]", &input);

                if(strstr(&input, &quit)!=NULL)
                        break;

                if(strstr(&input, &move)!=NULL)
                {
                        if(strstr(&input, &onto)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                move_onto(num[0], num[1]);
                        }
                        else if(strstr(&input, &over)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                move_over(num[0], num[1]);
                        }
                }
                else if(strstr(&input, &pile)!=NULL)
                {
                        if(strstr(&input, &onto)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                pile_onto(num[0], num[1]);
                        }
                        else if(strstr(&input, &over)!=NULL)
                        {
                                num = extractNums(&input);
                                if(num[0]==num[1]) continue;
                                pile_over(num[0], num[1]);
                        }
                }
        }

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

                if(blocks[i]->root==i)
                        print(blocks[i], i);

                printf("\n");
        }
}
Thank You.

duleb
New poster
Posts: 4
Joined: Mon Jun 11, 2007 8:06 am

Post by duleb » Wed Oct 31, 2007 2:19 pm

Somebody help me out
Im getting RTE and i cant figure out why

about my program
ques[][] is used to store the elments corresponding to each block 0 .. n-1
number[] store num of elements in each block
fin[] store the position of each element
for eg element a is present at block fin[a]

PLZ help me out

#include<iostream>
#include<string.h>

using namespace std;

int fin[30],number[30],ques[30][30],n;

int show()
{
int i,j;
for(i=0;i<n;i++)
{
printf("%d:",i);
for(j=0;j<number;j++)
printf(" %d",ques[j]);
printf("\n");
}
return 1;
}

int movestack(int a , int b)
{
int i,pos1,pos2,j=-1,x,y,arr[30],k=0,max,flag=0;
pos1 = fin[a];
pos2 = fin;
if(pos1==pos2)
flag =1;
max = number[fin[a]];
for(x = 0;x< max;x++)
if(ques[pos1][x] == a)
break;
max = number[fin];
for(y = 0;y< max;y++)
if(ques[pos2][y] == b)
break;

for(i = x;i<number[pos1];i++)
arr[++j] = ques[pos1];
number[pos1] = x;
for(k=0;k<=j;k++)
{
ques[pos2][number[pos2]] = arr[k];
fin[arr[k]] = pos2;
number[pos2] = number[pos2] + 1;
}

}

int moveinitial(int pos , int el)
{
int i,j,max,x;
max = number[ pos];
for(i = 0;i< max;i++)
if(ques[pos] == el)
break;
for(j=max-1;j>=i+1;j--)
{
x = ques[pos][j];
fin[x] = x;
number[pos] = number[pos] - 1;
ques[x][number[x]] = x;
number[x] = number[x] + 1;
}
return 1;
}

int move(int a ,int b,int mode)
{
int pos1,pos2;
pos1 = fin[a];
pos2 = fin;
if(pos1==pos2 || a==b)
return 1;
moveinitial(pos1,a);
if(mode == 1)
moveinitial(pos2,b);

fin[a] = pos2;
ques[pos2][number[pos2]] = a;
number[pos2] = number[pos2] + 1;
number[pos1] = number[pos1] - 1;

return 1;
}
int pile(int a,int b,int mode)
{

int pos1,pos2;
pos1 = fin[a];
pos2 = fin;

if(pos1==pos2 || a==b)
return 1;
if(mode == 1)
{
moveinitial(pos2,b);
movestack(a,b);
}
else if(mode == 2)
movestack(a,b);

return 1;
}

int main()
{

int a,b,i;
cin>>n;
for(i=0;i<n;i++)
{
ques[0] = i;
number = 1;
fin = i;
}

while(1)
{
char s[10];
s[0]='\0';
scanf("%s",s);
if(s[0]=='q')
{
show();
break;
}
if(!strcmp(s,"move"))
{
char s1[10];
scanf("%d",&a);
scanf("%s",s1);

scanf("%d",&b);
if(a>=n || b>=n)
continue;
if(!strcmp(s1,"onto"))
move(a,b,1);
else
move(a,b,2);


}
else if(!strcmp(s,"pile"))
{
char s1[10];
scanf("%d",&a);
scanf("%s",s1);
scanf("%d",&b);
if(a>=n || b>=n)
continue;
if(!strcmp(s1,"onto"))
pile(a,b,1);
else
pile(a,b,2);

}
}

return 1;
}

User avatar
mrunmoy
New poster
Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India
Contact:

C compiler used by UVA judge

Post by mrunmoy » Mon Dec 10, 2007 8:32 pm

Hello,

I would like to know which c compiler the judge uses so that I can compile my code in the same compiler and avoid submitting many times to online judge and any unnecessary error codes returned.

Also please comment on the following way of coding practice to be used for submissions in C.

Code: Select all

/* @JUDGE_ID: myId ProbNbr C "Some algorithm" */

#includes...
#defines...

... foo(...);

int main(int argc, char *argv[])
{
//  taking inputs {usually depends upon the problem at hand but...} should I use getch()?, getchar()?,scanf("%[^\n]",&buffer)? what would you reccomend? <<the problem i am facing is redirecting input files to my executable, it doesnt work every time, because of this i got a few RTEs and WAs>>

foo(...);

// printing outputs is simple...
}

... foo(...)
{
...
}
Best Regards,
Mrunmoy.

I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!

User avatar
mrunmoy
New poster
Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India
Contact:

C compiler used by UVA judge+++

Post by mrunmoy » Mon Dec 10, 2007 8:43 pm

The judge asks me to choose, now I am using bloodshed developer c++ to compile my code. which of these should i choose?

>> ANSI C 4.1.2 - GNU C Compiler with options: -lm -lcrypt -O2 -pipe -ansi -DONLINE_JUDGE

>>C++ 4.1.2 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE
Best Regards,
Mrunmoy.

I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!

User avatar
mrunmoy
New poster
Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India
Contact:

Getting Compilation Error on 101

Post by mrunmoy » Mon Dec 10, 2007 9:02 pm

I am getting CE and i cant figure out why? when the same code is working fine on the devc++ compiler as well as vc++ 6.0 compiler

pls help!
Best Regards,
Mrunmoy.

I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!

jan_holmes
Experienced poster
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore
Contact:

Post by jan_holmes » Mon Dec 10, 2007 9:32 pm

using ">>C++ 4.1.2 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE" is better because most functions in C will be available in C++.

User avatar
mrunmoy
New poster
Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India
Contact:

RE: C compiler used by UVA judge+++

Post by mrunmoy » Mon Dec 10, 2007 10:06 pm

Thanks jan_holmes,

But my code is getting CE. I have tested the code on mingw compiler(bllodshed devc++) and vc++ 6.0. I cannot understand why t is getting compiler? I do not get any emails describing the problem also...

help pls.

here goes my code:-

Code: Select all

/* @JUDGE_ID: 59960OQ 101 C "Brute Force" */
/* Solution to ACM UVa Problem 101 - The Blocks Problem  */
/* Author- Mrunmoy Samal*/

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

blah blah...


Best Regards,
Mrunmoy.

I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!

User avatar
mrunmoy
New poster
Posts: 17
Joined: Mon Apr 09, 2007 3:11 pm
Location: India
Contact:

Post by mrunmoy » Mon Dec 10, 2007 10:13 pm

after trying the choice which you had provided I am now getting runtime error! with no explainations! is there something which I am missing about the new judge? I have read through most of the FAQs, HOW TOs and beginner materials. If so many people are getting ACs the judge definitely cannot be stupid
:wink:

its me who is doing something wrong somewhere... i will take my question to the right forum under the right question now. but please answer my previous question. which compiler should i use?
Best Regards,
Mrunmoy.

I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!

maxdiver
Learning poster
Posts: 51
Joined: Tue Sep 04, 2007 2:12 pm
Location: Russia, Saratov
Contact:

Post by maxdiver » Mon Dec 10, 2007 11:17 pm

If you want help, you should put your full code here.

jan_holmes
Experienced poster
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore
Contact:

Post by jan_holmes » Tue Dec 11, 2007 12:56 pm

Runtime Error is happened not because of the unacknowledgement syntaxes in your code... It is happened because of the error on your programming logic source code, like for example you defined an array size of 10, but suddenly you try to access arr[10].

Post Reply

Return to “Volume 1 (100-199)”