Workers soft third individual programming assignments Raiders


But behind the test for reference only enjoy the feeling with the
reference code is not for plagiarism


Code Address:

Github : https://github.com/O-VIGIA/031702414

PSP table:

PSP2.1 Personal Software Process Stages Estimated time consuming (hours) The actual time-consuming (hours)
Planning plan 1h 0.5
Estimate Estimate how much time this task requires 25h 26h
Development Develop 5h 1h
Analysis Needs analysis (including learning new technologies) 1h 1h
Design Spec Generate design documents 1h 1h
Design Review Design Review 1h 0.5h
Coding Standard Code specifications (development of appropriate norms for the current development) 1h 0.5h
Design Specific design 1.5h 0.5h
Coding Specific coding 5h 5h
Code Review Code Review 1h 0.5h
Test Test (self-test, modify the code, submit modifications) 1.5h 1h
Reporting report 2.5h 3h
test repor testing report 0.5h 1h
Size Measurement Computing workload 0.5h 0.5h
Postmortem & Process Improvement Plan Later summarized, and process improvement plan 2.5h 3h
total 25h 19h

Writing process:

The first stage:

Released the next day only to see the work actually have a job, and then turn on the phone with a look at the blog title. Roughly glanced found to be independent of the number of questions, then I thought did a very similar deep search yourself before backtracking OJ topic, feel conventional thinking should not be too difficult to play. Sure enough, the day I called for the process to make a simple search deep nine bands Sudoku problem. I think the trouble is found out that the file read-write and pass command line parameters , and write blog analysis. I then started his own plan:

The first step: You have played the most conventional Sudoku engine fill algorithm

Thinking:

Check function used to determine the legality

Line 1 legality

2 legitimacy

3 small grids legitimacy for some special order separate

4 small grids need to determine the legality of Sudoku by thinking found a formula to calculate grid coordinates of the upper left corner Komiya

n-- grid number || || height-- number level-- only a small number of grids height of the step

width-- Komiya Komiya x-- cell width || || abscissa left corner cell-y - upper left corner cell ordinate Komiya

x=n/level /height *height

y=n %level /width *width

DFS + back to fill in a blank part of Sudoku

1 for all n grid numbered from left to right, top to bottom, beginning 0

Calculate the coordinates of the upper left corner of each numbered 2 to formula method where the row and column where the small house

3 attempts to enter the number and traversing No. 1 to 9 are the lattice 0

4 Check function by determining if the legality filled legitimate digital number n + 1 of the search continues down step three deep

5 illegal back a bit n and the number of grid reduced to 0

code show as below

/* Check函数:判断key填入n时是否满足条件 */

/* ---Check Begin--- */
bool Check(int n,int key)
{
  int x, y, height, width;
  for (int i=0;i<level;i++)// 判断n所在横列是否合法 
  {
      int j=n/level;// j为n竖坐标 
      if (num[j][i]==key) return false;
  }
  for (int i=0;i<level;i++)//判断n所在竖列是否合法
  {
      int j=n%level;//j为n横坐标 
      if (num[i][j]==key) return false;
  }
  /* 若为9,8,6,4阶数独则判断小宫格 */
  if (level==9||level==8||level==6||level==4) {
      /* x为n所在的小九宫格左顶点竖坐标 */
      /* y为n所在的小九宫格左顶点横坐标 */
      /* height为小宫格高度 */
      /* weidth为小宫格宽度 */
      switch (level) {
          case 9: {
              x = n / 9 / 3 * 3;
              y = n % 9 / 3 * 3;
              height = 3; width = 3;
              break;
          }
          case 8: {
              x = n / 8 / 4 * 4;
              y = n % 8 / 2 * 2;
              height = 4; width = 2;
              break;
          }
          case 6: {
              x = n / 6 / 2 * 2;
              y = n % 6 / 3 * 3;
              height = 2; width = 3;
              break;
          }
          case 4: {
              x = n / 4 / 2 * 2;
              y = n % 4 / 2 * 2;
              height = 2; width = 2;
              break;
          }
      }
      for (int i=x;i<x+height;i++)//判断n所在的小九宫格是否合法 
      {
          for (int j=y;j<y+width;j++)
          {
              if (num[i][j]==key) return false;
          }
      }
  }
  return true;//全部合法,返回正确
}
/* ---Check End--- */

/* DFS函数 :深搜+回溯 解决数独*/

/* ---DFS Begin--- */
int DFS(int n)
{
  if (n>(level*level-1))//所有的都符合,退出递归 
  {
      sign = true;
      return 0;
  }
  if (num[n/level][n%level]!= 0) //当前位不为空时跳过 
  {
      DFS(n+1);
  }
  else
  {
      for (int i=1;i<=level;i++)//否则对当前位进行枚举测试
      {
          if (Check(n,i)==true)//满足条件时填入数字
          {
              num[n/level][n%level] = i;

​```
          DFS(n+1);//继续搜索

          if (sign==true) return 0;//返回时如果构造成功,则直接退出

          num[n/level][n%level] = 0;//如果构造不成功,还原当前位
      }
  }
}
​```

}
/* ---DFS Begin--- */
/* init_num 函数:初始化num数组 */
/* ---init_num Begin--- */
void init_num() {
  for (int i = 0; i < 20; ++i)
      for (int j = 0; j < 20; ++j)
          num[i][j] = 0;
}
/* ---init_num end--- */

second stage:

He finished the main engines, on account of the issues the command line parameter passing, so Baidu.

After Baidu found that

int main(int argc, char *argv[])

the main function is inside the parameters of the original meaning of a

Briefly argc represents the number of command line arguments the argv [] array which is the content of the command line parameter (string type)

Then I started my own thinking:

Our standard command line is long Suduku.exe -m 8 -n 10000 -i input.txt -o ouput.txtso

Command line parameters I need to pass the order number 10000 8 reads the file name of the output file name output.txt input.txt these four parameters

After the test I found that argv [0] is Suduku.exe

I think so and so argv [2] argv [4] argv [6] argv [8] it is not exactly what I wanted parameters

If so after the test

int main(int argc, char *argv[]) {
    /*从命令行接收参数 */ 
    level = atoi(argv[2]);
    int m = atoi(argv[2]);
    int n = atoi(argv[4]);
    

​```
/*对多个数独进行操作*/ 
for(int kk=1;kk<=n;++kk){
    sign=false;
    init_num();
    sudu sudu1(m);//构造出m阶数独 
    sudu1.Martrix_input((int*)sudu1.Martrix, m, (char*)argv[6]);//从命令行指定的文件名中读入一个m阶数独 
    sudu1.Martrix_num_sudu((int*)sudu1.Martrix, m);//数独->数组 
    DFS(0); //数独数组计算引擎 
    sudu1.Martrix_sudu_num((int*)sudu1.Martrix,m);//数组->数独 
    sudu1.Martrix_output((int*)sudu1.Martrix, m, (char*)argv[8]);//在命令行指定的文件名中输出数独的解 
} 
​```

}

The third stage:

Solve the problem of command line parameters passed, the following is the file stream operations.

Baidu a moment to provide a simple file stream operations found that C ++ fstream library

E.g:

ifstream infile;
infile.open(filename);
infile>>“software”;
infile.close();

But in order to read after a complete Sudoku also need to read next Sudoku

After extremely crazy thinking

I still go to Baidu, and Baidu found tellg after () and seekg () these two documents pointer operations

In simple terms tellg () it is to read a position after coming out Sudoku

seekg (offset, ios :: beg) is offset offset position from the current position ios :: cur where you specify the starting position ios :: beg the last one ios :: end

(I tried this thing for a long time, a lot of open pit pit is to use a binary file may appear a little inferior to offset problems)

I started my thinking:

If I put the first offset is initialized to 0

Each time the reading position with a single case of tellg number () coming out of the offset assigned to the start bit shift offset is then read from the file hhhh Destiny

Write much simpler directly with additional ways to write the next Sudoku solution in additional write behind specify the file name

outfile.open(filename,ios::app);//以后继方式打开文件以便继续写 

The fourth stage:

Then I suddenly need to establish its own class because he completed only a few parameters and data I can initialize the class

Then began

I do not know why I put this command term

Anyway, he began a long way (for some reason can not Quanfa Code)

class sudu 
{
public:
    int row;//row=col行列相等 
    int *Martrix = new int[row*row];//创建时new一个数组 
    void Martrix_input(int *Martrix,int row,const char* filename);
    void Martrix_output(int *Martrix,int row,const char* filename);
    void Martrix_sudu_num(int *Martrix,int row);
    void Martrix_num_sudu(int *Martrix,int row);
    sudu(int a);
    ~sudu();
};

thought:

Sudoku sudu (int a) a level-> a level of order to construct a

* Martrix used to store a number of separate data read out from the file delete the destructor ~ sudu () in [] off

Martrix_input reading a grade level bits of data from the file Sudoku

After several engine after only solution Martrix_output write to the file

Martrix_sudu_num salted function One will construct a good num Sudoku incoming data in order to solve the problem Martrix this Sprouts

Martrix_num_sudu salted function num II will have been completed to solve Sudoku Martrix data into the output file in order to call a class function

The fifth stage

Finishing Code, rationalize and .h cpp call each other, that is,

stdafx.cpp
stdafx.h
Sudoku.cpp

The relationship between these three things

stdafx.h is used to define a variety of resources

stdafx.cpp of resources used to achieve stdafx.h

Sudoku.cpp just a reference library functions as reference resources in stdafx.h

When then submitted to the attention of Git build a .gitignore file upload other files to ignore

The sixth stage

Examination and analysis of the code optimization algorithm

Of course, my favorite is the optimization algorithms, and some Sudoku interesting story I'll look at these two

! ! Ultimate algorithm! !

Various tests .png

! Finally to make me happy place

First on large:

Unmeasured first float

A Sudoku: Due card consequent ask

I do not know what you two together

Finnish mathematician Yin Kala, spent three months to design the world's far more difficult largest Sudoku game, but it is only one answer. Yin Kala said that only the fastest ability to mind the smartest people think to crack this game. This is the British " Daily Mail " reported a June 30, 2012 of.

8 0 0 0 0 0 0 0 0
0 0 3 6 0 0 0 0 0
0 7 0 0 9 0 2 0 0
0 5 0 0 0 7 0 0 0
0 0 0 0 4 5 7 0 0
0 0 0 1 0 0 0 3 0
0 0 1 0 0 0 0 6 8
0 0 8 5 0 0 0 1 0
0 9 0 0 0 0 4 0 0

0 0 5 3 0 0 0 0 0 
8 0 0 0 0 0 0 2 0
0 7 0 0 1 0 5 0 0
4 0 0 0 0 5 3 0 0
0 1 0 0 7 0 0 0 6
0 0 3 2 0 0 0 8 0
0 6 0 5 0 0 0 0 9
0 0 4 0 0 0 0 3 0
0 0 0 0 0 9 7 0 0
9阶答案
传说级因卡因之问1:
8 1 2 7 5 3 6 4 9
9 4 3 6 8 2 1 7 5
6 7 5 4 9 1 2 8 3
1 5 4 2 3 7 8 9 6
3 6 9 8 4 5 7 2 1
2 8 7 1 6 9 5 3 4
5 2 1 9 7 4 3 6 8
4 3 8 5 2 6 9 1 7
7 9 6 3 1 8 4 5 2

传说级因卡因之问2:
1 4 5 3 2 7 6 9 8
8 3 9 6 5 4 1 2 7
6 7 2 9 1 8 5 4 3
4 9 6 1 8 5 3 7 2
2 1 8 4 7 3 9 5 6
7 5 3 2 9 6 4 8 1
3 6 7 5 4 2 8 1 9
9 8 4 7 6 1 2 3 5
5 2 1 8 3 9 7 6 4
Two standard 99 Sudoku Sudoku test blog

See the original question and answer blog jobs

Three hardcore Sudoku number 88 alone

0 8 0 7 5 2 0 0
0 0 4 0 0 0 0 0 
0 3 0 0 0 6 0 0
5 2 0 0 0 0 0 1
0 0 0 0 0 0 6 0
0 0 0 0 0 0 5 2
0 4 6 0 0 8 0 0
0 0 3 0 0 1 8 0

0 4 0 0 0 3 0 0
0 3 0 8 6 0 0 0
0 0 0 0 0 0 0 2
0 0 1 0 7 0 0 5
0 0 0 0 0 1 0 0
1 0 5 2 0 0 7 3
0 6 0 3 0 0 0 8
0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0
0 2 0 0 0 0 8 0
0 0 6 3 0 8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 1 0 0

1 0 0 0
4 0 1 0
0 0 0 0 
0 0 0 2
4阶答案:
3 1 2 4
4 2 1 3
2 4 3 1
1 3 4 2

4 2 1 3
1 3 2 4
2 4 3 1
3 1 4 2

1 3 2 4
4 2 1 2
2 4 3 1
3 1 4 2
Test it half a family portrait

Guess you like

Origin www.cnblogs.com/Martrix-revolution/p/11569573.html