The third practice of software engineering jobs

My GitHub: mmbhwhh

PSP

PSP Estimated time consuming (hours) The actual time-consuming (hours)
plan 2 3
Estimate how much time this task requires 16 20
Develop 1 2
Needs analysis (including learning new technologies) 2 2
Generate design documents 2 3
Design Review 3 3
Specific design 2 3
Specific coding 3 5
Code Review 3 3
Test (self-test, modify the code, submit modifications) 1 2
report 2 3
Process Improvement Program 1 2
total 22 31

Think

When beginning to get the title, Sudoku thought it was my imagination, it currently resides row, column, affect the grids of diffusion will simply think as long as each number has been determined to go out, subject to the said Sudoku is a unique solution, which makes me even more confident with my train of thought can solve it, and then tried.

The idea of ​​beginning

General and first homework of many students, like Mo, is the Sudoku traverse twice, first find the entry (as the title says test data is a unique solution), to find a certain point, then it will impact diffusion to the rows, columns, grids, will eventually fill the entire table. Write code for a long time, but found that my train of thought can only fill part of the data, never played Sudoku I was too tender, calm down and think about it later, if the number of unique simple as what is also called Sudoku, from what I know who I wrong data, despite a Sudoku has only one solution, but it can not necessarily use logic to determine the full value directly, because I played out of my error code error results show that Motian indeed part of the data can not be determined, the value must be filled into temptation, although digital mapping legitimate, but the solution does not come out. This result makes me autistic place.

Second thoughts

We experienced the lessons of the last, and simply direct solution of this question by way of violence. From the first idea is to find the last grid has a grid, if found a vacancy, then give it conferred on the legal value, then keep looking, if not find there have been two cases. One is already filled out, the results came out, and it is legal is it the solution; the second is, can not find a legal value, but it is still empty, indicating that the previous assigned value does not, at this time and then go back and change until correct. code show as below:

check (u, i) function is detected in u (the grids of the grid numbered from the upper left corner as the starting point all the way to the lower right corner, such as 9 grids have 1,2,3 ......... ..80,81 number) position, fill i together illegal

bool check(int u, int t)
{
    int x, y;
    x = (u - 1) / type + 1;
    y = (u - 1) % type + 1;//找到u这个位置的坐标
    for (int i = 1; i <= type; i++)
    {
        if (gg[x][i] == t && i != y)    return false;
        if (gg[i][y] == t && i != x)   return false;

    }//判断这个位置填入的数会不会与行和列冲突

    if (type == 4 || type == 6 || type == 8 || type == 9)
    {
        int xs, ys;
        int beginx, beginy;//当前坐标所在宫格的左上角坐标,可当做起始坐标
        switch (type)//判断宫格的规模
        {
        case 4:
            xs = 2;
            ys = 2;
            break;
        case 6:
            xs = 2;
            ys = 3;
            break;
        case 8:
            xs = 4;
            ys = 2;
            break;
        case 9:
            xs = 3;
            ys = 3;
            break;
        }
        int flogx = xs;
        int flogy = ys;//flogx和flogy只是待会下面用到的循环次数
        beginx = (x - 1) / xs * xs + 1;
        beginy = (y - 1) / ys * ys + 1;
        for (int i = beginx; flogx > 0; flogx--, i++)
        {

            for (int j = beginy; flogy > 0; flogy--, j++)
            {
                if (t == gg[i][j] && (x != i || y != j))    return false;
            }
            flogy = ys;
        }
        flogy = xs;
    }//判断此位置填入的数会不会与宫格里的冲突

    return true;//能走到这里说明填的数合法

}

The above check function will be used in dfs function, the role is to search for dfs

bool dfs(int u)
{
    if (u > type*type)  return true;//迭代到编号以外,说明前面填的数都是合法的
    int x, y;
    x = (u - 1) / type + 1;
    y = (u - 1) % type + 1;//给它坐标化
    if (gg[x][y])   return dfs(u + 1);//如果这个位置有数字,就去下一个点
    else {
        for (int i = 1; i <= type; i++)
        {
            if (check(u, i))
            {

                gg[x][y] = i;
                if (dfs(u + 1))return true;
                gg[x][y] = 0;
            }
        }//这个位置还没填入数据,就随便给它一个值,然后再执行下一个点,如果下一个点不可以了就会回来这个点重新赋值
        return false;
    }
}

Test Results


Performance Testing

Guess you like

Origin www.cnblogs.com/xp3736----/p/11584704.html