Re: 101 The Blocks Problem - Runtime Error
Posted: Fri Nov 09, 2012 2:23 am
Thx i hadn't realized that, but the problem is that it doesn't even mark wrong answer it shows a Runtime Error, it doesn't even run
Code: Select all
2
move 0 over 1
move 1 over 0
quit
Code: Select all
0:
1: 1 0
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct POSITION
{
int p,t;
} POSITION;
int blocks[100][100],n,top[100]={0},a,b;
POSITION pos[100];
void ret(int target)
{
int i,tmp;
for (i=pos[target].t+1;i<=top[pos[target].p];i++)
{
tmp=blocks[pos[target].p][i];
blocks[tmp][0]=tmp;top[tmp]=0;
pos[tmp].p=tmp;pos[tmp].t=0;
}
top[pos[target].p]=pos[target].t;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("UVA101.in","r",stdin);
#endif
int i,j,judgep,tmp;
POSITION bak;
char s[100]="",num[100]="";
scanf ("%d\n",&n);
memset (blocks,0,sizeof(blocks));
for (i=0;i<n;i++)
{
blocks[i][0]=i;
pos[i].p=i;pos[i].t=0;
}
while (gets(s) && s[0]!='q')
{
memset(num,0,sizeof(num));
for (i=5;s[i]!=' ';i++) num[i-5]=s[i];num[i-4]='\0';a=atoi(num);
for (judgep=strlen(s)-1;s[judgep]!=' ';judgep--);
memset(num,0,sizeof(num));
for (i=judgep+1;i<strlen(s);i++) num[i-judgep-1]=s[i];num[i-judgep]='\0';
b=atoi(num);judgep--;
if ((a>=0) && (a<n) && (b>=0) && (b<n) && !(a==b || pos[a].p==pos[b].p))
{
if (s[0]=='m')
{
ret(a);
if (s[judgep]=='o') ret(b);
blocks[pos[b].p][++top[pos[b].p]]=a;
top[pos[a].p]--;
pos[a].p=pos[b].p;pos[a].t=top[pos[b].p];
}
else
{
if (s[judgep]=='o') ret(b);
blocks[pos[b].p][++top[pos[b].p]]=a;
bak=pos[a];
for (i=pos[a].t+1;i<=top[pos[a].p];i++)
{
tmp=blocks[pos[a].p][i];
blocks[pos[b].p][++top[pos[b].p]]=tmp;
pos[tmp].p=pos[b].p;pos[tmp].t=top[pos[b].p];
}
pos[a].p=pos[b].p;pos[a].t=pos[b].t+1;
top[bak.p]=bak.t-1;
}
}
}
for (i=0;i<n;i++)
{
printf ("%d:",i);
for (j=0;j<=top[i];j++) printf (" %d",blocks[i][j]);
printf ("\n");
}
return 0;
}
Code: Select all
scanf ("%d\n",&n);
Code: Select all
24
move 2 over 8
pile 11 onto 18
pile 13 onto 15
pile 22 over 15
move 17 over 11
move 12 over 7
pile 4 onto 17
move 19 over 22
pile 11 over 18
move 21 onto 4
pile 23 onto 9
pile 13 over 15
move 6 over 16
move 21 over 12
move 9 onto 5
move 21 onto 11
move 14 onto 7
pile 18 over 9
pile 1 over 12
move 3 over 13
pile 7 onto 4
pile 14 over 1
move 19 over 1
pile 9 over 20
pile 22 onto 17
pile 2 over 17
move 11 over 1
move 19 onto 15
move 6 onto 15
move 6 onto 22
pile 17 onto 9
move 11 onto 22
pile 19 onto 6
move 7 onto 3
pile 20 onto 3
move 15 onto 8
move 18 onto 21
move 21 onto 21
pile 14 over 4
pile 18 over 16
move 18 over 12
pile 9 onto 2
pile 12 onto 7
move 4 onto 8
pile 18 onto 10
move 3 onto 18
move 18 over 1
pile 6 over 13
pile 3 over 4
pile 17 onto 19
move 22 onto 15
move 5 over 19
pile 19 over 4
move 22 over 17
move 23 over 5
pile 15 over 21
move 5 over 11
move 3 over 11
pile 19 over 20
pile 8 onto 22
pile 0 over 12
pile 4 onto 2
move 23 onto 18
pile 8 over 6
move 15 over 13
pile 9 over 1
pile 1 over 15
pile 20 onto 11
pile 12 over 4
move 9 over 23
move 17 over 11
pile 13 onto 6
move 19 onto 21
move 14 over 21
pile 5 onto 15
pile 9 over 2
move 22 over 20
pile 5 onto 21
pile 22 over 17
move 22 onto 0
move 23 over 15
pile 19 onto 9
pile 19 onto 7
pile 23 onto 4
pile 10 onto 0
move 18 over 4
move 4 onto 21
move 1 over 11
pile 2 over 19
pile 17 onto 9
pile 22 onto 3
pile 11 over 8
move 3 over 3
pile 15 over 8
move 18 over 17
move 13 over 0
pile 3 onto 13
pile 2 onto 11
move 7 over 0
move 14 over 11
move 3 over 10
pile 6 over 0
move 3 over 14
pile 22 onto 0
pile 10 onto 9
move 15 over 14
move 18 onto 10
move 13 over 2
pile 18 onto 17
pile 7 over 4
pile 1 onto 16
move 10 over 11
move 6 onto 23
move 21 onto 15
pile 15 over 2
pile 12 over 5
pile 1 over 0
move 3 over 16
pile 22 over 21
move 6 over 1
pile 12 onto 7
pile 12 onto 14
move 10 onto 12
move 5 over 14
pile 12 onto 20
pile 20 over 13
pile 6 onto 6
move 20 over 17
move 23 over 23
pile 7 onto 14
move 21 over 7
pile 18 onto 0
pile 18 onto 7
move 17 onto 20
pile 22 onto 19
pile 6 onto 23
move 17 over 7
move 21 onto 7
pile 12 onto 6
move 23 over 4
pile 16 over 6
pile 21 onto 6
move 18 over 13
move 12 over 20
pile 11 onto 2
move 7 onto 1
pile 11 over 2
move 13 onto 18
pile 5 onto 19
pile 5 onto 16
pile 18 over 14
pile 22 over 5
pile 18 over 17
move 11 onto 1
move 14 over 18
move 12 onto 4
pile 10 onto 13
move 21 onto 19
move 3 onto 12
move 0 over 23
pile 17 over 10
pile 15 onto 13
move 21 over 5
move 14 onto 8
pile 5 onto 4
pile 10 onto 18
move 13 over 17
pile 15 onto 21
pile 20 onto 11
pile 7 over 11
pile 20 onto 4
pile 9 onto 22
pile 23 over 8
pile 0 onto 13
pile 9 over 23
move 7 over 22
move 4 over 7
pile 15 over 23
move 12 over 9
pile 23 over 16
move 4 over 22
move 15 onto 21
pile 19 over 21
move 3 onto 1
pile 3 onto 7
pile 9 over 22
pile 16 over 9
move 2 onto 23
pile 6 onto 14
move 9 over 16
move 9 over 0
move 13 over 6
move 22 onto 1
pile 7 onto 12
move 0 onto 6
move 0 over 15
move 19 onto 13
move 20 onto 6
move 12 onto 7
pile 4 onto 21
pile 22 onto 17
move 6 onto 9
move 20 over 7
pile 8 onto 12
move 9 onto 4
pile 21 over 7
move 7 over 13
pile 10 over 10
pile 14 onto 15
move 17 over 16
pile 0 onto 3
move 18 over 11
move 7 over 12
move 13 onto 10
move 12 over 15
pile 8 over 8
pile 2 onto 7
move 1 onto 19
pile 14 over 10
move 18 over 13
move 2 onto 6
move 2 over 7
pile 18 onto 16
move 1 onto 9
pile 4 over 16
move 3 onto 8
move 22 onto 0
move 7 onto 12
pile 14 over 18
move 10 onto 21
pile 12 onto 4
pile 2 over 16
pile 15 onto 4
move 15 over 1
move 12 over 16
pile 21 over 9
move 15 over 12
move 15 over 20
move 11 onto 19
pile 0 over 1
move 14 onto 13
move 4 over 7
pile 12 onto 4
pile 20 over 19
pile 1 over 15
move 19 onto 8
pile 18 onto 2
move 6 onto 14
move 23 onto 14
pile 8 onto 2
move 10 over 18
pile 21 over 23
move 20 over 2
pile 23 over 11
pile 0 over 0
move 9 over 17
pile 19 over 14
move 8 over 1
pile 4 onto 8
move 16 onto 6
move 17 over 17
pile 6 over 2
pile 12 onto 8
move 12 onto 23
pile 21 onto 8
pile 1 onto 16
pile 10 onto 5
move 4 over 5
pile 12 over 23
pile 16 over 19
move 1 over 10
move 8 over 14
move 18 onto 13
pile 5 over 10
pile 13 onto 13
move 4 onto 21
move 20 onto 5
pile 19 over 19
move 8 over 23
move 19 onto 12
move 11 over 17
move 8 over 11
move 23 onto 22
pile 4 over 19
move 12 onto 18
move 8 over 5
move 15 onto 9
pile 19 onto 12
pile 12 over 12
pile 5 over 14
move 7 onto 4
move 17 over 3
move 18 onto 22
move 3 over 11
move 7 onto 0
pile 23 over 3
pile 7 over 11
pile 21 over 17
pile 19 over 5
pile 8 onto 23
move 6 over 17
pile 6 onto 9
pile 20 onto 19
move 19 onto 21
move 2 over 1
pile 18 onto 15
pile 2 onto 16
pile 0 over 3
move 17 onto 15
pile 23 onto 12
move 13 over 14
move 4 onto 3
move 13 onto 21
pile 21 over 20
move 8 over 1
pile 20 over 6
move 11 onto 21
pile 0 onto 3
pile 17 over 21
pile 19 onto 13
pile 7 over 0
move 3 over 4
move 7 over 20
pile 1 over 12
pile 8 onto 9
pile 21 onto 17
pile 10 onto 14
move 14 over 23
pile 1 onto 1
pile 15 over 14
move 4 onto 2
move 9 onto 22
move 6 onto 11
move 19 onto 3
pile 23 onto 18
pile 18 onto 12
pile 20 over 13
pile 18 over 23
pile 20 over 13
pile 17 onto 5
pile 22 onto 23
pile 22 over 3
pile 16 over 22
pile 2 onto 12
move 7 onto 3
pile 7 onto 10
pile 0 onto 9
move 13 onto 3
pile 6 over 15
move 0 onto 10
pile 16 over 4
pile 15 onto 10
pile 17 over 5
move 2 onto 4
move 20 onto 16
move 22 over 3
pile 12 over 1
pile 0 onto 4
pile 0 over 22
pile 20 over 19
move 19 onto 18
move 8 over 9
pile 11 onto 12
pile 7 over 7
move 21 onto 9
move 16 onto 15
move 12 onto 18
pile 10 onto 2
move 16 over 4
pile 20 over 12
pile 11 over 13
pile 9 onto 15
move 18 over 15
move 19 over 14
pile 7 over 13
move 13 onto 0
move 11 onto 1
move 13 onto 18
move 21 onto 14
move 15 onto 22
pile 7 onto 23
pile 6 onto 18
pile 21 onto 14
pile 8 over 21
move 0 onto 17
move 17 over 23
pile 10 onto 4
move 18 over 15
pile 0 over 14
pile 2 over 3
move 12 over 7
move 1 onto 15
pile 3 onto 5
pile 2 over 19
pile 21 onto 20
move 17 over 8
move 11 over 7
pile 8 onto 3
pile 23 onto 18
move 9 over 18
move 4 onto 3
move 23 onto 7
pile 17 over 21
move 5 onto 12
move 9 over 4
move 23 over 6
pile 13 onto 13
move 5 over 11
pile 0 onto 23
pile 2 over 0
pile 9 onto 4
pile 17 over 0
pile 10 over 11
move 12 onto 14
move 20 onto 23
pile 7 over 13
pile 8 onto 4
move 18 over 2
pile 16 over 19
move 14 onto 1
pile 7 over 15
move 23 over 7
move 19 onto 17
move 3 over 19
pile 10 over 18
move 4 over 5
pile 0 onto 7
pile 13 onto 21
move 9 over 15
pile 11 onto 0
move 16 over 17
pile 4 over 20
pile 5 over 17
pile 8 onto 9
pile 19 over 5
move 22 over 20
pile 10 onto 0
pile 21 over 17
pile 14 over 12
pile 3 over 23
move 1 over 7
pile 14 over 22
pile 7 over 5
move 11 onto 23
move 3 over 17
pile 17 over 10
move 16 onto 14
pile 10 onto 11
move 5 over 15
move 6 onto 16
move 6 onto 8
pile 16 onto 15
move 9 onto 19
move 6 onto 11
move 10 over 22
pile 11 onto 20
pile 2 onto 8
move 18 onto 11
pile 17 onto 21
move 15 over 21
move 0 over 20
move 5 over 2
move 15 over 0
move 9 over 11
move 10 onto 20
move 20 over 1
pile 11 over 0
pile 2 over 3
pile 8 over 0
pile 10 onto 1
move 1 onto 14
pile 9 over 16
pile 1 over 9
pile 13 onto 21
move 15 onto 9
pile 6 over 0
pile 3 over 0
move 8 onto 14
move 19 over 19
pile 7 onto 2
move 22 over 17
pile 15 over 20
pile 2 onto 8
move 13 over 13
pile 8 over 14
move 19 over 5
move 20 onto 9
pile 18 onto 13
move 17 onto 4
move 7 over 18
pile 13 over 13
pile 6 onto 21
pile 14 over 20
pile 8 over 19
move 20 over 4
move 14 onto 14
pile 10 over 1
pile 18 onto 13
move 20 onto 1
move 6 over 19
pile 20 onto 6
pile 10 onto 9
pile 23 onto 7
pile 5 over 20
pile 13 onto 16
pile 15 over 15
move 18 onto 11
pile 19 onto 1
move 13 onto 1
move 0 over 22
move 4 over 6
move 14 over 1
move 11 onto 22
pile 15 over 17
pile 8 over 9
move 22 over 3
pile 6 over 19
move 3 onto 0
pile 5 onto 21
pile 17 over 19
pile 4 over 3
move 19 over 11
pile 8 over 8
pile 13 onto 23
move 22 onto 5
move 3 over 8
move 3 over 7
pile 0 over 7
pile 8 over 2
move 11 over 1
pile 23 over 6
move 1 onto 1
move 17 over 10
pile 0 onto 0
pile 12 over 6
move 1 over 3
move 23 onto 18
pile 7 onto 14
move 4 onto 7
move 19 over 19
pile 1 onto 3
move 11 over 10
move 7 onto 9
pile 23 onto 14
pile 5 onto 19
pile 8 onto 10
move 5 over 21
pile 0 over 17
move 0 over 14
move 17 onto 6
pile 8 over 14
pile 14 over 9
pile 16 onto 6
pile 13 onto 10
move 2 onto 20
pile 12 over 4
move 4 onto 17
move 12 onto 4
pile 20 onto 22
pile 18 over 11
move 7 over 9
move 18 onto 21
pile 13 onto 16
move 19 over 11
pile 8 onto 10
move 15 onto 12
pile 23 over 17
pile 6 onto 5
move 23 over 10
move 9 over 2
move 6 over 0
pile 5 over 21
pile 14 onto 10
move 13 over 19
pile 16 over 20
pile 18 over 4
pile 0 over 13
move 13 onto 9
move 13 over 23
pile 12 over 22
pile 7 over 10
move 11 onto 15
pile 9 onto 21
pile 18 over 0
pile 17 onto 18
move 3 onto 1
move 19 onto 13
move 9 onto 19
move 7 onto 5
pile 20 over 0
pile 4 over 13
pile 0 over 3
pile 18 over 19
move 22 over 4
move 18 onto 14
pile 20 over 23
pile 9 onto 0
pile 23 onto 4
pile 6 onto 13
pile 19 over 4
move 20 onto 3
move 1 onto 11
pile 14 onto 15
pile 16 over 1
pile 3 onto 16
move 15 over 9
pile 17 onto 4
move 8 over 7
move 19 over 1
pile 17 over 1
pile 5 over 19
pile 23 onto 18
move 9 over 3
pile 13 over 2
pile 20 over 21
pile 14 over 8
move 4 over 14
pile 7 over 15
pile 21 onto 10
move 3 onto 8
move 23 over 8
pile 16 over 0
pile 21 onto 3
pile 2 onto 18
move 20 over 12
pile 11 onto 18
move 18 over 9
move 7 onto 11
pile 20 over 21
move 21 over 17
pile 23 over 17
pile 3 over 10
move 9 onto 13
move 4 over 18
pile 17 onto 4
move 4 onto 19
pile 1 onto 6
move 4 over 2
move 20 onto 21
pile 2 over 1
move 7 over 7
pile 10 onto 21
move 3 over 2
pile 9 onto 15
move 23 onto 10
move 2 over 13
move 4 over 4
move 17 over 3
pile 2 over 2
pile 0 over 4
pile 19 over 15
pile 15 over 23
pile 4 onto 18
pile 12 over 1
move 6 over 8
move 11 over 17
move 9 over 21
move 7 onto 0
pile 10 over 20
pile 14 onto 8
pile 18 onto 2
move 5 onto 7
move 4 onto 16
move 19 over 21
pile 13 onto 0
move 23 onto 7
pile 18 onto 7
pile 3 onto 0
pile 6 onto 10
pile 11 onto 1
move 2 over 23
move 15 onto 10
pile 12 over 4
pile 13 onto 4
move 4 onto 16
pile 21 over 8
pile 4 onto 23
move 20 over 3
move 3 over 7
pile 22 over 18
move 11 over 5
pile 8 onto 21
pile 17 onto 1
pile 21 onto 2
pile 23 over 10
pile 3 onto 7
move 4 over 22
pile 11 onto 14
pile 3 over 10
pile 22 onto 8
move 7 over 12
pile 22 onto 21
pile 13 onto 2
move 5 over 5
move 11 onto 10
move 8 onto 13
pile 8 onto 11
pile 16 onto 2
pile 8 over 20
pile 7 over 13
pile 15 onto 11
move 16 onto 15
move 23 onto 16
move 2 over 19
pile 9 over 16
pile 22 over 2
move 21 onto 13
move 19 onto 4
pile 21 over 11
pile 8 onto 17
pile 20 onto 3
move 16 over 0
pile 4 over 6
pile 4 onto 18
pile 7 over 12
pile 12 over 15
pile 7 onto 15
move 22 onto 5
move 6 onto 7
move 11 onto 23
move 2 over 14
pile 15 over 23
pile 5 over 8
pile 6 onto 4
pile 6 onto 5
move 22 over 8
pile 22 over 22
pile 3 onto 4
pile 2 onto 6
pile 19 over 16
pile 3 onto 20
pile 22 over 23
pile 19 onto 0
pile 6 over 23
pile 22 onto 10
move 15 onto 20
pile 18 onto 22
move 4 onto 5
pile 16 over 21
pile 17 onto 14
pile 16 over 17
move 22 over 12
pile 23 over 16
move 5 over 17
pile 13 over 7
pile 21 onto 17
move 20 onto 8
move 4 over 16
move 15 onto 3
move 9 onto 22
pile 7 over 2
move 0 onto 16
move 1 onto 14
pile 12 onto 13
pile 23 onto 18
move 18 over 20
move 21 onto 7
move 14 onto 23
move 2 onto 1
move 22 over 14
pile 1 over 8
move 15 onto 19
move 17 onto 6
pile 21 onto 19
pile 23 over 21
pile 11 over 0
move 20 over 5
move 1 onto 19
move 5 over 4
pile 16 over 7
pile 4 over 6
move 16 onto 22
pile 1 onto 11
move 15 over 16
move 16 over 18
move 23 onto 21
move 21 over 19
pile 5 onto 2
move 22 over 3
move 22 over 4
pile 11 over 18
pile 23 onto 1
pile 21 onto 20
pile 10 over 13
pile 18 over 22
pile 0 onto 22
move 22 over 8
pile 12 over 3
pile 1 over 12
pile 2 onto 1
pile 0 over 18
pile 0 onto 23
pile 23 onto 4
move 8 over 12
move 14 over 13
pile 9 onto 1
pile 20 over 14
move 18 over 15
move 14 onto 7
pile 18 onto 15
move 1 onto 17
move 5 over 8
pile 1 over 16
pile 21 over 14
move 16 onto 1
move 10 over 14
move 15 onto 5
pile 3 onto 17
pile 11 onto 13
pile 21 onto 11
pile 21 over 16
move 11 onto 12
move 9 over 12
pile 4 over 14
pile 21 over 22
move 6 onto 4
pile 15 onto 8
pile 8 onto 11
pile 16 over 16
pile 9 over 22
pile 15 over 11
pile 10 over 7
pile 14 onto 14
move 5 onto 15
pile 4 over 14
move 12 over 11
move 0 onto 22
pile 20 over 14
pile 17 over 17
move 19 onto 9
pile 2 onto 5
pile 0 over 2
move 23 onto 7
move 22 over 13
move 9 onto 7
move 16 onto 21
move 1 over 13
move 3 over 5
pile 20 over 7
pile 12 over 8
pile 14 over 23
move 23 onto 7
pile 8 over 7
move 8 onto 12
pile 15 onto 23
move 10 over 11
pile 6 over 22
pile 0 over 5
move 19 onto 8
move 10 onto 4
pile 16 over 1
pile 13 onto 22
pile 1 over 11
pile 20 over 2
pile 6 onto 23
move 8 onto 0
move 5 onto 22
move 15 over 21
pile 12 over 13
move 8 onto 7
move 18 over 7
move 10 onto 1
move 2 onto 18
move 5 over 23
pile 15 onto 5
move 1 over 6
pile 1 onto 0
pile 1 onto 13
move 3 onto 2
move 0 onto 18
pile 2 onto 23
move 23 onto 22
move 18 onto 17
pile 21 over 1
pile 22 over 5
move 12 onto 4
move 11 over 14
pile 21 onto 16
move 2 onto 9
pile 10 onto 1
pile 15 onto 6
move 21 over 0
move 18 over 12
move 6 over 22
pile 15 onto 1
move 0 over 6
move 20 onto 8
move 21 over 5
pile 10 onto 16
pile 8 onto 23
move 10 onto 8
move 15 onto 22
move 3 over 13
pile 9 over 9
pile 8 onto 20
pile 19 onto 1
move 12 over 20
pile 16 onto 9
pile 3 onto 14
move 12 onto 15
pile 2 onto 0
move 7 onto 21
pile 19 onto 19
pile 15 onto 19
pile 9 onto 21
pile 1 onto 0
move 4 over 6
move 4 over 0
pile 15 onto 10
move 0 onto 22
pile 13 onto 14
pile 14 onto 6
quit
Code: Select all
0:
1: 1
2: 2
3: 3
4:
5: 5 22 0
6: 6 14 13
7: 7
8:
9:
10: 10 15 12 4
11: 11
12:
13:
14:
15:
16:
17: 17
18: 18
19: 19
20: 20 8
21: 21 9 16
22:
23: 23
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stackTag {
int *list;
int numOfElements;
};
typedef char String[10];
void initialize(struct stackTag list[],int size);
void push(struct stackTag *S, int i);
int pop(struct stackTag *S);
int top(struct stackTag *S);
int findStack(struct stackTag list[],int key,int size);
void removeEl(struct stackTag *s,int key);
void initialize(struct stackTag list[],int size);
void put(struct stackTag *s,int key);
void push(struct stackTag *S, int i) {
*(S->list + S->numOfElements) = i;
++(S->numOfElements);
}
int pop(struct stackTag *S) {
return *(S->list + --S->numOfElements);
}
int top(struct stackTag *S) {
return *(S->list + (S->numOfElements - 1));
}
int findStack(struct stackTag list[],int key,int size) {
int i;
for (i = 0 ; i < size; i++) {
int j;
for ( j = 0 ; j < list[i].numOfElements; j++)
if (*(list[i].list+j) == key)
return i;
}
return -1;
}
void removeEl(struct stackTag *s,int key) {
int i = 0;
while(*(s->list+i) != key)
i++;
for(; i < (s->numOfElements) - 1; i++)
*(s->list+i) = *(s->list+i+1);
--(s->numOfElements);
}
void initialize(struct stackTag list[],int size) {
int i;
for (i = 0; i < size; i++) {
list[i].list = malloc(sizeof(int) * size);
*(list[i].list + 0) = i;
list[i].numOfElements = 1;
}
}
void put(struct stackTag *s,int key) {
int i;
for(i = 0 ; i < (s->numOfElements) - 1;i++) {
*(s-> list + i + 1) = *(s->list + i);
}
*(s->list) = key;
++(s->numOfElements);
}
int main() {
int size;
scanf("%d", &size);
struct stackTag list[size];
initialize(list,size);
String word1,word2;
int num1,num2;
do {
scanf("%s", word1);
if (strcmp(word1,"quit") != 0){
scanf("%d %s %d", &num1, word2, &num2);
} else {
break;
}
if(findStack(list,num1,size) != findStack(list,num2,size && num1 != num2)) {
if(strcmp(word1,"move") == 0){
if(strcmp(word2,"onto") == 0){
struct stackTag temp;
temp.numOfElements = 0;
temp.list = malloc(sizeof(int) * size);
int i = findStack(list,num2,size);
int k = findStack(list,num1,size);;
while(top(&list[i]) != num2) {
int y = pop(&list[i]);
put(list + y,y);
}
push(list + i,num1);
while(top(&list[k]) != num1) {
int y = pop(&list[k]);
put(list + y,y);
}
removeEl(list + k,num1);
free(temp.list);
} else if(strcmp(word2,"over") == 0){
int k = findStack(list,num1,size);
push(list + findStack(list,num2,size), num1);
removeEl(list + k, num1);
}
} else if (strcmp(word1,"pile") == 0) {
if(strcmp(word2,"onto") == 0){
struct stackTag temp1;
temp1.numOfElements = 0;
temp1.list = malloc(sizeof(int) * size);
int k1 = findStack(list,num1,size);
int k2 = findStack(list,num2,size);
while(top(&list[k2]) != num2){
int y = pop(list + k2);
put(list + y,y);
}
while(top(list+k1) != num1)
push(&temp1,pop(list+k1));
push(&temp1,pop(list+k1));
while(temp1.numOfElements != 0)
push(&list[k2],pop(&temp1));
free(temp1.list);
} else if(strcmp(word2,"over") == 0){
struct stackTag temp;
temp.numOfElements = 0;
temp.list = malloc(sizeof(int)*size);
int k1 = findStack(list,num1,size);
int k2 = findStack(list,num2,size);
while(top(list+k1) != num1)
push(&temp,pop(list + k1));
push(&temp,pop(list+k1));
while(temp.numOfElements != 0)
push(list + k2,pop(&temp));
free(temp.list);
}
}
}
}while(strcmp(word1,"quit") != 0);
int a;
for (a = 0; a < size; a++) {
int b = 0;
printf("%d:",a);
while(b < list[a].numOfElements){
printf(" %d",*(list[a].list+b));
++b;
}
printf("\n");
}
int u;
for(u = 0; u < size; u++)
free(list[u].list);
return 0;
}
Code: Select all
#include <iostream>
#include <vector>
#include <string>
#define DEBUG 1
void init(std::vector<int> world[], int size);
void print(std::vector<int> [], int);
void move(std::vector<int> [], int size, int a, int b, std::string mod);
void pile(std::vector<int> [], int, int, int, std::string);
int find(std::vector<int> [], int size, int target);
int main(void)
{
using namespace std;
int n = 25;
(cin >> n).get();
vector<int> stack[n];
init(stack, n);
string cmd, mod;
int a, b;
while (true)
{
cin >> cmd;
if (cmd == "quit")
break;
(cin >> a).get();
cin >> mod;
(cin >> b).get();
if (cmd == "move")
move(stack,n,a,b,mod);
else
pile(stack,n,a,b,mod);
if (DEBUG)
{
cout << endl << cmd << " " << a << " " << mod << " " << b << endl;
print(stack,n);
}
}
if (DEBUG)
cout << endl << "Final State" << endl;
print (stack, n);
return 0;
}
void init(std::vector<int> world[], int size)
{
for (int i = 0; i < size; i++)
world[i].push_back(i);
}
void print(std::vector<int> world[], int size)
{
for (int i = 0; i < size; i++)
{
std::cout << i << ":";
for (int j = 0; j < world[i].size(); j++)
std::cout << " " << world[i].at(j);
std::cout << std::endl;
}
}
void move(std::vector<int> world[], int size, int a, int b, std::string mod)
{
int pos_a = find(world, size, a);
int pos_b = find(world, size, b);
if (a==b) return;
if (pos_a == pos_b)
return;
while (world[pos_a].back() != a)
{
int temp = world[pos_a].back();
world[pos_a].pop_back();
world[temp].push_back(temp);
}
if (mod == "onto")
{
while (world[pos_b].back() != b)
{
int temp = world[pos_b].back();
world[pos_b].pop_back();
world[temp].push_back(temp);
}
}
world[pos_b].push_back(world[pos_a].back());
world[pos_a].pop_back();
}
void pile(std::vector<int> world[], int size, int a, int b, std::string mod)
{
int pos_a = find(world, size, a);
int pos_b = find(world, size, b);
if (a==b) return;
if (pos_a == pos_b)
return;
std::vector<int> rev;
while (!world[pos_a].empty())
{
rev.push_back(world[pos_a].back());
world[pos_a].pop_back();
if (rev.back() == a)
break;
}
if (mod == "onto")
{
while (world[pos_b].back() != b)
{
int temp = world[pos_b].back();
world[pos_b].pop_back();
world[temp].push_back(temp);
}
}
while (!rev.empty())
{
world[pos_b].push_back(rev.back());
rev.pop_back();
}
}
int find(std::vector<int> world[],int size, int target)
{
int pos = -1;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < world[i].size(); j++)
{
if (world[i].at(j) == target)
return i;
}
}
std::cout << target << " not found in block world" << std::endl;
return pos;
}