請問是否有辦法得知輸入的測資呢 ???
thanks!
請問是否有辦法得知輸入的測資呢 ???
thanks!
補上程式碼
請問是否有辦法得知輸入的測資呢 ???
thanks!
box list 搬移的時候忘記所有的 box 都要改 table id
以下為修正後 AC 的程式碼
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
typedef struct box_s box_t;
typedef struct table_s table_t;
typedef struct box_s {
uint32_t box_id;
uint32_t table_id;
box_t *next;
} box_t;
typedef struct table_s {
uint32_t table_id;
box_t *box_list;
} table_t;
table_t *tables;
box_t *boxs;
uint32_t n_tables;
void show_tables() {
box_t *tmp;
uint32_t idx;
for (idx = 0 ; idx < n_tables ; idx++) {
printf("%d: ", idx);
for (tmp = tables[idx].box_list->next ; tmp != NULL ; tmp = tmp->next) {
printf("%d ", tmp->box_id);
}
printf("\n");
}
}
bool is_same_table(box_t *a, box_t *b) {
if (a->table_id == b->table_id) {
return true;
}
return false;
}
void put_to_table(table_t *src_table, table_t *dst_table, box_t *box) {
box_t *tmp;
for (tmp = src_table->box_list ; tmp->next != box ; tmp = tmp->next);
tmp->next = NULL;
for (tmp = dst_table->box_list; tmp->next != NULL ; tmp = tmp->next);
tmp->next = box;
for (tmp = box ; tmp != NULL ; tmp = tmp->next)
tmp->table_id = dst_table->table_id;
}
void put_back(box_t *box) {
box_t *tmp, *tmp2;
while (box->next != NULL) {
for (tmp = box->next ; tmp->next != NULL; tmp = tmp->next);
put_to_table(&tables[tmp->table_id], &tables[tmp->box_id], tmp);
}
}
void action_move(char *action, box_t *source, box_t *target) {
if (strcmp(action, "onto") == 0) {
put_back(source);
put_back(target);
put_to_table(&tables[source->table_id], &tables[target->table_id], source);
} else if (strcmp(action, "over") == 0) {
put_back(source);
put_to_table(&tables[source->table_id], &tables[target->table_id], source);
} else {
return;
}
}
void action_pile(char *action, box_t *source, box_t *target) {
if (strcmp(action, "onto") == 0) {
put_back(target);
put_to_table(&tables[source->table_id], &tables[target->table_id], source);
} else if (strcmp(action, "over") == 0) {
put_to_table(&tables[source->table_id], &tables[target->table_id], source);
} else {
return;
}
}
int main() {
char cmd[5];
char action[5];
uint32_t source, target;
uint32_t count;
while (scanf("%d", &n_tables) != EOF) {
if (n_tables == 0 || n_tables >= 25)
continue;
tables = (table_t *)malloc(sizeof(table_t) * n_tables);
boxs = (box_t *)malloc(sizeof(box_t) * n_tables);
for (count = 0 ; count < n_tables ; count++) {
boxs[count].box_id = count;
boxs[count].table_id = count;
boxs[count].next = NULL;
tables[count].table_id = count;
tables[count].box_list = (box_t *)malloc(sizeof(box_t));
tables[count].box_list->next = &boxs[count];
}
while (scanf("%s", cmd) != EOF) {
if (strcmp(cmd, "quit") == 0) {
show_tables();
break;
}
scanf(" %d %s %d", &source, action, &target);
if (source >= n_tables || target >= n_tables)
continue;
if (source == target)
continue;
if (is_same_table(&boxs[source], &boxs[target]))
continue;
if (strcmp(cmd, "move" ) == 0) {
action_move(action, &boxs[source], &boxs[target]);
} else if (strcmp(cmd, "pile") == 0) {
action_pile(action, &boxs[source], &boxs[target]);
} else {
continue;
}
}
free(boxs);
free(tables);
}
return 0;
}