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

nford85
New poster
Posts: 3
Joined: Sun Dec 28, 2003 5:15 am

has anyone been successful with 101 in JAVA?

Post by nford85 »

Hi

I am using Java to solve problem 101, however, everytime I submit it, I get a reply that the answer was wrong, even though it never gives a wrong answer on my computer. Is it a problem with the code or is it a problem with Java?

[java]/* @JUDGE_ID: 29073KW 101 Java */

import java.io.*;
import java.util.*;

class Main{
static String ReadLn (int maxLg){
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);
return (new String (lin, 0, lg));
}

static int findNumRow (int num, int row, int blocks[][]){
int x=row, y=0;
boolean found = false;
do{
if (blocks[x][y]==num){
found=true;
}else{
y++;
}
}while(!found || y>=blocks.length);

return y;
}

static int findNum (int num, int order, int blocks[][]){
int x=0, y=0;
boolean found = false;
do{
if (blocks[x][y]==num){
found=true;
}else{
x++;
if (x>=blocks.length){
x=0;
y++;
}
}
}while(!found);

if (order==0){
return x;
}else{
return y;
}
}

static void removeGaps(int blocks[][]){
int lastY;
for (int i=0; i<blocks.length; i++){
lastY=-1;
for (int j=0; j<blocks.length; j++){
if (blocks[j]!=30){
if (lastY!=j-1 && lastY!=-1){
for (int y=lastY+1; y<blocks.length-1; y++){
blocks[y]=blocks[y+1];
}
}
lastY=j;
}
}
}
}

static void returnBlocks(int x1, int y1, int blocks[][]){
for (int y=y1+1; y<blocks.length; y++){
if (blocks[x1][y]!=30){
blocks[blocks[x1][y]][0]=blocks[x1][y];
blocks[x1][y]=30;
}
}
}

public static void main (String args[]){
int numBlockPos=Integer.parseInt(ReadLn(3).trim());
int blockArr[][]=new int[numBlockPos][numBlockPos];
for (int i=0; i<blockArr.length; i++){
blockArr[0]=i;
for (int j=1; j<numBlockPos; j++){
blockArr[j]=30;
}
}

String line, firstComm="", secComm="";
int n, a=0, b=0, x1=0, x2=0, y1=0, y2=0, x, y;
line=ReadLn(1);
line=ReadLn(15).trim().toLowerCase();

while (!line.equals("quit")){
StringTokenizer iLine = new StringTokenizer(line);
firstComm=iLine.nextToken();
a=Integer.parseInt(iLine.nextToken());
secComm=iLine.nextToken();
b=Integer.parseInt(iLine.nextToken());
x1=findNum(a, 0, blockArr);
x2=findNum(b, 0, blockArr);
y1=findNum(a, 1, blockArr);
if (x1!=x2){

if (firstComm.equals("move")){
returnBlocks(x1, y1, blockArr);
if (secComm.equals("onto")){
y2=findNum(b, 1, blockArr);
returnBlocks(x2, y2, blockArr);
n=y2+1;
}else{
n=findNumRow(30, x2, blockArr);
}
blockArr[x2][n]=blockArr[x1][y1];
blockArr[x1][y1]=30;
}else{
if (secComm.equals("onto")){
y2=findNum(b, 1, blockArr);
returnBlocks(x2, y2, blockArr);


while (y1<numBlockPos && y2<numBlockPos-1){
blockArr[x2][y2+1]=blockArr[x1][y1];
blockArr[x1][y1]=30;
y1++;
y2++;
}

}else{
y2=findNumRow(30, x2, blockArr)-1;

while (y1<numBlockPos && y2<numBlockPos-1){
blockArr[x2][y2+1]=blockArr[x1][y1];
blockArr[x1][y1]=30;
y1++;
y2++;
}
}

}
removeGaps(blockArr);
}

line=ReadLn(15).trim().toLowerCase();
}

for (x=0; x<numBlockPos; x++){
System.out.print(x+":");
for (y=0; y<numBlockPos; y++){
if (blockArr[x][y]!=30){
System.out.print(" "+blockArr[x][y]);
}
}
System.out.println();
}
}
}[/java]

Any ideas are welcome

Thanks
Nick

scruff
New poster
Posts: 29
Joined: Wed Dec 24, 2003 5:22 am

Post by scruff »

You are correct in that the only block that can be in position 0 is the block that belongs in that stack(ie: 1 belongs in stack 1, 3 belongs in stack 3).

aakash_mandhar
New poster
Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

Why WA

Post by aakash_mandhar »

My code given below gives the same op as acc for the ip given below but can anyone tell me what is the error.. Maybe some presentation fault a space here or there or a newline character here or there.. Plz help
[cpp]
# include<iostream.h>
# include<string.h>

int a[25][25];
int n;
int pos[25],top[25];
int i,from,to,p1,p2,t1,t2;
char str1[5],str2[5];

void display()
{
for(int i=0;i<n;i++)
{
cout<<i<<":";
for(int j=0;j<top;j++)
{
cout<<" "<<a[j];
}
cout<<"\n";
}
}

int main()
{
cin>>n;
for(i=0;i<n;i++)
{
pos=i;
a[0]=i;
top=1;
}



while(1)
{

cin>>str1;
if(strcmp(str1,"quit")==0) break;
cin>>from;
cin>>str2;
cin>>to;
if(from<0 || from>=n || to<0 || to>=n) continue;

p1=pos[from];
p2=pos[to];
if(p1==p2) continue;

for(i=0;i<top[p1];i++)
{
if(a[p1]==from) {t1=i;break;}
}

for(i=0;i<top[p2];i++)
{
if(a[p2]==to) {t2=i;break;}
}

if(strcmp(str1,"move")==0 &&strcmp(str2,"onto")==0)
{
int temp1,temp2;
for(i=t1+1;i<top[p1];i++) {temp1=a[p1];a[temp1][0]=temp1;top[temp1]=1;pos[temp1]=temp1;}
for(i=t2+1;i<top[p2];i++) {temp2=a[p2];a[temp2][0]=temp2;top[temp2]=1;pos[temp2]=temp2;}
top[p1]=t1;
top[p2]=t2+1;
a[p2][top[p2]]=from;
top[p2]++;
pos[p1]=p2;
}
else
if(strcmp(str1,"move")==0 &&strcmp(str2,"over")==0)
{
int temp1,temp2;
for(i=t1+1;i<top[p1];i++) {temp1=a[p1];a[temp1][0]=temp1;top[temp1]=1;pos[temp1]=temp1;}
//for(i=t2+1;i<top[p2];i++) {temp2=a[p2][i];a[temp2][0]=temp2;top[temp2]=1;pos[temp2]=temp2;}
top[p1]=t1;
//top[p2]=t2+1;
a[p2][top[p2]]=from;
top[p2]++;
pos[p1]=p2;

}
else
if(strcmp(str1,"pile")==0 &&strcmp(str2,"onto")==0)
{
int temp1,temp2;
for(i=t2+1;i<top[p2];i++) {temp2=a[p2][i];a[temp2][0]=temp2;top[temp2]=1;pos[temp2]=temp2;}
top[p2]=t2+1;

for(i=t1;i<top[p1];i++)
{
a[p2][top[p2]]=a[p1][i];
top[p2]++;
pos[a[p1][i]]=p2;
}
top[p1]=t1;

}
else
if(strcmp(str1,"pile")==0 &&strcmp(str2,"over")==0)
{
int temp1,temp2;
//for(i=t1+1;i<top[p1];i++) {temp1=a[p1][i];a[temp1][0]=temp1;top[temp1]=1;pos[temp1]=temp1;}
//for(i=t2+1;i<top[p2];i++) {temp2=a[p2][i];a[temp2][0]=temp2;top[temp2]=1;pos[temp2]=temp2;}
//top[p1]=t1;
//top[p2]=t2+1;
//a[p2][top[p2]]=from;
//top[p2]++;
//pos[p1]=p2;
for(i=t1;i<top[p1];i++)
{
a[p2][top[p2]]=a[p1][i];
top[p2]++;
pos[a[p1][i]]=p2;
}
top[p1]=t1;

}


}
display();
return 1;
}


[/cpp]

Given ip
  • 19
    move 1 onto 0
    move 0 onto 1
    move 0 onto 2
    move 2 onto 1
    move 4 over 5
    move 7 onto 8
    move 9 onto 7
    move 7 over 9
    move 9 over 7
    move 11 over 10
    move 12 over 10
    move 13 over 10
    move 14 over 10
    move 16 over 15
    move 17 over 15
    move 18 over 15
    move 16 onto 14
    pile 17 onto 12
    move 15 over 10
    pile 17 onto 14
    pile 15 over 7
    pile 6 over 5
    pile 3 onto 9
    quit
output obtained:
  • 0: 0
    1: 1 2
    2:
    3:
    4:
    5: 5 4 6
    6:
    7:
    8: 8 7 9 3
    9:
    10: 10 11 12
    11:
    12:
    13: 13
    14: 14 17
    15: 15
    16: 16
    17:
    18: 18
Plz help guys.. Thx in advance
...I was born to code...

scruff
New poster
Posts: 29
Joined: Wed Dec 24, 2003 5:22 am

Post by scruff »

The problem lies in your move code. Your code works in most cases and probably passes mose of the cases given on the board. The nasty error rears it head when you attempt to move items to a position where it had been already moved atleast once before... here's an example
input
4
move 0 onto 1
move 0 over 2
move 3 onto 0
quit
your program does this
move 0 onto 1
0:
1: 1 0
2: 2
3: 3
move 0 over 2
0:
1: 1
2: 2 0
3: 3
move 3 onto 0
0:
1: 1 3
2: 2 0
3:
To fix this just change your move code to like so
[cpp]
if(strcmp(str1,"move")==0 &&strcmp(str2,"onto")==0)
{
int temp1,temp2;
for(i=t1+1;i<top[p1];i++) {temp1=a[p1];a[temp1][0]=temp1;top[temp1]=1;pos[temp1]=temp1;}
for(i=t2+1;i<top[p2];i++) {temp2=a[p2];a[temp2][0]=temp2;top[temp2]=1;pos[temp2]=temp2;}
top[p1]=t1;
top[p2]=t2+1;
a[p2][top[p2]]=from;
top[p2]++;
// changed the next line from pos[p1]=p2;
pos[from]=p2;
}
else
if(strcmp(str1,"move")==0 &&strcmp(str2,"over")==0)
{
int temp1,temp2;
for(i=t1+1;i<top[p1];i++) {temp1=a[p1];a[temp1][0]=temp1;top[temp1]=1;pos[temp1]=temp1;}
//for(i=t2+1;i<top[p2];i++) {temp2=a[p2];a[temp2][0]=temp2;top[temp2]=1;pos[temp2]=temp2;}
top[p1]=t1;
//top[p2]=t2+1;
a[p2][top[p2]]=from;
top[p2]++;
// changed the next line from pos[p1]=p2;
pos[from]=p2;
}[/cpp]

Happy coding... be careful next time :)

aakash_mandhar
New poster
Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

Thanx

Post by aakash_mandhar »

Thank you scruff..
That was a pretty silly mistake to do on my part...

Thx a lot..

Aakash Deep Mandhar

:D
...I was born to code...

zolto
New poster
Posts: 3
Joined: Wed Jan 21, 2004 7:09 pm

Post by zolto »

I have another problem. I believe my code is good, it works on my computer, but judge says "Runtime Error (SIGSEGV)". What can be wrong?

[cpp]
#include <cstdio>
using namespace std;

int a, b, n;
char c[4], d[4];

typedef struct li
{
li *prev;
int key;
li *next;
};

typedef struct op
{
li *head;
li *tail;
};

void create(int T[], op L[], int i)
{
li *z = new li;
z->key=i;
z->prev=NULL;
z->next=NULL;
T=i;
L.head=z;
L.tail=z;
}

int main(void)
{
li *z, *zz;
scanf("%d",&n);
int *T = new int[n];
op *L = new op[n];
for (int i=0;i<n;i++) create(T,L,i);
scanf("%s",&c);
while (c[0]!='q')
{
scanf("%d %s %d",&a,&d,&b);
if (T[a]!=T)
{
if (c[0]=='m')
{
z=L[T[a]].tail;
while (z->key!=a)
{
L[z->key].head=z;
L[z->key].tail=z;
T[z->key]=z->key;
z=z->prev;
z->next->prev=NULL;
z->next=NULL;
}
}
if (d[1]=='t')
{
z=L[T].tail;
while (z->key!=b)
{
L[z->key].head=z;
L[z->key].tail=z;
T[z->key]=z->key;
z=z->prev;
z->next->prev=NULL;
z->next=NULL;
}
}
z=L[T[a]].tail;
while (z->key!=a)
{
T[z->key]=T;
z=z->prev;
}
L[T].tail->next=z;
zz=L[T].tail;
L[T].tail=L[T[a]].tail;
L[T[a]].tail=z->prev;
if (z->prev!=NULL) z->prev->next=NULL;
else L[T[a]].head=NULL;
z->prev=zz;
T[a]=T;
}
scanf("%s",&c);
}
for (int i=0;i<n;i++)
{
printf("%d:",i);
z=L.head;
while (z!=NULL)
{
printf(" %d",z->key);
z=z->next;
}
printf("\n");
}
return 0;
}
[/cpp]

scruff
New poster
Posts: 29
Joined: Wed Dec 24, 2003 5:22 am

Post by scruff »

Im at work right now so I can't check your code but at first glance it looks like you are not cleaning up after yourself. You are creating new nodes but you never delete them... I believe this could cause an error but I thought that it was more of a "Time limit exceeded" type of error. Try deleting your nodes and see if that helps

zolto
New poster
Posts: 3
Joined: Wed Jan 21, 2004 7:09 pm

Post by zolto »

Hmm.. Didn't work, or I delete wrong..
[cpp]
...
for (int i=0;i<n;i++)
{
printf("%d:",i);
z=L.head;
while (z!=NULL)
{
printf(" %d",z->key);
zz=z->next;
delete(z);
z=zz;
z->prev=NULL;
}
printf("\n");
}
delete(T);
delete(L);
return 0;
}
[/cpp]
A valuable information may be as well, that program crashes literally after no time - Judge reports 0:00.000

scruff
New poster
Posts: 29
Joined: Wed Dec 24, 2003 5:22 am

Post by scruff »

Yes, I see that now. Well, it says it crashes with an invalid memory reference. But you would delete your nodes like so[cpp]
/* this is the last part of your code */
for (int i=0;i<n;i++)
{
printf("%d:",i);
z=L.head;
while (z!=NULL)
{
printf(" %d",z->key);
z=z->next;
}
printf("\n");
}
// added these two lines
delete [] L;
delete [] T;
return 0;
}
[/cpp]

You only have to use delete on the variables that you used new to allocate memory. So if you want to delete z it would be in your function create at the end.

I get off work in 4 hours I will try to look at it more then, but you should just double check your references.

scruff
New poster
Posts: 29
Joined: Wed Dec 24, 2003 5:22 am

Post by scruff »

You don't seemed to have enough assertions or any at all. You are assuming that z->next has another node in it when it quite possibly and does actually have NULL from the initilization. You need to run debug and look at where your code fails at or when you are assigning things make sure you are assigning them to a node that is not NULL. Watch "z" and make sure that prev and next always point to another node and make sure that you aren't referencing NULL.[cpp]
#include <cstdio>
using namespace std;

int a, b, n;
char c[4], d[4];

typedef struct li
{
li *prev;
int key;
li *next;
};

typedef struct op
{
li *head;
li *tail;
};

void create(int T[], op L[], int i)
{
li *z = new li;
z->key=i;
z->prev=NULL;
z->next=NULL;
T=i;
L.head=z;
L.tail=z;
}

int main(void)
{
li *z, *zz;
scanf("%d",&n);
int *T = new int[n];
op *L = new op[n];
for (int i=0;i<n;i++) create(T,L,i);
scanf("%s",&c);
while (c[0]!='q')
{
scanf("%d %s %d",&a,&d,&b);
if (T[a]!=T)
{
if (c[0]=='m')
{
z=L[T[a]].tail;
while (z->key!=a)
{
L[z->key].head=z;
L[z->key].tail=z;
T[z->key]=z->key;
z=z->prev;
z->next->prev=NULL;
z->next=NULL;
}
}
if (d[1]=='t')
{
z=L[T].tail;
while (z->key!=b)
{
L[z->key].head=z;
L[z->key].tail=z;
T[z->key]=z->key;
z=z->prev;
z->next->prev=NULL;
z->next=NULL;
}
}
z=L[T[a]].tail;
/***********************************
Make sure that z is pointing to a node
Added the first condition of your loop
***********************************/
while((z)&& (z->key!=a) )
{
T[z->key]=T;
z=z->prev;
}
L[T].tail->next=z;
zz=L[T].tail;
L[T].tail=L[T[a]].tail;
/******************************
Your code fails here as well so you need to
make sure are referencing a correct node.
I didn't fix this part because I don't have time
tonight. Maybe tomorrow.
******************************/
L[T[a]].tail=z->prev;
if (z->prev!=NULL) z->prev->next=NULL;
else L[T[a]].head=NULL;
z->prev=zz;
T[a]=T;
}
scanf("%s",&c);
}
for (int i=0;i<n;i++)
{
printf("%d:",i);
z=L.head;
while (z!=NULL)
{
printf(" %d",z->key);
z=z->next;
}
printf("\n");
}
/************************************
Delete your dynamic data here
************************************/
delete [] T;
delete [] L;
return 0;
} [/cpp]

BTW, here's a program that will give you larger inputs so that you can see the failures. I believe that the inputs you use might not be large enough to cause problems for you. This will help you debug as it gives you the correct output for it's input.

http://people.eecs.ku.edu/~rucker/valladolid/p101.exe

Happy Coding!

41531HN
New poster
Posts: 1
Joined: Fri Jan 23, 2004 9:41 am

101 seg fault

Post by 41531HN »

thanx in advance for helping

this code gives a seg fault, but works perfectly fine when i compiled it with gcc ver 3.2.2

#include <stdio.h>

typedef struct list
{
int v;
struct list *next;
}list;
list **arr;
int n;

list *find (int a)
{
int i;
for(i = 0; i < n; i++)
{
list *j;
for( j = arr; j->next != (list *)0; j = j->next)
{
if(j->next->v == a)
return j;
}
}
return (list *)0;
}

list *find2 (int b)
{
int i;
list *j = find(b);
{
for(; j->next != (list *)0; j = j->next);
}
return j;
}

int main()
{
int a, b, f, t;
char buf[BUFSIZ];
int i;
scanf("%d", &n);
arr = (list **)calloc(n, sizeof(list));
for(i = 0; i < n; i++)
{
list *ele = (list *)malloc(sizeof(list));
ele->v = i;
ele->next = (list *)0;
arr = (list *)malloc(sizeof(list));
arr->next = ele;
arr->v = -1;
}
while( scanf("%s", buf) != EOF)
{
list *posa, *posb, *iter;
if(strcmp(buf, "quit" ) == 0)
break;
if(strcmp(buf, "move" ) == 0)
f = 1;
else f = 0;
scanf("%d %s", &a, buf);
if(strcmp(buf, "onto" ) == 0)
t = 1;
else t = 0;
scanf("%d", &b);
if( !( a < n && b < n && a >= 0 && b >= 0))
continue;
if( find2(a) == find2(b))
continue;
switch(f)
{
case 1:
switch(t)
{
case 1:
posa = find(a);
posb = find(b);
if(posa == (list *)0 || posb == (list *)0)
continue;
for(iter = posa->next->next; iter != (list *)0; iter = iter->next)
{
arr[iter->v]->next = (list *)malloc(sizeof(iter));
arr[iter->v]->next->v = iter->v;
arr[iter->v]->next->next = (list *)0;
free(iter);
}
posa->next->next = (list *)0;
for(iter = posb->next->next; iter != (list *)0; iter = iter->next)
{
arr[iter->v]->next = (list *)malloc(sizeof(iter));
arr[iter->v]->next->v = iter->v;
arr[iter->v]->next->next = (list *)0;
free(iter);
}
posb->next->next = posa->next;
posa->next = (list *)0;
break;
case 0:
posa = find(a);
posb = find2(b);
if(posa == (list *)0 || posb == (list *)0)
continue;
for(iter = posa->next->next; iter != (list *)0; iter = iter->next)
{
arr[iter->v]->next = (list *)malloc(sizeof(iter));
arr[iter->v]->next->v = iter->v;
arr[iter->v]->next->next = (list *)0;
free(iter);
}
posa->next->next = (list *)0;
posb->next = posa->next;
posa->next = (list *)0;
break;
}
break;
case 0:
switch(t)
{
case 1:
posa = find(a);
posb = find(b);
if(posa == (list *)0 || posb == (list *)0)
continue;
for(iter = posb->next->next; iter != (list *)0; iter = iter->next)
{
arr[iter->v]->next = (list *)malloc(sizeof(iter));
arr[iter->v]->next->v = iter->v;
arr[iter->v]->next->next = (list *)0;
free(iter);
}
posb->next->next = posa->next;
posa->next = (list *)0;
break;
case 0:
posa = find(a);
posb = find2(b);
if(posa == (list *)0 || posb == (list *)0)
continue;
posb->next = posa->next;
posa->next = (list *)0;
break;
}
break;
}
}
for(i = 0; i < n; i++)
{
list *iter;
printf("%d:", i);
for(iter = arr->next; iter != (list *)0; iter = iter->next)
{
printf(" %d", iter->v);
}
printf("\n");
}
}

thanx once again

scruff
New poster
Posts: 29
Joined: Wed Dec 24, 2003 5:22 am

Post by scruff »

I didn't check your code but....

Your code should give you many errors when compiling. You have not included your malloc.h for all of you allocation and deallocation for your dynamic memory. string.h has also been left out from your includes.

Maybe you copy and pasted the wrong code but this should've been found. You should go and edit your preferences to email you the output from the judge. You can see the errors there and it would save time and effort of posting messages.

minus273
New poster
Posts: 6
Joined: Mon Jan 26, 2004 3:09 pm

Volume I #101 TLE

Post by minus273 »

[pascal]
program uva101;
{The blocks program}
var
b:packed array[1..25,0..25] of byte;
p:packed array[1..25] of byte;
stack:array[1..25] of byte;
size,i,j,k,stackp:word;
cmd,part1,part2:string;
op1,op2,tm1,tm2:word;
procedure mope(block,dest:word);
begin
dec(b[p[block],0]);
inc(b[dest,0]);
b[dest,b[dest,0]]:=block;
p[block]:=dest;
end;
procedure push(posi:word);
begin
inc(stackp);
stack[stackp]:=b[posi,b[posi,0]];
dec(b[posi,0]);
end;
procedure pop(posi:word);
begin
inc(b[posi,0]);
b[posi,b[posi,0]]:=stack[stackp];
p[stack[stackp]]:=posi;
dec(stackp);
end;
begin
readln(size);
{Initalize}
fillchar(b,sizeof(b),0);
stackp:=0;
for i:=1 to size do begin
b[i,0]:=1;
b[i,1]:=i;
p:=i;
end;
repeat
readln(cmd);
if cmd='quit' then
break;
part1:=copy(cmd,1,4);
cmd:=copy(cmd,5,30);
val(cmd,op1,tm1);
val(copy(cmd,1,tm1-1),op1,tm2);
cmd:=copy(cmd,tm1+1,30);
part2:=copy(cmd,1,4);
cmd:=copy(cmd,6,30);
val(cmd,op2,tm1);
{NOW HERE IS THE CODE}
if (part1='move') and (part2='onto') then begin
{MOVE a ONTO b}
{Clear a}
while b[p[op1],b[p[op1],0]]<>op1 do
mope(b[p[op1],b[p[op1],0]],b[p[op1],b[p[op1],0]]);
{Clear b}
while b[p[op2],b[p[op2],0]]<>op2 do
mope(b[p[op2],b[p[op2],0]],b[p[op2],b[p[op2],0]]);
mope(op1,p[op2]);
end else
if (part1='move') and (part2='over') then begin
{MOVE a OVER b}
{Clear a}
while b[p[op1],b[p[op1],0]]<>op1 do
mope(b[p[op1],b[p[op1],0]],b[p[op1],b[p[op1],0]]);
mope(op1,p[op2]);
end else
if (part1='pile') and (part2='onto') then begin
{MOVE a ONTO b}
{Clear b}
while b[p[op2],b[p[op2],0]]<>op2 do
mope(b[p[op2],b[p[op2],0]],b[p[op2],b[p[op2],0]]);
while b[p[op1],b[p[op1],0]]<>op1 do
push(p[op1]);
push(p[op1]);
while stackp>0 do
pop(p[op2]);
end else
if (part1='pile') and (part2='over') then begin
{MOVE a OVER b}
while b[p[op1],b[p[op1],0]]<>op1 do
push(p[op1]);
push(p[op1]);
while stackp>0 do
pop(p[op2]);
end;
until cmd='quit';
for i:=1 to size do begin
write(i,':');
if b[i,0]>0 then
for j:=1 to b[i,0] do
write(' ',b[i,j]);
writeln;
end;
end.
[/pascal]

Anyone tell me where my mistake was?
Il fait beau aujourd'hui.

minus273
New poster
Posts: 6
Joined: Mon Jan 26, 2004 3:09 pm

Post by minus273 »

Now NO TLE but WA...
[pascal]
program uva101;
{The blocks program}
var
b:packed array[0..24,0..25] of byte;
p:packed array[0..24] of byte;
stack:array[1..25] of byte;
size,i,j,k,stackp:word;
cmd,part1,part2:string;
op1,op2,tm1,tm2:word;
procedure mope(block,dest:word);
begin
dec(b[p[block],0]);
inc(b[dest,0]);
b[dest,b[dest,0]]:=block;
p[block]:=dest;
end;
procedure push(posi:word);
begin
inc(stackp);
stack[stackp]:=b[posi,b[posi,0]];
dec(b[posi,0]);
end;
procedure pop(posi:word);
begin
inc(b[posi,0]);
b[posi,b[posi,0]]:=stack[stackp];
p[stack[stackp]]:=posi;
dec(stackp);
end;
begin
readln(size);
{Initalize}
fillchar(b,sizeof(b),0);
stackp:=0;
for i:=0 to size-1 do begin
b[i,0]:=1;
b[i,1]:=i;
p:=i;
end;
repeat
readln(cmd);
if cmd='quit' then
break;
part1:=copy(cmd,1,4);
cmd:=copy(cmd,5,30);
val(cmd,op1,tm1);
val(copy(cmd,1,tm1-1),op1,tm2);
cmd:=copy(cmd,tm1+1,30);
part2:=copy(cmd,1,4);
cmd:=copy(cmd,6,30);
val(cmd,op2,tm1);
{NOW HERE IS THE CODE}
if (part1='move') and (part2='onto') then begin
{MOVE a ONTO b}
{Clear a}
while b[p[op1],b[p[op1],0]]<>op1 do
mope(b[p[op1],b[p[op1],0]],b[p[op1],b[p[op1],0]]);
{Clear b}
while b[p[op2],b[p[op2],0]]<>op2 do
mope(b[p[op2],b[p[op2],0]],b[p[op2],b[p[op2],0]]);
mope(op1,p[op2]);
end else
if (part1='move') and (part2='over') then begin
{MOVE a OVER b}
{Clear a}
while b[p[op1],b[p[op1],0]]<>op1 do
mope(b[p[op1],b[p[op1],0]],b[p[op1],b[p[op1],0]]);
mope(op1,p[op2]);
end else
if (part1='pile') and (part2='onto') then begin
{MOVE a ONTO b}
{Clear b}
while b[p[op2],b[p[op2],0]]<>op2 do
mope(b[p[op2],b[p[op2],0]],b[p[op2],b[p[op2],0]]);
while b[p[op1],b[p[op1],0]]<>op1 do
push(p[op1]);
push(p[op1]);
while stackp>0 do
pop(p[op2]);
end else
if (part1='pile') and (part2='over') then begin
{MOVE a OVER b}
while b[p[op1],b[p[op1],0]]<>op1 do
push(p[op1]);
push(p[op1]);
while stackp>0 do
pop(p[op2]);
end;
until cmd='quit';
for i:=0 to size-1 do begin
write(i,':');
if b[i,0]>0 then
for j:=1 to b[i,0] do
write(' ',b[i,j]);
writeln;
end;
end.
[/pascal]
Il fait beau aujourd'hui.

minus273
New poster
Posts: 6
Joined: Mon Jan 26, 2004 3:09 pm

Post by minus273 »

Anyone can help me?
Il fait beau aujourd'hui.

Post Reply

Return to “Volume 1 (100-199)”