Giter VIP home page Giter VIP logo

sudoku's People

Watchers

 avatar

sudoku's Issues

内存泄漏

int(*SudokuSolver::getSolution())[9]
{
    int(*p)[9] = new int[9][9];
    for (int i = 0; i < 9; ++i)
    {
        for (int j = 0; j < 9; ++j)
        {
            p[i][j] = problem[i][j];
        }
    }
    return p;
}

该函数申请的空间没有得到释放 建议在外部建立一个静态缓存 然后把这个缓存的指针传入这个函数中

vector<int(*)[9]> SudokuInput(char * filename)
{
    ifstream file;
    file.open(filename, ios::in);
    int linenum = 0, curline = 0;
    vector<int(*)[9]> ProblemSet;
    int(*temp)[9] = new int[9][9];
    if (!file.is_open())
    {
        cerr << "fail to open file!" << endl;
        return ProblemSet;
    }
    while (!file.eof())
    {
        string buffer;
        getline(file, buffer);
        if (buffer.empty())
            continue;
        linenum++;
        if (linenum % 9 == 1)
        {
            temp = new int[9][9];
            curline = 0;
        }
        for (int i = 0; i < 9; ++i)
        {
            temp[curline][i] = buffer[2 * i] - '0';
        }
        curline++;
        if (linenum % 9 == 0)
        {
            ProblemSet.push_back(temp);
            temp = NULL;
        }
    }
    file.close();
    return ProblemSet;
}

这个容器内指针所得到的空间没有得到释放,建议在main方法尾部加以处理

代码风格问题

命名

  1. 大部分函数采用了Pascal命名法,但是有部分函数采用了lowerCamelCase命名法(SudokuMaker::check,SudokuMaker::fill),建议可以统一
  2. 1中的问题也出现在了变量命名中,另外一些变量命名显得过于简略。
    a. 过多的使用单个字符命名,比如q,M,a,b这些,有些是让人搞不清楚这个变量是什么(得看定义),有些是不容易区分行列(嵌套循环里)
    b. 一些变量名起的看不出意义 如
void SudokuOutput(char *ret, int maxnum, char *ret2);

中的 ret ,ret2
建议改成 from,to 或者 src,dst 这样的名字 另外这个函数的名字不大很适合其功能,可以试试用Format这样的词来取名

  1. 类名SudokuSolve 可以改成 SudokuSolver
  2. 之前取错了的文件名可以改一下,以匹配类名

变量使用

  1. 如下
 char *result = new char[1000000 * 81 + 1];
 char *q = new char[1000000 * (81 * 2 + 1)];

a. 使用了写死的容量大小,可以改成常量表示
b. 作为全局缓冲的数组可以写成静态而不是动态的
c. 建议注释说明空间这样计算的意义

类的设计

class SudokuSolve {
public:
	bool Solve(int r, int l);//递归填数
	bool check(int r, int l, int num);//测试同行同宫同列是否已有num
	void ProblemInit(int p[9][9]);//初始化
    int(*getSolution())[9];//返回解
private:
	int problem[9][9];
};
class SudokuMaker {
public:
    bool fill(int r, int l, char *ret);//递归填数
    bool check(int r, int l, int num);//判断在[r,l]处放入num是否符合数独规则
    void RequestInit(int n);//初始化需求
private:
    int maxnum;//需要生成的数独终局数
    int count;//当前已生成的数独终局数
    int M[9][9];//维护的数独棋盘
};
  1. Solve、check、fill等函数作为类的内部实现细节可以设置其为private
  2. r,l等参数作为具体算法相关细节可以隐藏,因为其不必由外界传入参数来工作

新增

  1. delete 后不必使用小括号
  2. 在类的内部使用本类函数不用进行域的限制
  3. 返回二维数组的函数可以用 int[][] func(); 来替代 int (func*())[]; 以增强可读性 同理适用于 int [][] 替代 int (*)[]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.