#include #include #include #define MAX_BOARDS 100 static char *ReadLine(char *p, size_t size, FILE *fp) { if ((p=fgets(p, size, fp))) { size_t l = strlen(p); while(l && p[l-1] == '\n') { p[--l] = 0; } } return p; } int main(void) { char buff[1024]; int *num = NULL; int num_count = 0; int board[MAX_BOARDS][5][5] = {0}; int board_count = 0; char *p = NULL; int f = 0; int x = 0; int y = 0; int i = 0; int winner[MAX_BOARDS] = {0}; int winner_count = 1; int last_board = 0; int winner_num = -1; int sum = 0; ReadLine(buff, sizeof buff, stdin); p = strtok(buff, ","); while(p) { num = realloc(num, (num_count + 1) * sizeof *num); num[num_count++] = atoi(p); p = strtok(NULL, ","); } while (ReadLine(buff, sizeof buff, stdin)) { if (board_count == MAX_BOARDS) { printf("Too many boards\n"); return 1; } for(y = 0; y < 5; y++) { ReadLine(buff, sizeof buff, stdin); for(x = 0; x < 5; x++) { p = strtok(x == 0 ? buff : NULL, " "); board[board_count][y][x] = atoi(p); } } board_count++; } for(i = 0; i < num_count && winner_count != board_count + 1; i++) { for(f = 0; f < board_count; f++) { for(y = 0; y < 5; y++) { for(x = 0; x < 5; x++) { if (board[f][y][x] == num[i]) { board[f][y][x] = -1; } } } } for(f = 0; f < board_count; f++) { for(y = 0; y < 5; y++) { if (board[f][y][0] == -1 && board[f][y][1] == -1 && board[f][y][2] == -1 && board[f][y][3] == -1 && board[f][y][4] == -1) { if (winner[f] == 0) { winner[f] = winner_count++; } } } for(x = 0; x < 5; x++) { if (board[f][0][x] == -1 && board[f][1][x] == -1 && board[f][2][x] == -1 && board[f][3][x] == -1 && board[f][4][x] == -1) { if (winner[f] == 0) { winner[f] = winner_count++; } } } } winner_num = num[i]; } printf("i/num_count = %d/%d\n", i, num_count); for(f = 0; f < board_count; f++) { if (winner[f] == board_count) { last_board = f; } } printf("board_count = %d\n", board_count); printf("winner_num = %d\n", winner_num); printf("last_board = %d\n", f); sum = 0; for(y = 0; y < 5; y++) { for(x = 0; x < 5; x++) { if (board[last_board][y][x] != -1) { sum += board[last_board][y][x]; } } } printf("sum = %d\n", sum); printf("product = %d\n", sum * winner_num); return 0; }