Posted: Sun Feb 09, 2003 3:25 am
Here is my code. It is basically simple stack manipulation.
[c]#include <stdio.h>
#ifndef ONLINE_JUDGE
#include <fcntl.h>
#endif
#define FALSE 0
#define TRUE 1
#define NIL 26
typedef struct b_type
{
int block_number;
int stack_number;
struct b_type* block_below;
} block_type;
typedef struct s_type
{
block_type* top;
} stack_type;
block_type blocks[25];
stack_type stacks[25];
void push_single(int s, int b)
{
blocks.block_below = stacks[s].top;
stacks[s].top = &blocks;
blocks.stack_number = s;
}
int pop_single(int s)
{
int b;
if(stacks[s].top == NULL)
{
printf("NIL!\n");
exit(1);
return NIL;
}
else
{
b = stacks[s].top->block_number;
stacks[s].top = blocks.block_below;
blocks.block_below = NULL;
return b;
}
}
void push_stack(int s, int b, int top)
{
block_type* blockP;
blocks.block_below = stacks[s].top;
stacks[s].top = &blocks[top];
blockP = stacks[s].top;
while(blockP->block_number != b)
{
blockP->stack_number = s;
blockP = blockP->block_below;
}
blocks.stack_number = s;
}
int pop_stack(int s, int b)
{
int top;
if((stacks[s].top == NULL) || (blocks.stack_number != s))
{
printf("NIL!\n");
return NIL;
}
else
{
top = stacks[s].top->block_number;
stacks[s].top = blocks.block_below;
blocks.block_below = NULL;
return top;
}
}
void initialize_blocks(int number_of_blocks)
{
int i;
for(i=0; i<number_of_blocks; i++)
{
stacks.top = NULL;
blocks.block_number = i;
blocks.stack_number = NULL;
blocks.block_below = NULL;
push_single(i, i);
}
}
void output_state(int number_of_blocks)
{
int numbers[25];
int i, j, k;
block_type* blockP;
for(i=0; i<number_of_blocks; i++)
{
printf("%d:", i);
blockP = stacks.top;
j=0;
while(blockP != NULL)
{
numbers[j] = blockP->block_number;
j++;
blockP = blockP->block_below;
}
j--;
for(; j>=0; j--)
{
printf(" %d", numbers[j]);
}
printf("\n");
}
}
void return_to_initial_position(int b)
{
int s;
s = blocks[b].stack_number;
pop_single(s);
push_single(b, b);
}
void return_blocks_above(int b)
{
int s;
int c;
s = blocks[b].stack_number;
while(stacks[s].top != &blocks[b])
{
c = pop_single(s);
push_single(c, c);
}
}
int main(int argc, char* argv[])
{
int number_of_blocks;
int a;
int b;
int s, top;
char command[5] = {0,0,0,0,0};
char on_over[5] = {0,0,0,0,0};
int a_over_b;
int b_over_a;
#ifndef ONLINE_JUDGE
close (0); open (argv[1], O_RDONLY);
close (1); open ("myprog.out", O_WRONLY | O_CREAT, 0600);
#endif
scanf("%d\n", &number_of_blocks);
if((number_of_blocks <= 0) || (number_of_blocks >= 25))
{
exit(0);
/* This shouldn't happen because problem statement said it
wouldn't */
}
initialize_blocks(number_of_blocks);
while(scanf("%s %d %s %d\n", command, &a, on_over, &b))
{
if(!strcmp(command, "quit"))
{
break;
}
if(((a < 0) || (a >= number_of_blocks)) ||
((b < 0) || (b >= number_of_blocks)))
{
/* Block number out of bounds, ignored */
continue;
}
b_over_a = FALSE;
a_over_b = FALSE;
if(blocks[b].stack_number == blocks[a].stack_number)
{
int s = blocks[b].stack_number;
block_type* blockP = stacks[s].top;
/* Identified a and b are in the same stack. Start at the top
and work your way down and see which is higher, a or b */
while(blockP != NULL)
{
if(blockP->block_number == b)
{
b_over_a = TRUE;
break;
}
if(blockP->block_number == a)
{
a_over_b = TRUE;
break;
}
blockP = blockP->block_below;
}
}
if(strncmp(command, "pile", 4) == 0)
{
if(b_over_a)
{
continue;
}
if(a_over_b)
{
s = blocks[a].stack_number;
/* Check to see if there are blocks between a and b, if not
then ignore command */
if(!((&blocks[a] == stacks[s].top) && (blocks[a].block_below != &blocks[b])))
{
continue;
}
}
if(strncmp(on_over, "onto", 4) == 0)
{
return_blocks_above(b);
s = blocks[a].stack_number;
top = pop_stack(s, a);
s = blocks[b].stack_number;
push_stack(s, a, top);
} else
if(strncmp(on_over, "over", 4) == 0)
{
s = blocks[a].stack_number;
top = pop_stack(s, a);
s = blocks[b].stack_number;
push_stack(s, a, top);
}
} else
if(strncmp(command, "move", 4) == 0)
{
if(strncmp(on_over, "onto", 4) == 0)
{
return_blocks_above(a);
return_blocks_above(b);
s = blocks[a].stack_number;
pop_single(s);
s = blocks[b].stack_number;
push_single(s, a);
} else
if(strncmp(on_over, "over", 4) == 0)
{
return_blocks_above(a);
s = blocks[a].stack_number;
pop_single(s);
s = blocks[b].stack_number;
push_single(s, a);
}
}
}
output_state(number_of_blocks);
}[/c]
[c]#include <stdio.h>
#ifndef ONLINE_JUDGE
#include <fcntl.h>
#endif
#define FALSE 0
#define TRUE 1
#define NIL 26
typedef struct b_type
{
int block_number;
int stack_number;
struct b_type* block_below;
} block_type;
typedef struct s_type
{
block_type* top;
} stack_type;
block_type blocks[25];
stack_type stacks[25];
void push_single(int s, int b)
{
blocks.block_below = stacks[s].top;
stacks[s].top = &blocks;
blocks.stack_number = s;
}
int pop_single(int s)
{
int b;
if(stacks[s].top == NULL)
{
printf("NIL!\n");
exit(1);
return NIL;
}
else
{
b = stacks[s].top->block_number;
stacks[s].top = blocks.block_below;
blocks.block_below = NULL;
return b;
}
}
void push_stack(int s, int b, int top)
{
block_type* blockP;
blocks.block_below = stacks[s].top;
stacks[s].top = &blocks[top];
blockP = stacks[s].top;
while(blockP->block_number != b)
{
blockP->stack_number = s;
blockP = blockP->block_below;
}
blocks.stack_number = s;
}
int pop_stack(int s, int b)
{
int top;
if((stacks[s].top == NULL) || (blocks.stack_number != s))
{
printf("NIL!\n");
return NIL;
}
else
{
top = stacks[s].top->block_number;
stacks[s].top = blocks.block_below;
blocks.block_below = NULL;
return top;
}
}
void initialize_blocks(int number_of_blocks)
{
int i;
for(i=0; i<number_of_blocks; i++)
{
stacks.top = NULL;
blocks.block_number = i;
blocks.stack_number = NULL;
blocks.block_below = NULL;
push_single(i, i);
}
}
void output_state(int number_of_blocks)
{
int numbers[25];
int i, j, k;
block_type* blockP;
for(i=0; i<number_of_blocks; i++)
{
printf("%d:", i);
blockP = stacks.top;
j=0;
while(blockP != NULL)
{
numbers[j] = blockP->block_number;
j++;
blockP = blockP->block_below;
}
j--;
for(; j>=0; j--)
{
printf(" %d", numbers[j]);
}
printf("\n");
}
}
void return_to_initial_position(int b)
{
int s;
s = blocks[b].stack_number;
pop_single(s);
push_single(b, b);
}
void return_blocks_above(int b)
{
int s;
int c;
s = blocks[b].stack_number;
while(stacks[s].top != &blocks[b])
{
c = pop_single(s);
push_single(c, c);
}
}
int main(int argc, char* argv[])
{
int number_of_blocks;
int a;
int b;
int s, top;
char command[5] = {0,0,0,0,0};
char on_over[5] = {0,0,0,0,0};
int a_over_b;
int b_over_a;
#ifndef ONLINE_JUDGE
close (0); open (argv[1], O_RDONLY);
close (1); open ("myprog.out", O_WRONLY | O_CREAT, 0600);
#endif
scanf("%d\n", &number_of_blocks);
if((number_of_blocks <= 0) || (number_of_blocks >= 25))
{
exit(0);
/* This shouldn't happen because problem statement said it
wouldn't */
}
initialize_blocks(number_of_blocks);
while(scanf("%s %d %s %d\n", command, &a, on_over, &b))
{
if(!strcmp(command, "quit"))
{
break;
}
if(((a < 0) || (a >= number_of_blocks)) ||
((b < 0) || (b >= number_of_blocks)))
{
/* Block number out of bounds, ignored */
continue;
}
b_over_a = FALSE;
a_over_b = FALSE;
if(blocks[b].stack_number == blocks[a].stack_number)
{
int s = blocks[b].stack_number;
block_type* blockP = stacks[s].top;
/* Identified a and b are in the same stack. Start at the top
and work your way down and see which is higher, a or b */
while(blockP != NULL)
{
if(blockP->block_number == b)
{
b_over_a = TRUE;
break;
}
if(blockP->block_number == a)
{
a_over_b = TRUE;
break;
}
blockP = blockP->block_below;
}
}
if(strncmp(command, "pile", 4) == 0)
{
if(b_over_a)
{
continue;
}
if(a_over_b)
{
s = blocks[a].stack_number;
/* Check to see if there are blocks between a and b, if not
then ignore command */
if(!((&blocks[a] == stacks[s].top) && (blocks[a].block_below != &blocks[b])))
{
continue;
}
}
if(strncmp(on_over, "onto", 4) == 0)
{
return_blocks_above(b);
s = blocks[a].stack_number;
top = pop_stack(s, a);
s = blocks[b].stack_number;
push_stack(s, a, top);
} else
if(strncmp(on_over, "over", 4) == 0)
{
s = blocks[a].stack_number;
top = pop_stack(s, a);
s = blocks[b].stack_number;
push_stack(s, a, top);
}
} else
if(strncmp(command, "move", 4) == 0)
{
if(strncmp(on_over, "onto", 4) == 0)
{
return_blocks_above(a);
return_blocks_above(b);
s = blocks[a].stack_number;
pop_single(s);
s = blocks[b].stack_number;
push_single(s, a);
} else
if(strncmp(on_over, "over", 4) == 0)
{
return_blocks_above(a);
s = blocks[a].stack_number;
pop_single(s);
s = blocks[b].stack_number;
push_single(s, a);
}
}
}
output_state(number_of_blocks);
}[/c]