1.代碼托管地址
https://github.com/dong199903/suDu
2.耗費時間
| 列表 | Personal Software Process Stages | 預(yù)計耗時 | 實際耗時 |
|---|---|---|---|
| Planning | 計劃 | 60 | 30 |
| Estimate | 估計這個任務(wù)需要的時間 | 500 | 300 |
| Development | 開發(fā) | 140 | 100 |
| Analysit | 需求分析 | 100 | 150 |
| Design Spec | 生成設(shè)計文檔 | 100 | 100 |
| Coding Standard | 代碼規(guī)范 | 90 | 100 |
| Design | 具體設(shè)計 | 70 | 60 |
| Codeing | 具體編碼 | 1800 | 2000 |
| Code Review | 代碼復(fù)審 | 100 | 90 |
| Test | 測試 | 90 | 80 |
| Reporting | 報告 | 80 | 90 |
| Test Report | 測試報告 | 50 | 50 |
| Size Measure | 計算工作量 | 60 | 40 |
| Postmotre | 事后總結(jié) | 50 | 40 |
| 合計 | 3150 | 3010 |
3.需求分析
- 用熟悉的編程語言實現(xiàn)數(shù)獨的題目
- 總體分為生成不重復(fù)的數(shù)獨文件,并求解數(shù)獨文件的數(shù)獨程序
- 要求按照軟件工程規(guī)范來設(shè)計,且數(shù)獨的第一個是按照學號計算來的
- 要求數(shù)獨可以用兩種命令去實現(xiàn)對應(yīng)的功能
- 要求列出程序的性能圖
4.解題思想
- 按照題目要求,大致分為兩個模塊去分別實現(xiàn),所以我初步設(shè)計兩個類,一個是Sudu_Construct類,一個是Sudu_Deal類分別處理數(shù)獨,其中Sudu_Construct類就是用戶可以通過-r 行數(shù)的方式構(gòu)造出數(shù)獨,并將數(shù)獨文件寫在suduku.txt里面。另外一個Sudu_Deal類是求解對應(yīng)數(shù)獨文件,求出數(shù)獨的解過程。
- 首先要求構(gòu)造數(shù)獨,且不能重復(fù),那我開始還很猶豫用什么寫,于是搜集網(wǎng)上各種建議,并通過test文件測試這些方法,決定用全排列也就是回溯的方法去解決問題。
5.設(shè)計過程
- 大體固定兩個類,一個構(gòu)造數(shù)獨的類,一個求解數(shù)獨的類。
- 結(jié)合網(wǎng)絡(luò)資料,從模仿學習到自己的獨立去完整實現(xiàn)。
- 不斷優(yōu)化各類程序解決的復(fù)雜,保證程序健壯性,符合軟件工程代碼規(guī)范
- 將每次改進和代碼優(yōu)化以及文檔的編寫,都去逐步完善。
6.程序性能時間

1.jpg
7.代碼說明
首先是兩個類的定義
- Sudu_Construct類的定義
#pragma once
//構(gòu)造數(shù)獨
class Sudu_Construct
{
private:
int count;//個數(shù)
public:
Sudu_Construct(int num);//構(gòu)造函數(shù)
~Sudu_Construct();
void getSudu();
int getCount();
};
- Sudu_Deal類的定義
#pragma once
class Sudu_Deal {
public:
int data[10][10];
int biaoji[10][10];
public:
Sudu_Deal();
void deal();
int find(int x,int y);
~Sudu_Deal();
};
- 構(gòu)造數(shù)獨的核心過程
//構(gòu)造數(shù)獨
void Sudu_Construct::getSudu()
{
int count = 0,temp;
int port[10] = { 0,0,6,5,4,3,2,1,7,8 };
char str[10] = { '9','3','1','2','7','4','5','6','8','9' };
if (count < this->count)
{
for (int a = 0; a < 6; a++)
{
if (count == this->count)
break;
if (a)
next_permutation(port + 4, port + 6);
for (int b = 0; b < 6; b++)
{
if (b)
next_permutation(port + 7, port + 9);
int t = 0;
//char kong = ' ';
do {
if (t)
next_permutation(str + 2, str + 9);
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
if (j - port[i] >= 0)
temp = j - port[i];
else
temp = j - port[i] + 9;
//putchar(str[temp % 9]);
cout << str[temp % 9];
if (j < 9)
//putchar(' ');
cout << " ";
}
cout << endl;
}
count++;
t++;
if (count == this->count)
break;
else
cout << endl;
} while (t<40320);
if (count == this->count)
break;
}
}
}
}
- 處理數(shù)獨
int Sudu_Deal::find(int x, int y)
{
if (x > 9 || y > 9) return 1;
if (biaoji[x][y])
{
if (y < 9)
{
if (find(x, y + 1))
return 1;
}
else
{
if (x < 9)
{
if (find(x + 1, 1))
return 1;
}
else
return 1;
}
}
else
{
for (int n1 = 1; n1 <= 9; n1++)
{
int slogan = 1;
for (int i = 1; i <= 9; i++)//看列有沒有
{
if (this->data[i][y] == n1)
{
slogan = 0;
break;
}
}
if (slogan)//看行有沒有
{
for (int j = 1; j <= 9; j++)
{
if (this->data[x][j] == n1)
{
slogan = 0;
break;
}
}
}
if (slogan)
{
int rawtop, columntop;
if (x % 3 == 0)
rawtop = x;
else
rawtop = (x / 3) * 3 + 3;
if (y % 3 == 0)
columntop = y;
else
columntop = (y / 3) * 3 + 3;
for (int i = rawtop - 2; i <= rawtop; i++)
{
for (int j = columntop - 2; j <= columntop; j++)
{
if (this->data[i][j] == n1)
{
slogan = 0;
break;
}
}
if (!slogan)
break;
}
}
if (slogan)//判斷過后看1-9放哪一個
{
this->data[x][y] = n1;
if (y < 9)
{
if (find(x, y + 1))
return 1;
}
else
{
if (x < 9)
{
if (find(x + 1, 1))
return 1;
}
else
return 1;
}
this->data[x][y] = 0;
}
}
}
return 0;
}
總結(jié)
通過本次個人項目數(shù)獨程序的完成,我用軟件工程中的需求分析,和代碼規(guī)范去理解問題,組織程序,到最后的實現(xiàn)完成,整個過程其實還是磕磕絆絆的,因為回溯法沒理解深刻,上手code也比較費勁,最后通過網(wǎng)上閱讀回溯,和問同學,完成了整個基本流程。