| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1743 人关注过本帖
标题:C语言 生命游戏 OJ题Wrong Answer 求大佬指导。。。
只看楼主 加入收藏
AqourSoro
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-3-5
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
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 ' ';
}
搜索更多相关主题的帖子: for int long length row 
2019-03-05 10:17
AqourSoro
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-3-5
收藏
得分:0 
题目描述主要是要求实现一个模拟康威生命游戏的c程序,最好使用malloc动态管理使用的内存,
接受标准输入形如:
6 6 20
.X...X
X.X.X.
X...X.
X..XX.
..XX.X
...X.X
然后输出:
...X..
..X.X.
..X.X.
...X..
......
......
本人程序在自己debug的时候一直都符合要求,但不知为何一直提示wrong answer,不知有没有大佬指点指点。。。
2019-03-05 10:25
lxk1732942
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:450
专家分:425
注 册:2018-9-4
收藏
得分:20 
具体是哪里有问题?
看了看没发现啥问题...

[此贴子已经被作者于2019-3-7 19:04编辑过]

2019-03-07 18:47
快速回复:C语言 生命游戏 OJ题Wrong Answer 求大佬指导。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020830 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved