101 - The Blocks Problem
Moderator: Board moderators
-
- Experienced poster
- Posts: 131
- Joined: Thu Apr 17, 2003 8:39 am
- Location: Baku, Azerbaijan
Please, somebody run own ACC program on this input:
Bye, Mattty
thx19
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
Bye, Mattty
My AC code gives:
Code: Select all
0: 0
1: 1 2
2:
3:
4:
5: 5 4 6
6:
7:
8: 8 7 9 3
9:
10: 10 11 12
11:
12:
13: 13
14: 14 17
15: 15
16: 16
17:
18: 18
101 problem with compilation
Can someone tell me why i have always copiler errror ("gcj: Internal compiler error: program jc1 got fatal signal 11
")? Id dont use any function & classes (only StringTokenizer , String ...) Judge accept my 100 problem but this one dont want to compile (i use SDK1.4 winXP , i dont have possibility to use linux compilers
)[java]
/* @JUDGE_ID: 37623MN 101 Java "Only tabs of int"*/
import java.io.*;
import java.util.*;
class Main
{
int floor[][];
int columnsSize[];
int size;
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-1));
}
public static void main (String args[])
{
Main myWork = new Main();
myWork.begin();
}
void begin()
{
String input;
StringTokenizer idata;
size = Integer.parseInt(Main.ReadLn(255));
floor = new int[size][2];
columnsSize = new int[size];
for(int i = 0; i < size ; i++)
{
floor[0] = i;
columnsSize = 1;
}
while (!(input = Main.ReadLn(255)).equals("quit"))
{
idata = new StringTokenizer(input);
String command = idata.nextToken();
int tmp = Integer.parseInt(idata.nextToken());
String moveType = idata.nextToken();
int base = Integer.parseInt(idata.nextToken());
if(command.equals("move"))
{
if(moveType.equals("over")){
moveOver(tmp,base);
}else{
moveOnto(tmp,base);
}
}else{
if(moveType.equals("over")){
pileOver(tmp,base);
}else{
pileOnto(tmp,base);
}
}
}
show();
}
void show()
{
int kol[][] = new int[size][0];
for(int i = 0 ; i < size ; i++)
{
int kolumna_dla_I = floor[0];
kol[kolumna_dla_I] = new int[columnsSize[kolumna_dla_I]];
}
for(int i = 0 ; i < size ; i++)
{
int kolumna_dla_I = floor[0];
int miejsce_dla_I = floor[1];
kol[kolumna_dla_I][miejsce_dla_I] = i;
}
for(int i = 0 ; i < size ; i++)
{
System.out.print(i+":");
for(int k = 0 ; k < kol.length ; k++)
{
System.out.print(" " + kol[k]);
}
System.out.println();
}
}
void moveOver(int tmp , int base)
{
int tmpOld[] = {floor[tmp][0],floor[tmp][1]};
floor[tmp][0] = floor[base][0];
floor[tmp][1] = columnsSize[floor[base][0]]++;
columnsSize[tmpOld[0]]--;
for(int i = 0 ; i < size ; i++)
{
if(floor[0]==tmpOld[0] && floor[1]>tmpOld[1])
{
floor[1]--;
}
}
}
void moveOnto(int tmp , int base)
{
int tmpOld[] = {floor[tmp][0],floor[tmp][1]};
floor[tmp][0] = floor[base][0];
floor[tmp][1] = floor[base][1]+1;
columnsSize[floor[base][0]]++;
columnsSize[tmpOld[0]]--;
for(int i = 0 ; i < size ; i++)
{
if(floor[i][0]==tmpOld[0] && floor[i][1]>tmpOld[1])
{
floor[i][1]--;
}
if(i != tmp && floor[i][0]==floor[tmp][0] &&
floor[i][1]>=floor[tmp][1])
{
floor[i][1]++;
}
}
}
void pileOver(int tmp , int base)
{
int count = columnsSize[floor[tmp][0]] - floor[tmp][1];
int start[] = {floor[tmp][0],floor[tmp][1]};
for(int i = 0 ; i < count ; i++)
{
boolean stoper = true;
for(int k = 0 ; k < size && stoper; k++)
{
if(start[0] == floor[k][0] && start[1] == floor[k][1])
{
moveOver(k,base);
stoper = false;
}
}
}
}
void pileOnto(int tmp , int base)
{
int count = columnsSize[floor[tmp][0]] - floor[tmp][1];
int toMove[] = new int[count];
for(int i=0 ; i < size ; i++)
{
if(floor[i][0] == floor[tmp][0] && floor[i][1] >= floor[tmp][1])
{
toMove[floor[i][1]-floor[tmp][1]] = i;
}
}
int target = base;
for(int i = 0 ; i < count ; i++)
{
moveOnto(toMove[i] , target);
target = toMove[i];
}
}
}
[/java]
")? Id dont use any function & classes (only StringTokenizer , String ...) Judge accept my 100 problem but this one dont want to compile (i use SDK1.4 winXP , i dont have possibility to use linux compilers

/* @JUDGE_ID: 37623MN 101 Java "Only tabs of int"*/
import java.io.*;
import java.util.*;
class Main
{
int floor[][];
int columnsSize[];
int size;
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-1));
}
public static void main (String args[])
{
Main myWork = new Main();
myWork.begin();
}
void begin()
{
String input;
StringTokenizer idata;
size = Integer.parseInt(Main.ReadLn(255));
floor = new int[size][2];
columnsSize = new int[size];
for(int i = 0; i < size ; i++)
{
floor[0] = i;
columnsSize = 1;
}
while (!(input = Main.ReadLn(255)).equals("quit"))
{
idata = new StringTokenizer(input);
String command = idata.nextToken();
int tmp = Integer.parseInt(idata.nextToken());
String moveType = idata.nextToken();
int base = Integer.parseInt(idata.nextToken());
if(command.equals("move"))
{
if(moveType.equals("over")){
moveOver(tmp,base);
}else{
moveOnto(tmp,base);
}
}else{
if(moveType.equals("over")){
pileOver(tmp,base);
}else{
pileOnto(tmp,base);
}
}
}
show();
}
void show()
{
int kol[][] = new int[size][0];
for(int i = 0 ; i < size ; i++)
{
int kolumna_dla_I = floor[0];
kol[kolumna_dla_I] = new int[columnsSize[kolumna_dla_I]];
}
for(int i = 0 ; i < size ; i++)
{
int kolumna_dla_I = floor[0];
int miejsce_dla_I = floor[1];
kol[kolumna_dla_I][miejsce_dla_I] = i;
}
for(int i = 0 ; i < size ; i++)
{
System.out.print(i+":");
for(int k = 0 ; k < kol.length ; k++)
{
System.out.print(" " + kol[k]);
}
System.out.println();
}
}
void moveOver(int tmp , int base)
{
int tmpOld[] = {floor[tmp][0],floor[tmp][1]};
floor[tmp][0] = floor[base][0];
floor[tmp][1] = columnsSize[floor[base][0]]++;
columnsSize[tmpOld[0]]--;
for(int i = 0 ; i < size ; i++)
{
if(floor[0]==tmpOld[0] && floor[1]>tmpOld[1])
{
floor[1]--;
}
}
}
void moveOnto(int tmp , int base)
{
int tmpOld[] = {floor[tmp][0],floor[tmp][1]};
floor[tmp][0] = floor[base][0];
floor[tmp][1] = floor[base][1]+1;
columnsSize[floor[base][0]]++;
columnsSize[tmpOld[0]]--;
for(int i = 0 ; i < size ; i++)
{
if(floor[i][0]==tmpOld[0] && floor[i][1]>tmpOld[1])
{
floor[i][1]--;
}
if(i != tmp && floor[i][0]==floor[tmp][0] &&
floor[i][1]>=floor[tmp][1])
{
floor[i][1]++;
}
}
}
void pileOver(int tmp , int base)
{
int count = columnsSize[floor[tmp][0]] - floor[tmp][1];
int start[] = {floor[tmp][0],floor[tmp][1]};
for(int i = 0 ; i < count ; i++)
{
boolean stoper = true;
for(int k = 0 ; k < size && stoper; k++)
{
if(start[0] == floor[k][0] && start[1] == floor[k][1])
{
moveOver(k,base);
stoper = false;
}
}
}
}
void pileOnto(int tmp , int base)
{
int count = columnsSize[floor[tmp][0]] - floor[tmp][1];
int toMove[] = new int[count];
for(int i=0 ; i < size ; i++)
{
if(floor[i][0] == floor[tmp][0] && floor[i][1] >= floor[tmp][1])
{
toMove[floor[i][1]-floor[tmp][1]] = i;
}
}
int target = base;
for(int i = 0 ; i < count ; i++)
{
moveOnto(toMove[i] , target);
target = toMove[i];
}
}
}
[/java]
maybe this would help
I think the result will be:
0: 0 1 4 6
1:
2: 5 2
3: 3
4:
5:
6:
0: 0 1 4 6
1:
2: 5 2
3: 3
4:
5:
6:
my code gives:
my question is, how do 7 9 3 go over 8 in the end?
Code: Select all
0: 0
1: 1 2
2:
3:
4:
5: 5 4 6
6:
7: 7 9 3
8: 8
9:
10: 10 11 12
11:
12:
13: 13
14: 14 17
15: 15
16: 16
17:
18: 18
101 WA Could somebody tell me why it is something wrong?
Here is my c++ source code,and I have test many input datas.
-----------------------------------------------------------------------------
/*
*purpose:solve the blocks problem (uva 101)
*/
#include<stdio.h>
int block[25][27];
int n;
void print();
void init(void)
{
int i;
for( i=0;i<n; i++)
{
block[0]=-1;
block[1]=0;
}
}
void remove(int a,int start)
{
int k,l,temp;
l=block[a][1]+2;
for(k=start;k<l;k++)
{
temp=block[a][k];
block[temp][0]=-1;
block[temp][1]=0;
}
block[a][1]-=l-start;
}
void moveOnto(int a,int b)
{
// printf("move %d onto %d\n",a,b);
int temp1,temp2;
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
if(block[0]>-1)
{
temp2=block[1]+1;
remove(block[0],temp2);
temp1=block[0];
block[a][0]=block[0];
block[a][1]=temp2;
block[temp1][temp2]=a;
block[temp1][1]++;
}
else
{
if(block[1]>0)
remove(b,2);
block[a][0]=b;
block[a][1]=2;
block[1]=1;
block[2]=a;
}
// print();
}
void moveOver(int a,int b)
{
int k,l;
// printf("move %d over %d\n",a,b);
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
k=block[0];
if(k>-1)
b=block[0];
block[b][1]++;
l=block[b][1]+1;
block[b][l]=a;
block[a][0]=b;
block[a][1]=l;
// print();
}
void pileOnto(int a,int b)
{
int k,l,s,j,t,temp;
// printf("pile %d onto %d\n",a,b);
if(block[b][0]>-1)
{
remove(block[b][0],block[b][1]);
}
else
{
if(block[b][1]>0)
remove(b,2);
}
if(block[a][0]==-1)
{
s=a;
k=2;
block[b][2]=a;
block[b][1]++;
block[a][0]=b;
l=block[a][1]+2;
block[a][1]=2;
t=3;
}
else
{
s=block[a][0];
k=block[a][1];
l=block[s][1]+2;
j=block[b][1]+2;
t=2;
}
while(k<l)
{
temp=block[s][k++];
block[temp][0]=b;
block[temp][1]=t;
block[b][t++]=temp;
block[b][1]++;
block[s][1]--;
}
// print();
}
void pileOver(int a,int b)
{
int i,j,k,l,s,temp;
// printf("pile %d over %d\n",a,b);
if(block[a][0]==-1 )
{
if(block[b][0]==a)
return;
if(block[b][0]>-1)
b=block[b][0];
j=block[b][1]+2;
block[b][j]=a;
j++;
k=block[a][1]+2;
for(i=2;i<k;i++)
{
temp=block[a];
block[temp][0]=b;
block[temp][1]=j;
block[b][j++]=temp;
}
block[a][0]=b;
block[a][1]=2;
block[b][1]+=k-1;
return;
}
s=block[a][0];
if(block[b][0]==s)
return;
if(block[b][0]>0)
b=block[b][0];
l=block[s][1]+2;
k=block[b][1]+2;
block[s][1]-=l-block[a][1];
for(i=block[a][1];i<l ;i++)
{
temp=block[s];
block[temp][0]=b;
block[temp][1]=k;
block[b][k++]=temp;
block[b][1]++;
}
// print();
}
void print(void)
{
int i,j,l;
for(i=0; i<n; i++)
{
printf("%d:",i);
if(block[0]==-1)
{
printf(" %d",i);
l=block[1]+2;
for(j=2;j<l;j++)
printf(" %d",block[j]);
}
printf("\n");
}
}
bool DoCommand(void)
{
int a,b,i;
char op1,op2;
char s[20];
fflush(stdin);
gets(s);
op1=s[0];
if( op1=='q' )
return false;
i=1;
while(!((s>='0')&&(s<='9')))
i++;
a=s-'0';
i++;
if((s[i]>='0')&&(s[i]<='9'))
{
a=a*10+(s[i]-'0');
i++;
}
i++;
op2=s[i+1];
while(!((s[i]>='0')&&(s[i]<='9')))
i++;
b=s[i]-'0';
i++;
if((s[i]>='0')&&(s[i]<='9'))
b=b*10+(s[i]-'0');
if(op1=='m')
{
if(op2=='n')
moveOnto(a,b);
else
moveOver(a,b);
}
else
{
if(op2=='n')
pileOnto(a,b);
else
pileOver(a,b);
}
return true;
}
void main(void)
{
scanf("%d",&n);
init();
while(DoCommand());
print();
}
-----------------------------------------------------------------------------
/*
*purpose:solve the blocks problem (uva 101)
*/
#include<stdio.h>
int block[25][27];
int n;
void print();
void init(void)
{
int i;
for( i=0;i<n; i++)
{
block[0]=-1;
block[1]=0;
}
}
void remove(int a,int start)
{
int k,l,temp;
l=block[a][1]+2;
for(k=start;k<l;k++)
{
temp=block[a][k];
block[temp][0]=-1;
block[temp][1]=0;
}
block[a][1]-=l-start;
}
void moveOnto(int a,int b)
{
// printf("move %d onto %d\n",a,b);
int temp1,temp2;
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
if(block[0]>-1)
{
temp2=block[1]+1;
remove(block[0],temp2);
temp1=block[0];
block[a][0]=block[0];
block[a][1]=temp2;
block[temp1][temp2]=a;
block[temp1][1]++;
}
else
{
if(block[1]>0)
remove(b,2);
block[a][0]=b;
block[a][1]=2;
block[1]=1;
block[2]=a;
}
// print();
}
void moveOver(int a,int b)
{
int k,l;
// printf("move %d over %d\n",a,b);
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
k=block[0];
if(k>-1)
b=block[0];
block[b][1]++;
l=block[b][1]+1;
block[b][l]=a;
block[a][0]=b;
block[a][1]=l;
// print();
}
void pileOnto(int a,int b)
{
int k,l,s,j,t,temp;
// printf("pile %d onto %d\n",a,b);
if(block[b][0]>-1)
{
remove(block[b][0],block[b][1]);
}
else
{
if(block[b][1]>0)
remove(b,2);
}
if(block[a][0]==-1)
{
s=a;
k=2;
block[b][2]=a;
block[b][1]++;
block[a][0]=b;
l=block[a][1]+2;
block[a][1]=2;
t=3;
}
else
{
s=block[a][0];
k=block[a][1];
l=block[s][1]+2;
j=block[b][1]+2;
t=2;
}
while(k<l)
{
temp=block[s][k++];
block[temp][0]=b;
block[temp][1]=t;
block[b][t++]=temp;
block[b][1]++;
block[s][1]--;
}
// print();
}
void pileOver(int a,int b)
{
int i,j,k,l,s,temp;
// printf("pile %d over %d\n",a,b);
if(block[a][0]==-1 )
{
if(block[b][0]==a)
return;
if(block[b][0]>-1)
b=block[b][0];
j=block[b][1]+2;
block[b][j]=a;
j++;
k=block[a][1]+2;
for(i=2;i<k;i++)
{
temp=block[a];
block[temp][0]=b;
block[temp][1]=j;
block[b][j++]=temp;
}
block[a][0]=b;
block[a][1]=2;
block[b][1]+=k-1;
return;
}
s=block[a][0];
if(block[b][0]==s)
return;
if(block[b][0]>0)
b=block[b][0];
l=block[s][1]+2;
k=block[b][1]+2;
block[s][1]-=l-block[a][1];
for(i=block[a][1];i<l ;i++)
{
temp=block[s];
block[temp][0]=b;
block[temp][1]=k;
block[b][k++]=temp;
block[b][1]++;
}
// print();
}
void print(void)
{
int i,j,l;
for(i=0; i<n; i++)
{
printf("%d:",i);
if(block[0]==-1)
{
printf(" %d",i);
l=block[1]+2;
for(j=2;j<l;j++)
printf(" %d",block[j]);
}
printf("\n");
}
}
bool DoCommand(void)
{
int a,b,i;
char op1,op2;
char s[20];
fflush(stdin);
gets(s);
op1=s[0];
if( op1=='q' )
return false;
i=1;
while(!((s>='0')&&(s<='9')))
i++;
a=s-'0';
i++;
if((s[i]>='0')&&(s[i]<='9'))
{
a=a*10+(s[i]-'0');
i++;
}
i++;
op2=s[i+1];
while(!((s[i]>='0')&&(s[i]<='9')))
i++;
b=s[i]-'0';
i++;
if((s[i]>='0')&&(s[i]<='9'))
b=b*10+(s[i]-'0');
if(op1=='m')
{
if(op2=='n')
moveOnto(a,b);
else
moveOver(a,b);
}
else
{
if(op2=='n')
pileOnto(a,b);
else
pileOver(a,b);
}
return true;
}
void main(void)
{
scanf("%d",&n);
init();
while(DoCommand());
print();
}
i have passed it use pascal,and i do check my C++ source code for many
times!Is there some trick with input/output format?Could any body know that?
here is my new C++ source code:
------------------------------------------------------------
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int block[25][27];
int n;
void print();
void init(void)
{
int i;
for( i=0;i<n; i++)
{
block[0]=-1;
block[1]=0;
}
}
void remove(int a,int start)
{
int k,l,temp;
l=block[a][1]+2;
for(k=start;k<l;k++)
{
temp=block[a][k];
block[temp][0]=-1;
block[temp][1]=0;
block[a][1]--;
}
}
void moveOnto(int a,int b)
{
// printf("move %d onto %d\n",a,b);
int temp1,temp2;
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
if(block[0]>-1)
{
temp2=block[1]+1;
remove(block[0],temp2);
temp1=block[0];
block[a][0]=block[0];
block[a][1]=temp2;
block[temp1][temp2]=a;
block[temp1][1]++;
}
else
{
if(block[1]>0)
remove(b,2);
block[a][0]=b;
block[a][1]=2;
block[1]=1;
block[2]=a;
}
// print();
}
void moveOver(int a,int b)
{
int k,l;
// printf("move %d over %d\n",a,b);
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
k=block[0];
if(k>-1)
b=block[0];
block[b][1]++;
l=block[b][1]+1;
block[b][l]=a;
block[a][0]=b;
block[a][1]=l;
// print();
}
void pileOnto(int a,int b)
{
int k,l,s,j,temp;//from (s,j)->(b,k);
// printf("pile %d onto %d\n",a,b);
if(block[b][0]>-1)
{
remove(block[b][0],block[b][1]+1);
}
else
{
if(block[b][1]>0)
remove(b,2);
}
if(block[b][0]==-1)
{
k=2;
}
else
{
b=block[b][0];
k=block[b][1]+2;
}
if(block[a][0]==-1)
{
s=a;
j=2;
l=block[a][1]+2;
block[a][0]=b;
block[a][1]=k;
block[b][k++]=a;
block[b][1]++;
}
else
{
s=block[a][0];
j=block[a][1];
l=block[block[a][0]][1]+2;
}
while(j<l)
{
temp=block[s][j++];
block[temp][0]=b;
block[temp][1]=k;
block[b][k++]=temp;
block[b][1]++;
if(s!=a)
block[s][1]--;
}
// print();
}
void pileOver(int a,int b)
{
int i,j,k,l,s,temp;
// printf("pile %d over %d\n",a,b);
if(block[a][0]==-1 )
{
if(block[b][0]>-1)
b=block[b][0];
j=block[b][1]+2;
block[b][j]=a;
j++;
k=block[a][1]+2;
for(i=2;i<k;i++)
{
temp=block[a];
block[temp][0]=b;
block[temp][1]=j;
block[b][j++]=temp;
}
block[a][0]=b;
block[a][1]=2;
block[b][1]+=k-1;
// print();
return;
}
s=block[a][0];
if(block[b][0]>0)
b=block[b][0];
k=block[b][1]+2;
l=block[s][1]+2;
for(i=block[a][1];i<l ;i++)
{
temp=block[s];
block[temp][0]=b;
block[temp][1]=k;
block[b][k++]=temp;
block[b][1]++;
block[s][1]--;
}
// print();
}
void print(void)
{
int i,j,l;
for(i=0; i<n; i++)
{
printf("%d:",i);
if(block[0]==-1)
{
printf(" %d",i);
l=block[1]+2;
for(j=2;j<l;j++)
printf(" %d",block[j]);
}
printf("\n");
}
}
bool DoCommand(void)
{
int a,b;
char cmd1[5]="XXXX";
char cmd2[5]="XXXX";
// fflush(stdin);
scanf("%s %d %s %d",cmd1,&a,cmd2,&b);
if(strcmp(cmd1,"quit")==0)
return false;
if((a==b)||(a<0)||(a>=n)||(b<0)||(b>=n))
return true;
if((block[a][0]>-1)&&(block[b][0]>-1)&&block[a][0]==block[b][0])
return true;
if( (block[a][0]==-1)&&(block[b][0]==a) || (block[b][0]==-1)&&(block[a][0]==b) )
return true;
if(strcmp(cmd1,"move")==0)
{
if(strcmp(cmd2,"onto")==0)
moveOnto(a,b);
else if(strcmp(cmd2,"over")==0)
moveOver(a,b);
}
else if(strcmp(cmd1,"pile")==0)
{
if(strcmp(cmd2,"onto")==0)
pileOnto(a,b);
else if(strcmp(cmd2,"over")==0)
pileOver(a,b);
}
return true;
}
int main(void)
{
scanf("%d",&n);
if((n<1)||(n>24))
return 0;
init();
while(DoCommand());
print();
return 0;
}
times!Is there some trick with input/output format?Could any body know that?
here is my new C++ source code:
------------------------------------------------------------
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int block[25][27];

int n;
void print();
void init(void)
{
int i;
for( i=0;i<n; i++)
{
block[0]=-1;
block[1]=0;
}
}
void remove(int a,int start)
{
int k,l,temp;
l=block[a][1]+2;
for(k=start;k<l;k++)
{
temp=block[a][k];
block[temp][0]=-1;
block[temp][1]=0;
block[a][1]--;
}
}
void moveOnto(int a,int b)
{
// printf("move %d onto %d\n",a,b);
int temp1,temp2;
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
if(block[0]>-1)
{
temp2=block[1]+1;
remove(block[0],temp2);
temp1=block[0];
block[a][0]=block[0];
block[a][1]=temp2;
block[temp1][temp2]=a;
block[temp1][1]++;
}
else
{
if(block[1]>0)
remove(b,2);
block[a][0]=b;
block[a][1]=2;
block[1]=1;
block[2]=a;
}
// print();
}
void moveOver(int a,int b)
{
int k,l;
// printf("move %d over %d\n",a,b);
if(block[a][0]>-1)
{
remove(block[a][0],block[a][1]);
}
else
{
if(block[a][1]>0)
remove(a,2);
}
k=block[0];
if(k>-1)
b=block[0];
block[b][1]++;
l=block[b][1]+1;
block[b][l]=a;
block[a][0]=b;
block[a][1]=l;
// print();
}
void pileOnto(int a,int b)
{
int k,l,s,j,temp;//from (s,j)->(b,k);
// printf("pile %d onto %d\n",a,b);
if(block[b][0]>-1)
{
remove(block[b][0],block[b][1]+1);
}
else
{
if(block[b][1]>0)
remove(b,2);
}
if(block[b][0]==-1)
{
k=2;
}
else
{
b=block[b][0];
k=block[b][1]+2;
}
if(block[a][0]==-1)
{
s=a;
j=2;
l=block[a][1]+2;
block[a][0]=b;
block[a][1]=k;
block[b][k++]=a;
block[b][1]++;
}
else
{
s=block[a][0];
j=block[a][1];
l=block[block[a][0]][1]+2;
}
while(j<l)
{
temp=block[s][j++];
block[temp][0]=b;
block[temp][1]=k;
block[b][k++]=temp;
block[b][1]++;
if(s!=a)
block[s][1]--;
}
// print();
}
void pileOver(int a,int b)
{
int i,j,k,l,s,temp;
// printf("pile %d over %d\n",a,b);
if(block[a][0]==-1 )
{
if(block[b][0]>-1)
b=block[b][0];
j=block[b][1]+2;
block[b][j]=a;
j++;
k=block[a][1]+2;
for(i=2;i<k;i++)
{
temp=block[a];
block[temp][0]=b;
block[temp][1]=j;
block[b][j++]=temp;
}
block[a][0]=b;
block[a][1]=2;
block[b][1]+=k-1;
// print();
return;
}
s=block[a][0];
if(block[b][0]>0)
b=block[b][0];
k=block[b][1]+2;
l=block[s][1]+2;
for(i=block[a][1];i<l ;i++)
{
temp=block[s];
block[temp][0]=b;
block[temp][1]=k;
block[b][k++]=temp;
block[b][1]++;
block[s][1]--;
}
// print();
}
void print(void)
{
int i,j,l;
for(i=0; i<n; i++)
{
printf("%d:",i);
if(block[0]==-1)
{
printf(" %d",i);
l=block[1]+2;
for(j=2;j<l;j++)
printf(" %d",block[j]);
}
printf("\n");
}
}
bool DoCommand(void)
{
int a,b;
char cmd1[5]="XXXX";
char cmd2[5]="XXXX";
// fflush(stdin);
scanf("%s %d %s %d",cmd1,&a,cmd2,&b);
if(strcmp(cmd1,"quit")==0)
return false;
if((a==b)||(a<0)||(a>=n)||(b<0)||(b>=n))
return true;
if((block[a][0]>-1)&&(block[b][0]>-1)&&block[a][0]==block[b][0])
return true;
if( (block[a][0]==-1)&&(block[b][0]==a) || (block[b][0]==-1)&&(block[a][0]==b) )
return true;
if(strcmp(cmd1,"move")==0)
{
if(strcmp(cmd2,"onto")==0)
moveOnto(a,b);
else if(strcmp(cmd2,"over")==0)
moveOver(a,b);
}
else if(strcmp(cmd1,"pile")==0)
{
if(strcmp(cmd2,"onto")==0)
pileOnto(a,b);
else if(strcmp(cmd2,"over")==0)
pileOver(a,b);
}
return true;
}
int main(void)
{
scanf("%d",&n);
if((n<1)||(n>24))
return 0;
init();
while(DoCommand());
print();
return 0;
}
101 - Boxes Problem
Hi, this is my first prog submission, read the submission rules and dont know why this is a CE; is it the ifstream?
Thx.
[cpp]
#include <iostream>
#include <assert.h>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <conio.h>
#include <iomanip>
using namespace std;
class Blockworld
{
private:
int n[25][25];
int spaces;
public:
Blockworld(int);
void moveonto(int, int);
void returnBlocks(int);
void moveover(int, int);
void moveExact(int, int);
void returnpos(int, int &, int &);
void pileonto(int, int);
void pileover(int, int);
void print(int, int);
void printList();
};
Blockworld::Blockworld(int a)
{
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 25; j++)
{
n[j] = 0;
}
}
for (int k = 0; k < 25; k++)
{
n[k][0] = k;
n[k][1] = k;
}
spaces = a - 1;
}
void Blockworld::returnpos(int v, int &a, int &b)
{
//Find where a is.
for (int i = 1; i <=spaces ; i++)
{
for (int j = 1; j <=spaces; j++)
{
if (n[j] == v)
{
a = i;
b = j;
}
}
}
}
void Blockworld::returnBlocks(int a)
{
int ai, aj, temp;
(*this).returnpos(a, ai, aj);
for(int i = spaces; i > aj; i--)
{
if (n[ai] != 0)
{
temp = n[ai];
int j = spaces;
while((j > 0) && (n[temp][j] == 0))
{
j--;
}
n[temp][j+1] = temp;
n[ai] = 0;
}
}
}
void Blockworld::moveonto(int a, int b)
{
int ai, aj, bi, bj;
(*this).returnpos(a, ai, aj);
(*this).returnpos(b, bi, bj);
(*this).returnBlocks(a);
(*this).returnBlocks(b);
for(int i = spaces; i > 0; i--)
{
if (n[a] != 0)
{
int temp = n[a];
int k = spaces;
while ((k >= 0) && (n[bi][k] == 0))
{
k--;
}
n[bi][k + 1] = n[a];
n[a] = 0;
}
}
}
void Blockworld::moveover(int a, int b)
{
int ai, aj, bi, bj;
(*this).returnpos(a, ai, aj);
(*this).returnpos(b, bi, bj);
(*this).returnBlocks(a);
for(int i = spaces; i > 0; i--)
{
if (n[a] != 0)
{
int temp = n[a][i];
int k = spaces;
while ((k > 0) && (n[bi][k] == 0))
{
k--;
}
n[bi][k + 1] = n[a][i];
n[a][i] = 0;
}
}
}
void Blockworld::moveExact(int a, int b)
{
int ai, aj, bi, bj;
(*this).returnpos(a, ai, aj);
(*this).returnpos(b, bi, bj);
if (ai != bi)
{
for (int i = aj; i <= spaces; i++)
{
int j = spaces;
if (n[ai][i] != 0)
{
while ((j >=0) && (n[bi][j] == 0))
{
j--;
}
n[bi][j+1] = n[ai][i];
n[ai][i] = 0;
}
}
}
}
void Blockworld::pileonto(int a, int b)
{
int ai, aj;
(*this).returnpos(a, ai, aj);
(*this).returnBlocks(b);
(*this).moveExact(a, b);
}
void Blockworld::pileover(int a, int b)
{
(*this).moveExact(a, b);
}
void Blockworld::print(int a, int b)
{
cout << "(" << a << "," << b << ") = ";
cout << n[a] << endl;
}
void Blockworld::printList()
{
int i = 0;
while(i <= spaces)
{
cout << setw(5) << n[i][0] << ":";
for(int k = 1; k <= spaces; k++)
{
if (n[i][k] != 0)
cout << n[i][k] << " ";
if ((n[i][k] == 0) && (k == 1) && (i == 0))
cout << n[0][k] << " ";
}
i++;
cout << endl;
}
cout << endl;
}
int main()
{
ofstream fout ("sampleout.txt");
ifstream fin ("samplein.txt");
int spaces, number1, number2;
string word1, word2, word3;
string s;
fin >> spaces;
Blockworld a(spaces);
while (fin >> word1 >> number1 >> word2 >> number2)
{
if (word2 == "onto")
{
if (word1 == "move")
a.moveonto(number1, number2);
else
a.pileonto(number1, number2);
}
else if (word2 == "over")
{
if (word1 == "move")
a.moveover(number1, number2);
else
a.pileover(number1, number2);
}
else if (word1 != "quit")
{
a.printList();
return 0;
}
}
a.printList();
return 0;
}
[/cpp]
Thx.
[cpp]
#include <iostream>
#include <assert.h>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <conio.h>
#include <iomanip>
using namespace std;
class Blockworld
{
private:
int n[25][25];
int spaces;
public:
Blockworld(int);
void moveonto(int, int);
void returnBlocks(int);
void moveover(int, int);
void moveExact(int, int);
void returnpos(int, int &, int &);
void pileonto(int, int);
void pileover(int, int);
void print(int, int);
void printList();
};
Blockworld::Blockworld(int a)
{
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 25; j++)
{
n[j] = 0;
}
}
for (int k = 0; k < 25; k++)
{
n[k][0] = k;
n[k][1] = k;
}
spaces = a - 1;
}
void Blockworld::returnpos(int v, int &a, int &b)
{
//Find where a is.
for (int i = 1; i <=spaces ; i++)
{
for (int j = 1; j <=spaces; j++)
{
if (n[j] == v)
{
a = i;
b = j;
}
}
}
}
void Blockworld::returnBlocks(int a)
{
int ai, aj, temp;
(*this).returnpos(a, ai, aj);
for(int i = spaces; i > aj; i--)
{
if (n[ai] != 0)
{
temp = n[ai];
int j = spaces;
while((j > 0) && (n[temp][j] == 0))
{
j--;
}
n[temp][j+1] = temp;
n[ai] = 0;
}
}
}
void Blockworld::moveonto(int a, int b)
{
int ai, aj, bi, bj;
(*this).returnpos(a, ai, aj);
(*this).returnpos(b, bi, bj);
(*this).returnBlocks(a);
(*this).returnBlocks(b);
for(int i = spaces; i > 0; i--)
{
if (n[a] != 0)
{
int temp = n[a];
int k = spaces;
while ((k >= 0) && (n[bi][k] == 0))
{
k--;
}
n[bi][k + 1] = n[a];
n[a] = 0;
}
}
}
void Blockworld::moveover(int a, int b)
{
int ai, aj, bi, bj;
(*this).returnpos(a, ai, aj);
(*this).returnpos(b, bi, bj);
(*this).returnBlocks(a);
for(int i = spaces; i > 0; i--)
{
if (n[a] != 0)
{
int temp = n[a][i];
int k = spaces;
while ((k > 0) && (n[bi][k] == 0))
{
k--;
}
n[bi][k + 1] = n[a][i];
n[a][i] = 0;
}
}
}
void Blockworld::moveExact(int a, int b)
{
int ai, aj, bi, bj;
(*this).returnpos(a, ai, aj);
(*this).returnpos(b, bi, bj);
if (ai != bi)
{
for (int i = aj; i <= spaces; i++)
{
int j = spaces;
if (n[ai][i] != 0)
{
while ((j >=0) && (n[bi][j] == 0))
{
j--;
}
n[bi][j+1] = n[ai][i];
n[ai][i] = 0;
}
}
}
}
void Blockworld::pileonto(int a, int b)
{
int ai, aj;
(*this).returnpos(a, ai, aj);
(*this).returnBlocks(b);
(*this).moveExact(a, b);
}
void Blockworld::pileover(int a, int b)
{
(*this).moveExact(a, b);
}
void Blockworld::print(int a, int b)
{
cout << "(" << a << "," << b << ") = ";
cout << n[a] << endl;
}
void Blockworld::printList()
{
int i = 0;
while(i <= spaces)
{
cout << setw(5) << n[i][0] << ":";
for(int k = 1; k <= spaces; k++)
{
if (n[i][k] != 0)
cout << n[i][k] << " ";
if ((n[i][k] == 0) && (k == 1) && (i == 0))
cout << n[0][k] << " ";
}
i++;
cout << endl;
}
cout << endl;
}
int main()
{
ofstream fout ("sampleout.txt");
ifstream fin ("samplein.txt");
int spaces, number1, number2;
string word1, word2, word3;
string s;
fin >> spaces;
Blockworld a(spaces);
while (fin >> word1 >> number1 >> word2 >> number2)
{
if (word2 == "onto")
{
if (word1 == "move")
a.moveonto(number1, number2);
else
a.pileonto(number1, number2);
}
else if (word2 == "over")
{
if (word1 == "move")
a.moveover(number1, number2);
else
a.pileover(number1, number2);
}
else if (word1 != "quit")
{
a.printList();
return 0;
}
}
a.printList();
return 0;
}
[/cpp]
Your program is reading off from a file. All programs regardless of language are supposed to read from the standard input stream (stdin) and write to the standard output stream (stdout).
My ideas on solving programming problems are 91% common sense, practicality and luck, 8% pure knowledge, and 1% extreme Math.
HELP on 101 [Run time error but it runs fine in my comp!!!]
Code: Select all
[c]/*************************************************
_
_ |_|
|_| |_|
|_| _ |_|
|_| |_| |_|
|_| |_| |_| - pBlockRun
| | _ |
|_|-|_|-|_|-|_|
|
pPosHead
This is my program representation of the blocks.
I used linked list to connect the blocks.
**************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char string4[5];
struct BlockNodeTag
{
int nBlockNo;
struct BlockNodeTag *pLink;
};
typedef struct BlockNodeTag structBlockType;
typedef structBlockType *ptrBlockNode;
struct PosNodeTag
{
int nthPos; /* number of blocks a position has */
ptrBlockNode pBlockLink;
struct PosNodeTag *pPosLink;
};
typedef struct PosNodeTag structPosType;
typedef structPosType *ptrPosNode;
void getInput(ptrPosNode pPosHead);
void init(ptrPosNode *pPosHead,int nMax);
void process(ptrPosNode pPosHead,string4 strMove,int nA,string4 strPos,int nB);
void move(ptrPosNode pPosHead,ptrBlockNode *pBlockTempHead,int nA);
void pile(ptrPosNode pPosHead,ptrBlockNode *pBlockTempHead,int nA);
void onto(ptrPosNode pPosHead,ptrBlockNode pBlockTempHead,int nB);
void over(ptrPosNode pPosHead,ptrBlockNode pBlockTempHead,int nB);
void return_above(ptrPosNode pPosHead,int nKey);
void dispOutput(ptrPosNode pPosHead);
main()
{
int nMax;
ptrPosNode pPosHead;
scanf("%d",&nMax);
init(&pPosHead,nMax);
getInput(pPosHead);
dispOutput(pPosHead);
}
void getInput(ptrPosNode pPosHead)
{
int nA,
nB;
string4 strMove,
strPos;
do
{
scanf("%s",strMove);
if(strcmp(strMove,"quit") != 0)
{
scanf("%d%s%d",&nA,strPos,&nB);
process(pPosHead,strMove,nA,strPos,nB);
}
}while(strcmp(strMove,"quit") != 0);
}
void init(ptrPosNode *pPosHead,int nMax)
{
int i;
ptrBlockNode pBlockHead;
ptrPosNode pPosRun,
pPosTrail;
pPosRun = *pPosHead;
for(i=0 ; i<=nMax-1 ; i++)
{
pBlockHead = malloc(sizeof(structBlockType));
pBlockHead->nBlockNo = i;
pBlockHead->pLink = NULL;
pPosRun = malloc(sizeof(ptrPosNode));
pPosRun->nthPos = i;
pPosRun->pBlockLink = pBlockHead;
if(i == 0)
*pPosHead = pPosRun;
else
pPosTrail->pPosLink = pPosRun;
pPosTrail = pPosRun;
pPosRun->pPosLink = NULL;
}
}
void process(ptrPosNode pPosHead,string4 strMove,int nA,string4 strPos,int nB)
{
int nPosA,nPosB;
ptrPosNode pPosRun;
ptrBlockNode pBlockRun,
pBlockTempHead;
pPosRun= pPosHead;
while(pPosRun != NULL)
{
pBlockRun = pPosRun->pBlockLink;
while(pBlockRun != NULL)
{
if(pBlockRun->nBlockNo == nA)
nPosA = pPosRun->nthPos;
if(pBlockRun->nBlockNo == nB)
nPosB = pPosRun->nthPos;
pBlockRun = pBlockRun->pLink;
}
pPosRun = pPosRun->pPosLink;
}
if(nPosA != nPosB)
{
pBlockTempHead = NULL;
if(strcmp(strMove,"move") == 0)
move(pPosHead,&pBlockTempHead,nA);
else if(strcmp(strMove,"pile") == 0)
pile(pPosHead,&pBlockTempHead,nA);
if(strcmp(strPos,"onto") == 0)
onto(pPosHead,pBlockTempHead,nB);
else if(strcmp(strPos,"over") == 0)
over(pPosHead,pBlockTempHead,nB);
pBlockTempHead = NULL;
}
}
void move(ptrPosNode pPosHead,ptrBlockNode *pBlockTempHead,int nA)
{
int bFound;
ptrPosNode pPosRun;
ptrBlockNode pBlockRun,
pBlockTrail;
return_above(pPosHead,nA);
pPosRun = pPosHead;
bFound = 0;
/* picks up the block nA */
while(pPosRun != NULL && !bFound)
{
pBlockTrail = NULL;
pBlockRun = pPosRun->pBlockLink;
while(pBlockRun != NULL && !bFound)
{
if(pBlockRun->nBlockNo == nA)
{
*pBlockTempHead = pBlockRun;
if(pBlockTrail == NULL)
pPosRun->pBlockLink = NULL;
else
pBlockTrail->pLink = NULL;
bFound = 1;
}
pBlockTrail = pBlockRun;
pBlockRun = pBlockRun->pLink;
}
pPosRun = pPosRun->pPosLink;
}
}
void pile(ptrPosNode pPosHead,ptrBlockNode *pBlockTempHead,int nA)
{
int bFound;
ptrPosNode pPosRun;
ptrBlockNode pBlockRun,
pBlockTrail;
bFound = 0;
pPosRun = pPosHead;
/* picks up block nA and blocks above it */
while(pPosRun != NULL && !bFound)
{
pBlockRun = pPosRun->pBlockLink;
pBlockTrail = NULL;
while(pBlockRun != NULL && !bFound)
{
if(pBlockRun->nBlockNo == nA)
{
*pBlockTempHead = pBlockRun;
if(pBlockTrail == NULL)
pPosRun->pBlockLink = NULL;
else
pBlockTrail->pLink = NULL;
bFound = 1;
}
else
{
pBlockTrail = pBlockRun;
pBlockRun = pBlockRun->pLink;
}
}
pPosRun = pPosRun->pPosLink;
}
}
void onto(ptrPosNode pPosHead,ptrBlockNode pBlockTempHead,int nB)
{
int bFound;
ptrPosNode pPosRun;
ptrBlockNode pBlockRun;
return_above(pPosHead,nB);
/* puts block nA on top of block nB */
pPosRun = pPosHead;
bFound = 0;
while(pPosRun != NULL && !bFound)
{
pBlockRun = pPosRun->pBlockLink;
while(pBlockRun != NULL && !bFound)
{
if(pBlockRun->nBlockNo == nB)
{
pBlockRun->pLink = pBlockTempHead;
bFound = 1;
}
else
pBlockRun = pBlockRun->pLink;
}
pPosRun = pPosRun->pPosLink;
}
}
void over(ptrPosNode pPosHead,ptrBlockNode pBlockTempHead,int nB)
{
int bFound;
ptrPosNode pPosRun;
ptrBlockNode pBlockRun;
/* puts block nA on top of stack of block nB */
pPosRun = pPosHead;
bFound = 0;
while(pPosRun != NULL && !bFound)
{
pBlockRun = pPosRun->pBlockLink;
if(pBlockRun != NULL)
{
if(pBlockRun->pLink == NULL)
if(pBlockRun->nBlockNo == nB)
bFound = 1;
else
pBlockRun = pBlockRun->pLink;
else
while(pBlockRun->pLink != NULL )
{
if(pBlockRun->nBlockNo == nB)
bFound = 1;
pBlockRun = pBlockRun->pLink;
}
if(bFound)
pBlockRun->pLink = pBlockTempHead;
}
pPosRun = pPosRun->pPosLink;
}
}
void return_above(ptrPosNode pPosHead,int nKey)
{
int bFound,
bAllReturned;
ptrPosNode pPosRun;
ptrBlockNode pBlockRun,
pReturnList; /* linked list of blocks to be returned */
pPosRun = pPosHead;
bFound = 0;
while(pPosRun != NULL && !bFound)
{
pBlockRun = pPosRun->pBlockLink;
while(pBlockRun != NULL && !bFound)
{
if(pBlockRun->nBlockNo == nKey)
{
pReturnList = pBlockRun->pLink;
pBlockRun->pLink = NULL;
bFound = 1;
}
else
pBlockRun = pBlockRun->pLink;
}
pPosRun = pPosRun->pPosLink;
}
bAllReturned = 0;
if(pReturnList != NULL)
{
pPosRun = pPosHead;
while(!bAllReturned)
{
if(pPosRun->nthPos == pReturnList->nBlockNo)
{
pBlockRun = pPosRun->pBlockLink;
if(pBlockRun != NULL)
{
while(pBlockRun->pLink != NULL)
pBlockRun = pBlockRun->pLink;
pBlockRun->pLink = pReturnList;
pReturnList = pReturnList->pLink;
pBlockRun->pLink->pLink = NULL;
}
else
{
pPosRun->pBlockLink = pReturnList;
pReturnList = pReturnList->pLink;
pPosRun->pBlockLink->pLink = NULL;
}
}
if(pReturnList == NULL)
bAllReturned = 1;
if(pPosRun == NULL)
pPosRun = pPosHead;
else
pPosRun = pPosRun->pPosLink;
}
}
}
void dispOutput(ptrPosNode pPosHead)
{
ptrPosNode pPosRun;
ptrBlockNode pBlockRun;
pPosRun = pPosHead;
while(pPosRun != NULL)
{
printf("%d:",pPosRun->nthPos);
pBlockRun = pPosRun->pBlockLink;
while(pBlockRun != NULL)
{
printf(" %d",pBlockRun->nBlockNo);
pBlockRun = pBlockRun->pLink;
}
printf("\n");
pPosRun = pPosRun->pPosLink;
}
}
[/c]
-
- New poster
- Posts: 12
- Joined: Sun Nov 09, 2003 1:27 am
- Location: East West University, Dhaka.
- Contact:
When we run our program in TC we usually don't get runtime error. If we compile our code in gcc then we can easily find the problem. Please compile ur code in gcc and then look what it says. Another thing for run time error is to access wrong area such as
or if we what to access the address that is out of our allocated memory then we will get runtime error. Please check for these things. Hopefully u will get accepted.
Niaz
str = '0' [ but i = -1]
or if we what to access the address that is out of our allocated memory then we will get runtime error. Please check for these things. Hopefully u will get accepted.
Niaz
The Last Man Standing 
