2.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct listnode {
  4. struct listnode *next;
  5. char val;
  6. };
  7. typedef struct {
  8. struct listnode *head;
  9. } Stack;
  10. void stack_push(Stack *s, char val) {
  11. struct listnode *n = malloc(sizeof(struct listnode));
  12. n->next = s->head;
  13. n->val = val;
  14. s->head = n;
  15. }
  16. char* stack_top(Stack *s) {
  17. if (s->head == NULL) return NULL;
  18. return &(s->head->val);
  19. }
  20. void stack_pop(Stack *s) {
  21. if (s->head == NULL) return;
  22. struct listnode *next = s->head->next;
  23. free(s->head);
  24. s->head = next;
  25. }
  26. char buf[4096];
  27. int main() {
  28. Stack s[9];
  29. FILE *fp = fopen("input", "r");
  30. fread(buf, 1, 36 * 8, fp);
  31. for (int i = 0; i < 9; i++) {
  32. for (int j = 0; j < 8; j++) {
  33. char c = buf[(7 - j) * 36 + i * 4 + 1];
  34. if (c != ' ') {
  35. stack_push(&s[i], c);
  36. }
  37. }
  38. }
  39. fgets(buf, 4096, fp);
  40. fgets(buf, 4096, fp);
  41. int amount, from, to;
  42. while (fscanf(fp, "move %d from %d to %d\n", &amount, &from, &to) > 0) {
  43. for (int i = 0; i < amount; i++) {
  44. char c = *stack_top(&s[from - 1]);
  45. stack_pop(&s[from - 1]);
  46. buf[i] = c;
  47. }
  48. for (int i = 0; i < amount; i++) {
  49. int n = amount - i - 1;
  50. stack_push(&s[to-1], buf[n]);
  51. }
  52. }
  53. for (int i = 0; i < 9; i++) {
  54. printf("%c", *stack_top(&s[i]));
  55. }
  56. printf("\n");
  57. return 0;
  58. }