C语言 生命游戏 OJ题Wrong Answer 求大佬指导。。。
学校OJ系统给出的一道题, 写出的程序给出了符合的结构却一直wrong answer。。。有没有好心的大佬帮我看看哪有问题。。DEBUG到不行了
以下是题目描述
然后是本人的代码。。。只有一部分写了注释,后半部分还没写。。大佬们谅解。。。
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> /*Used for BOOL type.*/ #define TRUE 1 #define FALSE 0 /*Define the decimal form of cells.*/ #define LIVING_CELL 10 #define DEAD_CELL 9 /*Indicate the surviving condition.*/ #define UPPER_BOUND 3 #define LOWER_BOUND 2 /*Define the Boolean type for judging whether a cell is alive.*/ typedef int BOOL; /*Global variables to record the size of the game, and steps to simulate.*/ long long int height, length, steps; /*An 2d-array to record cells from inputs and the state to output.*/ int** map; /*An 2d-array that constantly changes to record the number of cells around one cell.*/ int** environment; /*A method for request a piece of memory to create an 2d-array based on given rows and columns.*/ int** create_container(long int height, long int length); /*release the memory that is required by the container, disable the pointer.*/ void destory_container(int*** container, long int height); /*A method returns a boolean value which shows whether a cell is alive on a given position.*/ BOOL is_alive(long int row, long int col); /*A method checks surrounding cells of a cell with given position.*/ int check_environment(long int row, long int col); /*update environment array based on current cells.*/ void update_environment(); /*update the state for a single cell.*/ void update_cell(long int row, long int col); /*update all cells based on the environment array.*/ void update_map(); /*Two methods convert cells between decimal form and character form.*/ int char_to_int(char input); char int_to_char(int output); int main() { /*Initial pointers, which are 2d arrays.*/ map = NULL; environment = NULL; /*a pointer for temporarily record cells to be input and output.*/ char* tmp_ptr = NULL; scanf("%lld %lld %lld", &height, &length, &steps); getchar(); /*if inputs are invalid, exit.*/ if (height <= 0 || length <= 0 || steps <= 0) { return 1; } /*allocate memories for arrays.*/ map = create_container(height, length); environment = create_container(height, length); /*allocate memories for temper */ tmp_ptr = (char*)malloc(length * sizeof(char) + 1); memset(tmp_ptr, 0, sizeof(char)); /*read all cells line by line.*/ for (int i = 0; i < height; i++) { scanf("%s", tmp_ptr); for (int j = 0; j < length; j++) { /*convert it into decimal format and store in array.*/ map[i][j] = char_to_int(tmp_ptr[j]); } getchar(); } /*run the game based on the given steps.*/ for (int i = 0; i < steps; i++) { update_environment(); update_map(); } /*No need for recording environment, relase the memories.*/ destory_container(&environment, height); /*output the final state in the format of inputs.*/ for (int i = 0; i < height; i++) { /*convert decimals into characters,*/ /*output through tmp_ptr. */ for (int j = 0; j < length; j++) { tmp_ptr[j] = int_to_char(map[i][j]); } printf("%s", tmp_ptr); if(i < height - 1) { printf("\n"); } } /*release all memories that are requested before.*/ free(tmp_ptr); tmp_ptr = NULL; destory_container(&map, height); return 0; } int** create_container(long int height, long int length) { /*local pointer points the memories to return.*/ int** ptr_map = NULL; /*using malloc to allocate memories in suitable size.*/ ptr_map = (int**)malloc(height * sizeof(int*)); if (ptr_map == NULL) { /*if allocate failed, return null*/ return NULL; } /*initial memories.*/ memset(ptr_map, 0, height * sizeof(int*)); for (int i = 0; i < height; i++) { ptr_map[i] = (int*)malloc(length * sizeof(int)); if (ptr_map[i] == NULL) { for (int j = 0; j < i; j++) { free(ptr_map[j]); ptr_map[j] = NULL; } free(ptr_map); return NULL; } memset(ptr_map[i], 0, sizeof(int)); } return ptr_map; } void destory_container(int*** container, long int height) { for (int i = 0; i < height; i++) { free((*container)[i]); (*container)[i] = NULL; } free(*container); *container = NULL; } BOOL is_alive(long int row, long int col) { if (map[row][col] == LIVING_CELL) { return TRUE; } else { return FALSE; } } int check_environment(long int row, long int col) { int envir = 0; if ((row == 0) && (col == 0)) { envir += (is_alive(0, 1) + is_alive(1, 1) + is_alive(1, 0)); } if ((row == 0) && (col == length - 1)) { envir += (is_alive(0, col - 1) + is_alive(1, col - 1) + is_alive(1, col)); } if ((row == height - 1) && (col == length - 1)) { envir += (is_alive(row - 1, col) + is_alive(row - 1, col - 1) + is_alive(row, col - 1)); } if ((row == height - 1) && (col == 0)) { envir += (is_alive(row - 1, 0) + is_alive(row - 1, 1) + is_alive(row, 1)); } if ((row == 0) && (0 < col) && (col < length - 1)) { envir += (is_alive(0, col - 1) + is_alive(1, col - 1) + is_alive(1, col) + is_alive(1, col + 1) + is_alive(0, col + 1)); } if ((0 < row) &&(row < height - 1) && (col == 0)) { envir += (is_alive(row - 1, 0) + is_alive(row - 1, 1) + is_alive(row, 1) + is_alive(row + 1, 1) + is_alive(row + 1, 0)); } if ((0 < row) &&(row < height - 1) && (col == length - 1)) { envir += (is_alive(row - 1, col) + is_alive(row - 1, col - 1) + is_alive(row, col - 1) + is_alive(row + 1, col - 1) + is_alive(row + 1, col)); } if ((row == length - 1) && (0 < col) && (col < length - 1)) { envir += (is_alive(row, col - 1) + is_alive(row - 1, col - 1) + is_alive(row - 1, col) + is_alive(row - 1, col + 1) + is_alive(row, col + 1)); } if ((0 < row) && (row < height - 1) && (0 < col) && (col < length - 1)) { envir += (is_alive(row, col - 1) + is_alive(row - 1, col - 1) + is_alive(row - 1, col) + is_alive(row - 1, col + 1) + is_alive(row, col + 1) + is_alive(row + 1, col + 1) + is_alive(row + 1, col) + is_alive(row + 1, col - 1)); } return envir; } void update_cell(long int row, long int col) { int condition = environment[row][col]; if (is_alive(row, col) == TRUE) { if ((condition < LOWER_BOUND) || (condition > UPPER_BOUND)) { map[row][col] = DEAD_CELL; } else { map[row][col] = LIVING_CELL; } } else { if (condition == UPPER_BOUND) { map[row][col] = LIVING_CELL; } } } void update_map() { for (int i = 0; i < height; i++) { for (int j = 0; j < length; j++) { update_cell(i, j); } } } void update_environment() { for (int i = 0; i < height; i++) { for (int j = 0; j < length; j++) { environment[i][j] = check_environment(i, j); } } } int char_to_int(char input) { if (input == '.') { return 9; } else if (input == 'X') { return 10; } return 0; } char int_to_char(int output) { if (output == 9) { return '.'; } else if(output == 10) { return 'X'; } return ' '; }